2020-08-29 08:56:15 +08:00
|
|
|
! -------------------------------------------------------------------------
|
|
|
|
! LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
2021-05-25 04:16:21 +08:00
|
|
|
! https://www.lammps.org/ Sandia National Laboratories
|
2020-08-29 08:56:15 +08:00
|
|
|
! Steve Plimpton, sjplimp@sandia.gov
|
|
|
|
!
|
|
|
|
! Copyright (2003) Sandia Corporation. Under the terms of Contract
|
|
|
|
! DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
|
|
|
|
! certain rights in this software. This software is distributed under
|
|
|
|
! the GNU General Public License.
|
|
|
|
!
|
|
|
|
! See the README file in the top-level LAMMPS directory.
|
|
|
|
! -------------------------------------------------------------------------
|
|
|
|
!
|
|
|
|
! Fortran interface to the LAMMPS library implemented as a Fortran 2003
|
|
|
|
! style module that wraps the C-style library interface in library.cpp
|
|
|
|
! and library.h using the ISO_C_BINDING module of the Fortran compiler.
|
|
|
|
!
|
|
|
|
! Based on the LAMMPS Fortran 2003 module contributed by:
|
2020-10-02 22:36:17 +08:00
|
|
|
! Karl D. Hammond <hammondkd@missouri.edu>
|
|
|
|
! University of Missouri, 2012-2020
|
2020-08-29 08:56:15 +08:00
|
|
|
!
|
|
|
|
! The Fortran module tries to follow the API of the C-library interface
|
|
|
|
! closely, but like the Python wrapper it employs an object oriented
|
|
|
|
! approach. To accommodate the object oriented approach, all exported
|
|
|
|
! subroutine and functions have to be implemented in Fortran to then
|
|
|
|
! call the interfaced C style functions with adapted calling conventions
|
|
|
|
! as needed. The C-library interfaced functions retain their names
|
|
|
|
! starting with "lammps_" while the Fortran versions start with "lmp_".
|
|
|
|
!
|
|
|
|
MODULE LIBLAMMPS
|
|
|
|
|
|
|
|
USE, INTRINSIC :: ISO_C_BINDING, ONLY: c_ptr, c_null_ptr, c_loc, &
|
2020-10-02 22:36:17 +08:00
|
|
|
c_int, c_char, c_null_char, c_double, c_size_t, c_f_pointer
|
2020-08-29 08:56:15 +08:00
|
|
|
|
|
|
|
IMPLICIT NONE
|
|
|
|
PRIVATE
|
|
|
|
PUBLIC :: lammps
|
|
|
|
|
|
|
|
TYPE lammps
|
|
|
|
TYPE(c_ptr) :: handle
|
|
|
|
CONTAINS
|
|
|
|
PROCEDURE :: close => lmp_close
|
|
|
|
PROCEDURE :: file => lmp_file
|
|
|
|
PROCEDURE :: command => lmp_command
|
|
|
|
PROCEDURE :: commands_list => lmp_commands_list
|
|
|
|
PROCEDURE :: commands_string => lmp_commands_string
|
|
|
|
PROCEDURE :: version => lmp_version
|
|
|
|
PROCEDURE :: get_natoms => lmp_get_natoms
|
|
|
|
END TYPE lammps
|
|
|
|
|
|
|
|
INTERFACE lammps
|
|
|
|
MODULE PROCEDURE lmp_open
|
|
|
|
END INTERFACE lammps
|
|
|
|
|
|
|
|
! interface definitions for calling functions in library.cpp
|
|
|
|
INTERFACE
|
2022-08-07 05:43:40 +08:00
|
|
|
FUNCTION lammps_open(argc, argv, comm) BIND(C, name='lammps_open_fortran')
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPORT :: c_ptr, c_int
|
|
|
|
INTEGER(c_int), VALUE, INTENT(in) :: argc, comm
|
|
|
|
TYPE(c_ptr), DIMENSION(*), INTENT(in) :: argv
|
|
|
|
TYPE(c_ptr) :: lammps_open
|
|
|
|
END FUNCTION lammps_open
|
|
|
|
|
2022-08-07 05:43:40 +08:00
|
|
|
FUNCTION lammps_open_no_mpi(argc, argv, handle) BIND(C, name='lammps_open_no_mpi')
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPORT :: c_ptr, c_int
|
|
|
|
INTEGER(c_int), VALUE, INTENT(in) :: argc
|
|
|
|
TYPE(c_ptr), DIMENSION(*), INTENT(in) :: argv
|
2022-08-07 05:43:40 +08:00
|
|
|
TYPE(c_ptr), VALUE :: handle
|
2020-08-29 08:56:15 +08:00
|
|
|
TYPE(c_ptr) :: lammps_open_no_mpi
|
|
|
|
END FUNCTION lammps_open_no_mpi
|
|
|
|
|
|
|
|
SUBROUTINE lammps_close(handle) BIND(C, name='lammps_close')
|
|
|
|
IMPORT :: c_ptr
|
|
|
|
TYPE(c_ptr), VALUE :: handle
|
|
|
|
END SUBROUTINE lammps_close
|
|
|
|
|
2021-06-28 05:07:42 +08:00
|
|
|
SUBROUTINE lammps_mpi_init() BIND(C, name='lammps_mpi_init')
|
2020-08-29 08:56:15 +08:00
|
|
|
END SUBROUTINE lammps_mpi_init
|
|
|
|
|
2021-06-28 05:07:42 +08:00
|
|
|
SUBROUTINE lammps_mpi_finalize() BIND(C, name='lammps_mpi_finalize')
|
2020-08-29 08:56:15 +08:00
|
|
|
END SUBROUTINE lammps_mpi_finalize
|
|
|
|
|
2021-06-28 05:07:42 +08:00
|
|
|
SUBROUTINE lammps_kokkos_finalize() BIND(C, name='lammps_kokkos_finalize')
|
|
|
|
END SUBROUTINE lammps_kokkos_finalize
|
|
|
|
|
2022-08-07 05:43:40 +08:00
|
|
|
SUBROUTINE lammps_file(handle, filename) BIND(C, name='lammps_file')
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPORT :: c_ptr
|
|
|
|
TYPE(c_ptr), VALUE :: handle
|
|
|
|
TYPE(c_ptr), VALUE :: filename
|
|
|
|
END SUBROUTINE lammps_file
|
|
|
|
|
2022-08-07 05:43:40 +08:00
|
|
|
SUBROUTINE lammps_command(handle, cmd) BIND(C, name='lammps_command')
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPORT :: c_ptr
|
|
|
|
TYPE(c_ptr), VALUE :: handle
|
|
|
|
TYPE(c_ptr), VALUE :: cmd
|
|
|
|
END SUBROUTINE lammps_command
|
|
|
|
|
2022-08-07 05:43:40 +08:00
|
|
|
SUBROUTINE lammps_commands_list(handle, ncmd, cmds) BIND(C, name='lammps_commands_list')
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPORT :: c_ptr, c_int
|
|
|
|
TYPE(c_ptr), VALUE :: handle
|
|
|
|
INTEGER(c_int), VALUE, INTENT(in) :: ncmd
|
|
|
|
TYPE(c_ptr), DIMENSION(*), INTENT(in) :: cmds
|
|
|
|
END SUBROUTINE lammps_commands_list
|
|
|
|
|
2022-08-07 05:43:40 +08:00
|
|
|
SUBROUTINE lammps_commands_string(handle, str) BIND(C, name='lammps_commands_string')
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPORT :: c_ptr
|
|
|
|
TYPE(c_ptr), VALUE :: handle
|
|
|
|
TYPE(c_ptr), VALUE :: str
|
|
|
|
END SUBROUTINE lammps_commands_string
|
|
|
|
|
2020-10-02 22:36:17 +08:00
|
|
|
FUNCTION lammps_malloc(size) BIND(C, name='malloc')
|
|
|
|
IMPORT :: c_ptr, c_size_t
|
|
|
|
INTEGER(c_size_t), value :: size
|
|
|
|
TYPE(c_ptr) :: lammps_malloc
|
|
|
|
END FUNCTION lammps_malloc
|
|
|
|
|
2020-08-29 08:56:15 +08:00
|
|
|
SUBROUTINE lammps_free(ptr) BIND(C, name='lammps_free')
|
|
|
|
IMPORT :: c_ptr
|
|
|
|
TYPE(c_ptr), VALUE :: ptr
|
|
|
|
END SUBROUTINE lammps_free
|
|
|
|
|
|
|
|
FUNCTION lammps_version(handle) BIND(C, name='lammps_version')
|
|
|
|
IMPORT :: c_ptr, c_int
|
|
|
|
TYPE(c_ptr), VALUE :: handle
|
|
|
|
INTEGER(c_int) :: lammps_version
|
|
|
|
END FUNCTION lammps_version
|
|
|
|
|
|
|
|
FUNCTION lammps_get_natoms(handle) BIND(C, name='lammps_get_natoms')
|
|
|
|
IMPORT :: c_ptr, c_double
|
|
|
|
TYPE(c_ptr), VALUE :: handle
|
|
|
|
REAL(c_double) :: lammps_get_natoms
|
|
|
|
END FUNCTION lammps_get_natoms
|
|
|
|
END INTERFACE
|
|
|
|
|
|
|
|
CONTAINS
|
|
|
|
! Fortran wrappers and helper functions.
|
|
|
|
|
|
|
|
! Constructor for the LAMMPS class.
|
|
|
|
! Combined wrapper around lammps_open_fortran() and lammps_open_no_mpi()
|
2022-08-07 05:43:40 +08:00
|
|
|
TYPE(lammps) FUNCTION lmp_open(args, comm)
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPLICIT NONE
|
2022-08-07 05:43:40 +08:00
|
|
|
INTEGER, INTENT(in), OPTIONAL :: comm
|
2020-08-29 08:56:15 +08:00
|
|
|
CHARACTER(len=*), INTENT(in), OPTIONAL :: args(:)
|
|
|
|
TYPE(c_ptr), ALLOCATABLE :: argv(:)
|
|
|
|
TYPE(c_ptr) :: dummy=c_null_ptr
|
2022-08-07 05:43:40 +08:00
|
|
|
INTEGER(c_int) :: i, c_comm, argc
|
2020-08-29 08:56:15 +08:00
|
|
|
|
|
|
|
IF (PRESENT(args)) THEN
|
2022-08-07 05:43:40 +08:00
|
|
|
! convert fortran argument list to c style
|
2020-08-29 08:56:15 +08:00
|
|
|
argc = SIZE(args)
|
|
|
|
ALLOCATE(argv(argc))
|
2022-08-07 05:43:40 +08:00
|
|
|
DO i=1, argc
|
2020-08-29 08:56:15 +08:00
|
|
|
argv(i) = f2c_string(args(i))
|
|
|
|
END DO
|
|
|
|
ELSE
|
|
|
|
argc = 1
|
|
|
|
ALLOCATE(argv(1))
|
|
|
|
argv(1) = f2c_string("liblammps")
|
|
|
|
ENDIF
|
|
|
|
|
|
|
|
IF (PRESENT(comm)) THEN
|
2022-08-07 05:43:40 +08:00
|
|
|
c_comm = comm
|
|
|
|
lmp_open%handle = lammps_open(argc, argv, c_comm)
|
2020-08-29 08:56:15 +08:00
|
|
|
ELSE
|
2022-08-07 05:43:40 +08:00
|
|
|
lmp_open%handle = lammps_open_no_mpi(argc, argv, dummy)
|
2020-08-29 08:56:15 +08:00
|
|
|
END IF
|
|
|
|
|
|
|
|
! Clean up allocated memory
|
2022-08-07 05:43:40 +08:00
|
|
|
DO i=1, argc
|
2020-08-29 08:56:15 +08:00
|
|
|
CALL lammps_free(argv(i))
|
|
|
|
END DO
|
|
|
|
DEALLOCATE(argv)
|
|
|
|
END FUNCTION lmp_open
|
|
|
|
|
|
|
|
! Combined Fortran wrapper around lammps_close() and lammps_mpi_finalize()
|
2022-08-07 05:43:40 +08:00
|
|
|
SUBROUTINE lmp_close(self, finalize)
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPLICIT NONE
|
|
|
|
CLASS(lammps) :: self
|
2022-08-07 05:43:40 +08:00
|
|
|
LOGICAL, INTENT(in), OPTIONAL :: finalize
|
2020-08-29 08:56:15 +08:00
|
|
|
|
|
|
|
CALL lammps_close(self%handle)
|
|
|
|
|
|
|
|
IF (PRESENT(finalize)) THEN
|
|
|
|
IF (finalize) THEN
|
2021-06-28 05:07:42 +08:00
|
|
|
CALL lammps_kokkos_finalize()
|
|
|
|
CALL lammps_mpi_finalize()
|
2020-08-29 08:56:15 +08:00
|
|
|
END IF
|
|
|
|
END IF
|
|
|
|
END SUBROUTINE lmp_close
|
|
|
|
|
|
|
|
INTEGER FUNCTION lmp_version(self)
|
|
|
|
IMPLICIT NONE
|
|
|
|
CLASS(lammps) :: self
|
|
|
|
|
|
|
|
lmp_version = lammps_version(self%handle)
|
|
|
|
END FUNCTION lmp_version
|
|
|
|
|
|
|
|
DOUBLE PRECISION FUNCTION lmp_get_natoms(self)
|
|
|
|
IMPLICIT NONE
|
|
|
|
CLASS(lammps) :: self
|
|
|
|
|
|
|
|
lmp_get_natoms = lammps_get_natoms(self%handle)
|
|
|
|
END FUNCTION lmp_get_natoms
|
|
|
|
|
2022-08-07 05:43:40 +08:00
|
|
|
SUBROUTINE lmp_file(self, filename)
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPLICIT NONE
|
|
|
|
CLASS(lammps) :: self
|
|
|
|
CHARACTER(len=*) :: filename
|
|
|
|
TYPE(c_ptr) :: str
|
|
|
|
|
|
|
|
str = f2c_string(filename)
|
2022-08-07 05:43:40 +08:00
|
|
|
CALL lammps_file(self%handle, str)
|
2020-08-29 08:56:15 +08:00
|
|
|
CALL lammps_free(str)
|
|
|
|
END SUBROUTINE lmp_file
|
|
|
|
|
|
|
|
! equivalent function to lammps_command()
|
2022-08-07 05:43:40 +08:00
|
|
|
SUBROUTINE lmp_command(self, cmd)
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPLICIT NONE
|
|
|
|
CLASS(lammps) :: self
|
|
|
|
CHARACTER(len=*) :: cmd
|
|
|
|
TYPE(c_ptr) :: str
|
|
|
|
|
|
|
|
str = f2c_string(cmd)
|
2022-08-07 05:43:40 +08:00
|
|
|
CALL lammps_command(self%handle, str)
|
2020-08-29 08:56:15 +08:00
|
|
|
CALL lammps_free(str)
|
|
|
|
END SUBROUTINE lmp_command
|
|
|
|
|
|
|
|
! equivalent function to lammps_commands_list()
|
2022-08-07 05:43:40 +08:00
|
|
|
SUBROUTINE lmp_commands_list(self, cmds)
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPLICIT NONE
|
|
|
|
CLASS(lammps) :: self
|
|
|
|
CHARACTER(len=*), INTENT(in), OPTIONAL :: cmds(:)
|
|
|
|
TYPE(c_ptr), ALLOCATABLE :: cmdv(:)
|
2022-08-07 05:43:40 +08:00
|
|
|
INTEGER :: i, ncmd
|
2020-08-29 08:56:15 +08:00
|
|
|
|
|
|
|
! convert command list to c style
|
|
|
|
ncmd = SIZE(cmds)
|
|
|
|
ALLOCATE(cmdv(ncmd))
|
2022-08-07 05:43:40 +08:00
|
|
|
DO i=1, ncmd
|
2020-08-29 08:56:15 +08:00
|
|
|
cmdv(i) = f2c_string(cmds(i))
|
|
|
|
END DO
|
|
|
|
|
2022-08-07 05:43:40 +08:00
|
|
|
CALL lammps_commands_list(self%handle, ncmd, cmdv)
|
2020-08-29 08:56:15 +08:00
|
|
|
|
|
|
|
! Clean up allocated memory
|
2022-08-07 05:43:40 +08:00
|
|
|
DO i=1, ncmd
|
2020-08-29 08:56:15 +08:00
|
|
|
CALL lammps_free(cmdv(i))
|
|
|
|
END DO
|
|
|
|
DEALLOCATE(cmdv)
|
|
|
|
END SUBROUTINE lmp_commands_list
|
|
|
|
|
|
|
|
! equivalent function to lammps_commands_string()
|
2022-08-07 05:43:40 +08:00
|
|
|
SUBROUTINE lmp_commands_string(self, str)
|
2020-08-29 08:56:15 +08:00
|
|
|
IMPLICIT NONE
|
|
|
|
CLASS(lammps) :: self
|
|
|
|
CHARACTER(len=*) :: str
|
|
|
|
TYPE(c_ptr) :: tmp
|
|
|
|
|
|
|
|
tmp = f2c_string(str)
|
2022-08-07 05:43:40 +08:00
|
|
|
CALL lammps_commands_string(self%handle, tmp)
|
2020-08-29 08:56:15 +08:00
|
|
|
CALL lammps_free(tmp)
|
|
|
|
END SUBROUTINE lmp_commands_string
|
|
|
|
|
|
|
|
! ----------------------------------------------------------------------
|
|
|
|
! local helper functions
|
|
|
|
! copy fortran string to zero terminated c string
|
2022-08-07 05:43:40 +08:00
|
|
|
! ----------------------------------------------------------------------
|
2020-08-29 08:56:15 +08:00
|
|
|
FUNCTION f2c_string(f_string) RESULT(ptr)
|
|
|
|
CHARACTER (len=*), INTENT(in) :: f_string
|
|
|
|
CHARACTER (len=1, kind=c_char), POINTER :: c_string(:)
|
|
|
|
TYPE(c_ptr) :: ptr
|
2020-10-02 22:36:17 +08:00
|
|
|
INTEGER(c_size_t) :: i, n
|
2020-08-29 08:56:15 +08:00
|
|
|
|
|
|
|
n = LEN_TRIM(f_string)
|
2020-10-02 22:36:17 +08:00
|
|
|
ptr = lammps_malloc(n+1)
|
2022-08-07 05:43:40 +08:00
|
|
|
CALL C_F_POINTER(ptr, c_string, [1])
|
|
|
|
DO i=1, n
|
2020-08-29 08:56:15 +08:00
|
|
|
c_string(i) = f_string(i:i)
|
|
|
|
END DO
|
|
|
|
c_string(n+1) = c_null_char
|
|
|
|
END FUNCTION f2c_string
|
|
|
|
END MODULE LIBLAMMPS
|