multiple instances of lammps in python

I am trying to test out some algorithmic ideas using lammps for forces/energies within python, and I’m encountering the following issue that I do not entirely know how to resolve.

My code layout is as follows:

# Problem setup

import numpy as np
from lammps import lammps
#from ctypes import *

lmp = lammps()
lmp2 = lammps()

lmp.file(“problem_setup.lammps")
lmp2.file(“problem_setup.lammps”)

natoms = lmp.get_natoms()

# Create two numpy wrappers for positions and forces for easy access/manipuliton
X = np.ctypeslib.as_array(lmp.extract_atom("x",3).contents,shape=(natoms,3))
F = np.ctypeslib.as_array(lmp.extract_atom("f",3).contents,shape=(natoms,3))

X2 = np.ctypeslib.as_array(lmp2.extract_atom("x",3).contents,shape=(natoms,3))
F2 = np.ctypeslib.as_array(lmp2.extract_atom("f",3).contents,shape=(natoms,3))

# run some standard lammps code (i.e., with a standard fix) for some number of time steps
# not reproduced here

# As a first test, I will try copying over the current configuration, stored in X, and quench it
# to inspect the minimized state
X2 = np.copy(X)
lmp2.command("run 0")
lmp2.command("min_style cg")
lmp2.command("minimize 1.e-14 1.e-14 1000 1000")

It is at this point that I am encountering my “problem." I would have thought X2 now held the quenched
positions. But the values in X2 are unchanged. The forces associated with lmp2, stored in F2, are altered
and are close to zero, as expected.

-gideon

I am trying to test out some algorithmic ideas using lammps for
forces/energies within python, and I’m encountering the following issue
that I do not entirely know how to resolve.

My code layout is as follows:

# Problem setup

import numpy as np
from lammps import lammps
#from ctypes import *

lmp = lammps()
lmp2 = lammps()

lmp.file(“problem_setup.lammps")
lmp2.file(“problem_setup.lammps”)

natoms = lmp.get_natoms()

# Create two numpy wrappers for positions and forces for easy
access/manipuliton
X = np.ctypeslib.as_array(lmp.extract_atom("x",3).contents,
shape=(natoms,3))
F = np.ctypeslib.as_array(lmp.extract_atom("f",3).contents,
shape=(natoms,3))

​this is not correct. the dimension of the array is *not* "natoms,3". it is
o​f dimension "nlocal,3".
this is only working by coincidence, since you are not running LAMMPS in
parallel.

X2 = np.ctypeslib.as_array(lmp2.extract_atom("x",3).contents,
shape=(natoms,3))
F2 = np.ctypeslib.as_array(lmp2.extract_atom("f",3).contents,
shape=(natoms,3))

# run some standard lammps code (i.e., with a standard fix) for some
number of time steps
# not reproduced here

# As a first test, I will try copying over the current configuration,
stored in X, and quench it
# to inspect the minimized state
X2 = np.copy(X)
lmp2.command("run 0")
lmp2.command("min_style cg")
lmp2.command("minimize 1.e-14 1.e-14 1000 1000")

It is at this point that I am encountering my “problem." I would have
thought X2 now held the quenched
positions. But the values in X2 are unchanged. The forces associated
with lmp2, stored in F2, are altered
and are close to zero, as expected.

​from the comment about the lammps_extract_atom() function:

/* ----------------------------------------------------------------------
   extract a pointer to an internal LAMMPS atom-based entity
   name = desired quantity, e.g. x or mass
   returns a void pointer to the entity
     which the caller can cast to the proper data type
   returns a NULL if Atom::extract() does not recognize the name

** the returned pointer is not a permanent valid reference to the
     per-atom quantity, since LAMMPS may reallocate per-atom data **

   customize by adding names to Atom::extract()
------------------------------------------------------------------------- */

BTW: the cleaner way to get/set/update atom parameters for your kind of setup would be to use the gather_atoms and scatter_atoms functions.
axel.