Skip to content

Commit

Permalink
Merge pull request lammps#4380 from rbberger/python_computes
Browse files Browse the repository at this point in the history
Python and library API changes for computes
  • Loading branch information
akohlmey authored Jan 30, 2025
2 parents 75b33ac + 2b0a641 commit 4889323
Show file tree
Hide file tree
Showing 14 changed files with 426 additions and 1 deletion.
59 changes: 58 additions & 1 deletion doc/src/Fortran.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ of the contents of the :f:mod:`LIBLAMMPS` Fortran interface to LAMMPS.
:ftype set_internal_variable: subroutine
:f eval: :f:func:`eval`
:ftype eval: function
:f clearstep_compute: :f:subr:`clearstep_compute`
:ftype clearstep_compute: subroutine
:f addstep_compute: :f:subr:`addstep_compute`
:ftype addstep_compute: subroutine
:f addstep_compute_all: :f:subr:`addstep_compute_all`
:ftype addstep_compute_all: subroutine
:f gather_atoms: :f:subr:`gather_atoms`
:ftype gather_atoms: subroutine
:f gather_atoms_concat: :f:subr:`gather_atoms_concat`
Expand Down Expand Up @@ -1453,11 +1459,62 @@ Procedures Bound to the :f:type:`lammps` Derived Type
an internal-style variable, an error is generated.

:p character(len=*) name: name of the variable
:p read(c_double) val: new value to assign to the variable
:p real(c_double) val: new value to assign to the variable
:to: :cpp:func:`lammps_set_internal_variable`

--------

.. f:function:: eval(expr)
This function is a wrapper around :cpp:func:`lammps_eval` that takes a
LAMMPS equal style variable string, evaluates it and returns the resulting
scalar value as a floating-point number.

.. versionadded:: TBD

:p character(len=\*) expr: string to be evaluated
:to: :cpp:func:`lammps_eval`
:r value [real(c_double)]: result of the evaluated string

--------

.. f:subroutine:: clearstep_compute()
Clear whether a compute has been invoked

.. versionadded:: TBD

:to: :cpp:func:`lammps_clearstep_compute`

--------

.. f:subroutine:: addstep_compute(nextstep)
Add timestep to list of future compute invocations
if the compute has been invoked on the current timestep

.. versionadded:: TBD

overloaded for 32-bit and 64-bit integer arguments

:p integer(kind=8 or kind=4) nextstep: next timestep
:to: :cpp:func:`lammps_addstep_compute`

--------

.. f:subroutine:: addstep_compute_all(nextstep)
Add timestep to list of future compute invocations

.. versionadded:: TBD

overloaded for 32-bit and 64-bit integer arguments

:p integer(kind=8 or kind=4) nextstep: next timestep
:to: :cpp:func:`lammps_addstep_compute_all`

--------

.. f:subroutine:: gather_atoms(name, count, data)
This function calls :cpp:func:`lammps_gather_atoms` to gather the named
Expand Down
18 changes: 18 additions & 0 deletions doc/src/Library_objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ fixes, or variables in LAMMPS using the following functions:
- :cpp:func:`lammps_set_internal_variable`
- :cpp:func:`lammps_variable_info`
- :cpp:func:`lammps_eval`
- :cpp:func:`lammps_clearstep_compute`
- :cpp:func:`lammps_addstep_compute_all`
- :cpp:func:`lammps_addstep_compute`

-----------------------

Expand Down Expand Up @@ -61,6 +64,21 @@ fixes, or variables in LAMMPS using the following functions:

-----------------------

.. doxygenfunction:: lammps_clearstep_compute
:project: progguide

-----------------------

.. doxygenfunction:: lammps_addstep_compute_all
:project: progguide

-----------------------

.. doxygenfunction:: lammps_addstep_compute
:project: progguide

-----------------------

.. doxygenenum:: _LMP_DATATYPE_CONST

.. doxygenenum:: _LMP_STYLE_CONST
Expand Down
9 changes: 9 additions & 0 deletions doc/src/fix_python_invoke.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ gives access to the LAMMPS state from Python.
from these callbacks, trying to execute input script commands will in the best
case not work or in the worst case result in undefined behavior.

Restart, fix_modify, output, run start/stop, minimize info
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

No information about this fix is written to :doc:`binary restart files <restart>`. None of the :doc:`fix_modify <fix_modify>` options
are relevant to this fix. No global or per-atom quantities are stored
by this fix for access by various :doc:`output commands <Howto_output>`.
No parameter of this fix can be used with the *start/stop* keywords of
the :doc:`run <run>` command. This fix is not invoked during :doc:`energy minimization <minimize>`.

Restrictions
""""""""""""

Expand Down
2 changes: 2 additions & 0 deletions doc/utils/sphinx-config/false_positives.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,7 @@ Nevery
Nevins
newfile
Newns
newstep
newtype
nextsort
Neyts
Expand Down Expand Up @@ -3773,6 +3774,7 @@ Tigran
Tij
Tildesley
Timan
timeflag
timeI
timespan
timestamp
Expand Down
3 changes: 3 additions & 0 deletions examples/COUPLE/plugin/liblammpsplugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib)
ADDSYM(set_internal_variable);
ADDSYM(variable_info);
ADDSYM(eval);
ADDSYM(clearstep_compute);
ADDSYM(addstep_compute);
ADDSYM(addstep_compute_all);

ADDSYM(gather_atoms);
ADDSYM(gather_atoms_concat);
Expand Down
3 changes: 3 additions & 0 deletions examples/COUPLE/plugin/liblammpsplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ struct _liblammpsplugin {
int (*set_internal_variable)(void *, const char *, double);
int (*variable_info)(void *, int, char *, int);
double (*eval)(void *, const char *);
void (*clearstep_compute)(void *);
void (*addstep_compute)(void *, void *);
void (*addstep_compute_all)(void *, void *);

void (*gather_atoms)(void *, const char *, int, int, void *);
void (*gather_atoms_concat)(void *, const char *, int, int, void *);
Expand Down
102 changes: 102 additions & 0 deletions fortran/lammps.f90
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ MODULE LIBLAMMPS
PROCEDURE :: set_string_variable => lmp_set_string_variable
PROCEDURE :: set_internal_variable => lmp_set_internal_variable
PROCEDURE :: eval => lmp_eval

PROCEDURE :: clearstep_compute => lmp_clearstep_compute
PROCEDURE, PRIVATE :: lmp_addstep_compute_smallint
PROCEDURE, PRIVATE :: lmp_addstep_compute_bigint
GENERIC :: addstep_compute => lmp_addstep_compute_smallint, lmp_addstep_compute_bigint
PROCEDURE, PRIVATE :: lmp_addstep_compute_all_smallint
PROCEDURE, PRIVATE :: lmp_addstep_compute_all_bigint
GENERIC :: addstep_compute_all => lmp_addstep_compute_all_smallint, &
lmp_addstep_compute_all_bigint

PROCEDURE, PRIVATE :: lmp_gather_atoms_int
PROCEDURE, PRIVATE :: lmp_gather_atoms_double
GENERIC :: gather_atoms => lmp_gather_atoms_int, &
Expand Down Expand Up @@ -626,6 +636,24 @@ FUNCTION lammps_eval(handle, expr) BIND(C)
REAL(c_double) :: lammps_eval
END FUNCTION lammps_eval

SUBROUTINE lammps_clearstep_compute(handle) BIND(C)
IMPORT :: c_ptr
IMPLICIT NONE
TYPE(c_ptr), VALUE :: handle
END SUBROUTINE lammps_clearstep_compute

SUBROUTINE lammps_addstep_compute(handle, step) BIND(C)
IMPORT :: c_ptr
IMPLICIT NONE
TYPE(c_ptr), VALUE :: handle, step
END SUBROUTINE lammps_addstep_compute

SUBROUTINE lammps_addstep_compute_all(handle, step) BIND(C)
IMPORT :: c_ptr
IMPLICIT NONE
TYPE(c_ptr), VALUE :: handle, step
END SUBROUTINE lammps_addstep_compute_all

SUBROUTINE lammps_gather_atoms(handle, name, TYPE, count, DATA) BIND(C)
IMPORT :: c_int, c_ptr
IMPLICIT NONE
Expand Down Expand Up @@ -1846,6 +1874,80 @@ FUNCTION lmp_eval(self, expr)
CALL lammps_free(Cexpr)
END FUNCTION lmp_eval

! equivalent subroutine to lammps_clearstep_compute
SUBROUTINE lmp_clearstep_compute(self)
CLASS(lammps), INTENT(IN) :: self
CALL lammps_clearstep_compute(self%handle)
END SUBROUTINE lmp_clearstep_compute

! equivalent subroutine to lammps_addstep_compute
SUBROUTINE lmp_addstep_compute_bigint(self, nextstep)
CLASS(lammps), INTENT(IN) :: self
INTEGER(kind=8), INTENT(IN) :: nextstep
INTEGER(c_int), TARGET :: smallstep
INTEGER(c_int64_t), TARGET :: bigstep
TYPE(c_ptr) :: ptrstep
IF (SIZE_BIGINT == 4_c_int) THEN
smallstep = INT(nextstep,kind=c_int)
ptrstep = C_LOC(smallstep)
ELSE
bigstep = nextstep
ptrstep = C_LOC(bigstep)
END IF
CALL lammps_addstep_compute(self%handle, ptrstep)
END SUBROUTINE lmp_addstep_compute_bigint

! equivalent subroutine to lammps_addstep_compute
SUBROUTINE lmp_addstep_compute_smallint(self, nextstep)
CLASS(lammps), INTENT(IN) :: self
INTEGER(kind=4), INTENT(IN) :: nextstep
INTEGER(c_int), TARGET :: smallstep
INTEGER(c_int64_t), TARGET :: bigstep
TYPE(c_ptr) :: ptrstep
IF (SIZE_BIGINT == 4_c_int) THEN
smallstep = nextstep
ptrstep = C_LOC(smallstep)
ELSE
bigstep = nextstep
ptrstep = C_LOC(bigstep)
END IF
CALL lammps_addstep_compute(self%handle, ptrstep)
END SUBROUTINE lmp_addstep_compute_smallint

! equivalent subroutine to lammps_addstep_compute_all
SUBROUTINE lmp_addstep_compute_all_bigint(self, nextstep)
CLASS(lammps), INTENT(IN) :: self
INTEGER(kind=8), INTENT(IN) :: nextstep
INTEGER(c_int), TARGET :: smallstep
INTEGER(c_int64_t), TARGET :: bigstep
TYPE(c_ptr) :: ptrstep
IF (SIZE_BIGINT == 4_c_int) THEN
smallstep = INT(nextstep,kind=c_int)
ptrstep = C_LOC(smallstep)
ELSE
bigstep = nextstep
ptrstep = C_LOC(bigstep)
END IF
CALL lammps_addstep_compute_all(self%handle, ptrstep)
END SUBROUTINE lmp_addstep_compute_all_bigint

! equivalent subroutine to lammps_addstep_compute_all
SUBROUTINE lmp_addstep_compute_all_smallint(self, nextstep)
CLASS(lammps), INTENT(IN) :: self
INTEGER(kind=4), INTENT(IN) :: nextstep
INTEGER(c_int), TARGET :: smallstep
INTEGER(c_int64_t), TARGET :: bigstep
TYPE(c_ptr) :: ptrstep
IF (SIZE_BIGINT == 4_c_int) THEN
smallstep = nextstep
ptrstep = C_LOC(smallstep)
ELSE
bigstep = nextstep
ptrstep = C_LOC(bigstep)
END IF
CALL lammps_addstep_compute_all(self%handle, ptrstep)
END SUBROUTINE lmp_addstep_compute_all_smallint

! equivalent function to lammps_gather_atoms (for integers)
SUBROUTINE lmp_gather_atoms_int(self, name, count, data)
CLASS(lammps), INTENT(IN) :: self
Expand Down
24 changes: 24 additions & 0 deletions python/lammps/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ def __init__(self,name='',cmdargs=None,ptr=None,comm=None):
self.lib.lammps_extract_variable_datatype.argtypes = [c_void_p, c_char_p]
self.lib.lammps_extract_variable_datatype.restype = c_int

self.lib.lammps_clearstep_compute.argtype = [c_void_p]
self.lib.lammps_addstep_compute.argtype = [c_void_p, c_void_p]
self.lib.lammps_addstep_compute_all.argtype = [c_void_p, c_void_p]

self.lib.lammps_eval.argtypes = [c_void_p, c_char_p]
self.lib.lammps_eval.restype = c_double

Expand Down Expand Up @@ -1594,6 +1598,26 @@ def extract_variable(self, name, group=None, vartype=None):

# -------------------------------------------------------------------------

def clearstep_compute(self, nextstep):
with ExceptionCheck(self):
return self.lib.lammps_clearstep_compute(self.lmp)

# -------------------------------------------------------------------------

def addstep_compute(self, nextstep):
with ExceptionCheck(self):
nextstep = self.c_bigint(nextstep)
return self.lib.lammps_addstep_compute(self.lmp, POINTER(nextstep))

# -------------------------------------------------------------------------

def addstep_compute_all(self, nextstep):
with ExceptionCheck(self):
nextstep = self.c_bigint(nextstep)
return self.lib.lammps_addstep_compute_all(self.lmp, POINTER(nextstep))

# -------------------------------------------------------------------------

def flush_buffers(self):
"""Flush output buffers
Expand Down
Loading

0 comments on commit 4889323

Please sign in to comment.