Extend lib interface to set fix external callback

This allows creating a callback in Python and attaching it to
a fix external instance.
This commit is contained in:
Richard Berger
2019-08-20 14:04:49 -06:00
parent 8cfdf4fad5
commit 2b42428d28
5 changed files with 141 additions and 2 deletions

View File

@ -0,0 +1,36 @@
# this example requires the LAMMPS Python package (lammps.py) to be installed
# and LAMMPS to be loadable as shared library in LD_LIBRARY_PATH
import lammps
def callback(caller, ntimestep, nlocal, tag, x, fext):
"""
This callback receives a caller object that was setup when registering the callback
In addition to timestep and number of local atoms, the tag and x arrays are passed as
NumPy arrays. The fext array is a force array allocated for fix external, which
can be used to apply forces to all atoms. Simply update the value in the array,
it will be directly written into the LAMMPS C arrays
"""
print("Data passed by caller (optional)", caller)
print("Timestep:", ntimestep)
print("Number of Atoms:", nlocal)
print("Atom Tags:", tag)
print("Atom Positions:", x)
print("Force Additions:", fext)
fext.fill(1.0)
print("Force additions after update:", fext)
print("="*40)
L = lammps.lammps()
L.file("in.lammps")
# you can pass an arbitrary Python object to the callback every time it is called
# this can be useful if you need more state information such as the LAMMPS ptr to
# make additional library calls
custom_object = ["Some data", L]
L.set_fix_external_callback("2", callback, custom_object)
L.command("run 100")

View File

@ -0,0 +1,23 @@
# LAMMPS input for coupling LAMMPS with Python via fix external
units metal
dimension 3
atom_style atomic
atom_modify sort 0 0.0
lattice diamond 5.43
region box block 0 1 0 1 0 1
create_box 1 box
create_atoms 1 box
mass 1 28.08
velocity all create 300.0 87293 loop geom
fix 1 all nve
fix 2 all external pf/callback 1 1
#dump 2 all image 25 image.*.jpg type type &
# axes yes 0.8 0.02 view 60 -30
#dump_modify 2 pad 3
thermo 1