LCOV - code coverage report
Current view: top level - src - deepmd_wrapper.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:262480d) Lines: 14 15 93.3 %
Date: 2024-11-22 07:00:40 Functions: 3 4 75.0 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2024 CP2K developers group <https://cp2k.org>                                   !
       4             : !                                                                                                  !
       5             : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6             : !--------------------------------------------------------------------------------------------------!
       7             : 
       8             : ! **************************************************************************************************
       9             : !> \brief Interface to the DeePMD-kit or a c++ wrapper.
      10             : !> \par History
      11             : !>      07.2019 created [Yongbin Zhuang]
      12             : !>      06.2021 refactored [Yunpei Liu]
      13             : !>      10.2023 adapt to DeePMD-kit C Interface [Yunpei Liu]
      14             : !> \author Yongbin Zhuang
      15             : ! **************************************************************************************************
      16             : 
      17             : MODULE deepmd_wrapper
      18             :    USE ISO_C_BINDING,                   ONLY: C_CHAR,&
      19             :                                               C_DOUBLE,&
      20             :                                               C_INT,&
      21             :                                               C_NULL_CHAR,&
      22             :                                               C_NULL_PTR,&
      23             :                                               C_PTR
      24             :    USE kinds,                           ONLY: dp
      25             : #include "./base/base_uses.f90"
      26             : 
      27             :    IMPLICIT NONE
      28             :    PRIVATE
      29             :    PUBLIC :: deepmd_model_type, deepmd_model_load, deepmd_model_compute, deepmd_model_release
      30             : 
      31             :    TYPE deepmd_model_type
      32             :       PRIVATE
      33             :       TYPE(C_PTR)                          :: c_ptr = C_NULL_PTR
      34             :    END TYPE deepmd_model_type
      35             : 
      36             : CONTAINS
      37             : 
      38             : ! **************************************************************************************************
      39             : !> \brief Load DP from a model file.
      40             : !> \param filename Path to the model file.
      41             : !> \return Pointer to the DP model.
      42             : ! **************************************************************************************************
      43           2 :    FUNCTION deepmd_model_load(filename) RESULT(model)
      44             :       CHARACTER(len=*), INTENT(INOUT)                    :: filename
      45             :       TYPE(deepmd_model_type)                            :: model
      46             : 
      47             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'deepmd_model_load'
      48             : 
      49             :       INTEGER                                            :: handle
      50             :       INTERFACE
      51             :          FUNCTION NewDeepPot(filename) BIND(C, name="DP_NewDeepPot")
      52             :             IMPORT :: C_PTR, C_CHAR
      53             :             CHARACTER(kind=C_CHAR), DIMENSION(*)               :: filename
      54             :             TYPE(C_PTR)                                        :: NewDeepPot
      55             :          END FUNCTION
      56             :       END INTERFACE
      57             : 
      58           2 :       CALL timeset(routineN, handle)
      59             : 
      60             : #if defined(__DEEPMD)
      61           2 :       model%c_ptr = NewDeepPot(filename=TRIM(filename)//C_NULL_CHAR)
      62             : #else
      63             :       CPABORT("CP2K was compiled without libdeepmd_c library.")
      64             :       MARK_USED(filename)
      65             :       MARK_USED(model)
      66             : #endif
      67             : 
      68           2 :       CALL timestop(handle)
      69           2 :    END FUNCTION deepmd_model_load
      70             : 
      71             : ! **************************************************************************************************
      72             : !> \brief Compute energy, force and virial from DP.
      73             : !> \param model Pointer to the DP model.
      74             : !> \param natom Number of atoms.
      75             : !> \param coord Coordinates of the atoms.
      76             : !> \param atype Atom types.
      77             : !> \param cell Cell vectors.
      78             : !> \param energy Potential energy.
      79             : !> \param force Forces.
      80             : !> \param virial Virial tensor.
      81             : !> \param atomic_energy Atomic energies.
      82             : !> \param atomic_virial Atomic virial tensors.
      83             : ! **************************************************************************************************
      84           2 :    SUBROUTINE deepmd_model_compute(model, natom, coord, atype, cell, energy, force, virial, &
      85           2 :                                    atomic_energy, atomic_virial)
      86             :       TYPE(deepmd_model_type)                            :: model
      87             :       INTEGER                                            :: natom
      88             :       REAL(kind=dp), DIMENSION(natom, 3), INTENT(IN)     :: coord
      89             :       INTEGER, DIMENSION(natom), INTENT(IN)              :: atype
      90             :       REAL(kind=dp), DIMENSION(9), INTENT(IN)            :: cell
      91             :       REAL(kind=dp), INTENT(OUT)                         :: energy
      92             :       REAL(kind=dp), DIMENSION(natom, 3), INTENT(OUT)    :: force
      93             :       REAL(kind=dp), DIMENSION(9), INTENT(OUT)           :: virial
      94             :       REAL(kind=dp), DIMENSION(natom), INTENT(OUT)       :: atomic_energy
      95             :       REAL(kind=dp), DIMENSION(natom, 9), INTENT(OUT)    :: atomic_virial
      96             : 
      97             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'deepmd_model_compute'
      98             : 
      99             :       INTEGER                                            :: handle
     100             :       INTERFACE
     101             :          SUBROUTINE DeepPotCompute(model, natom, coord, atype, cell, energy, force, virial, &
     102             :                                    atomic_energy, atomic_virial) BIND(C, name="DP_DeepPotCompute")
     103             :             IMPORT :: C_PTR, C_INT, C_DOUBLE
     104             :             TYPE(C_PTR), VALUE                                 :: model
     105             :             INTEGER(C_INT), VALUE                              :: natom
     106             :             REAL(C_DOUBLE), DIMENSION(natom, 3)                :: coord
     107             :             INTEGER(C_INT), DIMENSION(natom)                   :: atype
     108             :             REAL(C_DOUBLE), DIMENSION(9)                       :: cell
     109             :             REAL(C_DOUBLE)                                     :: energy
     110             :             REAL(C_DOUBLE), DIMENSION(natom, 3)                :: force
     111             :             REAL(C_DOUBLE), DIMENSION(9)                       :: virial
     112             :             REAL(C_DOUBLE), DIMENSION(natom)                   :: atomic_energy
     113             :             REAL(C_DOUBLE), DIMENSION(natom, 9)                :: atomic_virial
     114             :          END SUBROUTINE
     115             :       END INTERFACE
     116             : 
     117           2 :       CALL timeset(routineN, handle)
     118             : 
     119             : #if defined(__DEEPMD)
     120             :       CALL DeepPotCompute(model=model%c_ptr, &
     121             :                           natom=natom, &
     122             :                           coord=coord, &
     123             :                           atype=atype, &
     124             :                           cell=cell, &
     125             :                           energy=energy, &
     126             :                           force=force, &
     127             :                           virial=virial, &
     128             :                           atomic_energy=atomic_energy, &
     129           2 :                           atomic_virial=atomic_virial)
     130             : #else
     131             :       CPABORT("CP2K was compiled without libdeepmd_c library.")
     132             :       MARK_USED(model)
     133             :       MARK_USED(natom)
     134             :       MARK_USED(coord)
     135             :       MARK_USED(atype)
     136             :       MARK_USED(cell)
     137             :       energy = 0.0_dp
     138             :       force = 0.0_dp
     139             :       virial = 0.0_dp
     140             :       atomic_energy = 0.0_dp
     141             :       atomic_virial = 0.0_dp
     142             : #endif
     143             : 
     144           2 :       CALL timestop(handle)
     145           2 :    END SUBROUTINE
     146             : 
     147             : ! **************************************************************************************************
     148             : !> \brief Releases a deepmd model and all its ressources.
     149             : !> \param model Pointer to the DP model.
     150             : ! **************************************************************************************************
     151           2 :    SUBROUTINE deepmd_model_release(model)
     152             :       TYPE(deepmd_model_type)                            :: model
     153             : 
     154           2 :       model%c_ptr = C_NULL_PTR
     155           2 :    END SUBROUTINE deepmd_model_release
     156             : 
     157           0 : END MODULE deepmd_wrapper

Generated by: LCOV version 1.15