Skip to content

Commit

Permalink
Slight refactoring of deepmd_wrapper.F
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Jan 8, 2024
1 parent e2aab9b commit 96ef312
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 156 deletions.
152 changes: 82 additions & 70 deletions src/deepmd_wrapper.F
Original file line number Diff line number Diff line change
Expand Up @@ -15,80 +15,62 @@
! **************************************************************************************************

MODULE deepmd_wrapper

USE ISO_C_BINDING, ONLY: C_CHAR,&
C_DOUBLE,&
C_INT,&
C_LOC,&
C_NULL_CHAR,&
C_NULL_PTR,&
C_PTR
USE kinds, ONLY: dp
#include "./base/base_uses.f90"

IMPLICIT NONE
PRIVATE
PUBLIC :: dp_deep_pot, dp_deep_pot_compute, deepmd_model_release

#if(__DEEPMD)
INTERFACE
FUNCTION dp_deep_pot_c(c_model) BIND(C, name="DP_NewDeepPot")
USE ISO_C_BINDING, ONLY: C_PTR, &
C_CHAR, &
C_DOUBLE, &
C_INT, &
C_NULL_CHAR, &
C_LOC
CHARACTER(LEN=1, KIND=C_CHAR) :: c_model(*)
TYPE(C_PTR) :: dp_deep_pot_c

END FUNCTION

SUBROUTINE dp_deep_pot_compute_c(dp, natom, &
coord, atype, cell, &
energy, force, virial, &
atomic_energy, atomic_virial) BIND(C, name="DP_DeepPotCompute")
USE ISO_C_BINDING, ONLY: C_PTR, &
C_INT, &
C_CHAR, &
C_DOUBLE
TYPE(C_PTR), VALUE :: dp
INTEGER(C_INT), VALUE :: natom
REAL(C_DOUBLE), DIMENSION(natom, 3), INTENT(IN) :: coord
INTEGER(C_INT), DIMENSION(natom), INTENT(IN) :: atype
REAL(C_DOUBLE), DIMENSION(9), INTENT(IN) :: cell
REAL(C_DOUBLE), INTENT(OUT) :: energy
REAL(C_DOUBLE), DIMENSION(natom, 3), INTENT(OUT) :: force
REAL(C_DOUBLE), DIMENSION(9), INTENT(OUT) :: virial
REAL(C_DOUBLE), DIMENSION(natom), INTENT(OUT) :: atomic_energy
REAL(C_DOUBLE), DIMENSION(natom, 9), INTENT(OUT) :: atomic_virial

END SUBROUTINE
END INTERFACE
#endif
PUBLIC :: deepmd_model_type, deepmd_model_load, deepmd_model_compute, deepmd_model_release

TYPE deepmd_model_type
PRIVATE
TYPE(C_PTR) :: c_ptr = C_NULL_PTR
END TYPE deepmd_model_type

CONTAINS

! **************************************************************************************************
!> \brief Load DP from a model file.
!> \param model Path to the model file.
!> \param filename Path to the model file.
!> \return Pointer to the DP model.
! **************************************************************************************************
FUNCTION deepmd_model_load(filename) RESULT(model)
CHARACTER(len=*), INTENT(INOUT) :: filename
TYPE(deepmd_model_type) :: model

FUNCTION dp_deep_pot(model) RESULT(pot)
CHARACTER(len=*), INTENT(INOUT) :: model
TYPE(C_PTR) :: pot
CHARACTER(LEN=*), PARAMETER :: routineN = 'deepmd_model_load'

#if(__DEEPMD)
pot = dp_deep_pot_c(c_model=TRIM(model)//C_NULL_CHAR)
INTEGER :: handle
INTERFACE
FUNCTION NewDeepPot(filename) BIND(C, name="DP_NewDeepPot")
IMPORT :: C_PTR, C_CHAR
CHARACTER(kind=C_CHAR), DIMENSION(*) :: filename
TYPE(C_PTR) :: NewDeepPot
END FUNCTION
END INTERFACE

CALL timeset(routineN, handle)

#if defined(__DEEPMD)
model%c_ptr = NewDeepPot(filename=TRIM(filename)//C_NULL_CHAR)
#else
CPABORT("CP2K was compiled without libdeepmd_c library.")
MARK_USED(filename)
MARK_USED(model)
pot = C_NULL_PTR
#endif
END FUNCTION dp_deep_pot

CALL timestop(handle)
END FUNCTION deepmd_model_load

! **************************************************************************************************
!> \brief Compute energy, force and virial from DP.
!> \param dp Pointer to the DP model.
!> \param model Pointer to the DP model.
!> \param natom Number of atoms.
!> \param coord Coordinates of the atoms.
!> \param atype Atom types.
Expand All @@ -99,25 +81,55 @@ END FUNCTION dp_deep_pot
!> \param atomic_energy Atomic energies.
!> \param atomic_virial Atomic virial tensors.
! **************************************************************************************************

SUBROUTINE dp_deep_pot_compute(dp, natom, coord, atype, cell, energy, force, virial, atomic_energy, atomic_virial)
USE, INTRINSIC :: ISO_C_BINDING
TYPE(C_PTR), VALUE :: dp
INTEGER(C_INT), VALUE :: natom
REAL(C_DOUBLE), DIMENSION(natom, 3), INTENT(IN) :: coord
INTEGER(C_INT), DIMENSION(natom), INTENT(IN) :: atype
REAL(C_DOUBLE), DIMENSION(9), INTENT(IN), OPTIONAL :: cell
REAL(C_DOUBLE), INTENT(OUT), OPTIONAL :: energy
REAL(C_DOUBLE), DIMENSION(natom, 3), INTENT(OUT), OPTIONAL :: force
REAL(C_DOUBLE), DIMENSION(9), INTENT(OUT), OPTIONAL :: virial
REAL(C_DOUBLE), DIMENSION(natom), INTENT(OUT), OPTIONAL :: atomic_energy
REAL(C_DOUBLE), DIMENSION(natom, 9), INTENT(OUT), OPTIONAL :: atomic_virial

#if(__DEEPMD)
CALL dp_deep_pot_compute_c(dp, natom, coord, atype, cell, energy, force, virial, atomic_energy, atomic_virial)
SUBROUTINE deepmd_model_compute(model, natom, coord, atype, cell, energy, force, virial, &
atomic_energy, atomic_virial)
TYPE(deepmd_model_type) :: model
INTEGER :: natom
REAL(kind=dp), DIMENSION(natom, 3), INTENT(IN) :: coord
INTEGER, DIMENSION(natom), INTENT(IN) :: atype
REAL(kind=dp), DIMENSION(9), INTENT(IN) :: cell
REAL(kind=dp), INTENT(OUT) :: energy
REAL(kind=dp), DIMENSION(natom, 3), INTENT(OUT) :: force
REAL(kind=dp), DIMENSION(9), INTENT(OUT) :: virial
REAL(kind=dp), DIMENSION(natom), INTENT(OUT) :: atomic_energy
REAL(kind=dp), DIMENSION(natom, 9), INTENT(OUT) :: atomic_virial

CHARACTER(LEN=*), PARAMETER :: routineN = 'deepmd_model_compute'

INTEGER :: handle
INTERFACE
SUBROUTINE DeepPotCompute(model, natom, coord, atype, cell, energy, force, virial, &
atomic_energy, atomic_virial) BIND(C, name="DP_DeepPotCompute")
IMPORT :: C_PTR, C_INT, C_DOUBLE
TYPE(C_PTR), VALUE :: model
INTEGER(C_INT), VALUE :: natom
REAL(C_DOUBLE), DIMENSION(natom, 3) :: coord
INTEGER(C_INT), DIMENSION(natom) :: atype
REAL(C_DOUBLE), DIMENSION(9) :: cell
REAL(C_DOUBLE) :: energy
REAL(C_DOUBLE), DIMENSION(natom, 3) :: force
REAL(C_DOUBLE), DIMENSION(9) :: virial
REAL(C_DOUBLE), DIMENSION(natom) :: atomic_energy
REAL(C_DOUBLE), DIMENSION(natom, 9) :: atomic_virial
END SUBROUTINE
END INTERFACE

CALL timeset(routineN, handle)

#if defined(__DEEPMD)
CALL DeepPotCompute(model=model%c_ptr, &
natom=natom, &
coord=coord, &
atype=atype, &
cell=cell, &
energy=energy, &
force=force, &
virial=virial, &
atomic_energy=atomic_energy, &
atomic_virial=atomic_virial)
#else
CPABORT("CP2K was compiled without libdeepmd_c library.")
MARK_USED(dp)
MARK_USED(model)
MARK_USED(natom)
MARK_USED(coord)
MARK_USED(atype)
Expand All @@ -128,18 +140,18 @@ SUBROUTINE dp_deep_pot_compute(dp, natom, coord, atype, cell, energy, force, vir
MARK_USED(atomic_energy)
MARK_USED(atomic_virial)
#endif

CALL timestop(handle)
END SUBROUTINE

! **************************************************************************************************
!> \brief Releases a deepmd model and all its ressources.
!> \param model Pointer to the DP model.
! **************************************************************************************************
SUBROUTINE deepmd_model_release(model)
USE ISO_C_BINDING, ONLY: C_PTR, &
C_NULL_PTR
TYPE(C_PTR), INTENT(INOUT) :: model
TYPE(deepmd_model_type) :: model

model = C_NULL_PTR
model%c_ptr = C_NULL_PTR
END SUBROUTINE deepmd_model_release

END MODULE deepmd_wrapper
6 changes: 3 additions & 3 deletions src/fist_nonbond_env_types.F
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
!> \author HAF
! **************************************************************************************************
MODULE fist_nonbond_env_types
USE ISO_C_BINDING, ONLY: C_PTR
USE atomic_kind_types, ONLY: atomic_kind_type
USE cell_types, ONLY: cell_release,&
cell_type
USE deepmd_wrapper, ONLY: deepmd_model_release
USE deepmd_wrapper, ONLY: deepmd_model_release,&
deepmd_model_type
USE fist_neighbor_list_types, ONLY: fist_neighbor_deallocate,&
fist_neighbor_type
USE kinds, ONLY: default_string_length,&
Expand Down Expand Up @@ -74,7 +74,7 @@ MODULE fist_nonbond_env_types
INTEGER, POINTER :: use_indices(:) => NULL()
REAL(KIND=dp), POINTER :: force(:, :) => NULL()
REAL(KIND=dp) :: virial(3, 3) = 0.0_dp
TYPE(C_PTR) :: model
TYPE(deepmd_model_type) :: model
END TYPE

! **************************************************************************************************
Expand Down
Loading

0 comments on commit 96ef312

Please sign in to comment.