Hi LAMMPS developers and users,
With Python lammps module, I’ve known that the atom-style variable values can be obtained by extract_variable
method. The question is how to set the atom-style variable values, for example, by passing the numpy ndarray to lammps.
This question is a continuation of Problem 2 in Loop cannot be run normally in python lammps module - LAMMPS / LAMMPS General Discussion - Materials Science Community Discourse. In details, I am running a simulation which has to be coupled with a complex python function, say mypyfunc
. For each timestep, mypyfunc
receives the input from lammps simulated results, v_res
, and computed the external force exerted on each atom, fext
, and passes fext
to lammps. For the time being, I don’t know how to set the per-atom fext
via the Python lammps module. The expedient is that I save fext
to a csv file, and let lammps read this file, assign fext
to an atomfile-style variable as follows:
from mypyfunc import mypyfunc, save_to_csv
from lammps import *
lmp = lammps()
lmp.file('initialize.in')
for i in range(Nt):
lmp.command('run 1')
v_res = np.array([lmp.numpy.extract_fix(
'res', LMP_STYLE_GLOBAL, LMP_TYPE_ARRAY, nrow=nrow, ncol=2
) for nrow in range(50)])
fext = mypyfunc(v_res)
save_to_csv(fext, 'fext_current.csv')
lmp.command('variable fext delete')
lmp.command('variable fext atomfile fext_current.csv')
I think this is very awkward and time-consuming, in that I must delete fext
, re-create it, read a file from the disk, parse it, and re-assign it to fext
. As @akohlmey suggested in Loop cannot be run normally in python lammps module - LAMMPS / LAMMPS General Discussion - Materials Science Community Discourse
The only way I can think to speed this up would be to not read forces from files, but compute them on-the-fly.
In practice, they are indeed computed on the fly, merely via Python. LAMMPS has very good support for Python programming, in terms of whether the Python lammps/pylammps module or the embedded LAMMPS PYTHON package. So I believe there must a way to set this per-atom force on the fly, rather than save it to a file and read it. But I haven’t found this method yet. The methods to set variables are only set_string_variable
and set_internal_variable
, but they are meant for string-style and internal-style variables and cannot satisfy my needs.
Any commens or advice will be appreciated!