LAMMPS WWW Site - LAMMPS Documentation - LAMMPS Mailing List Archives
Re: [lammps-users] multiple instances of lammps in python
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lammps-users] multiple instances of lammps in python

From: Axel Kohlmeyer <akohlmey@...24...>
Date: Sat, 11 Nov 2017 19:17:13 -0500

On Sat, Nov 11, 2017 at 3:17 PM, Gideon Simpson <gideon.simpson@...24...> wrote:
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()


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()
------------------------------------------------------------------------- */



Check out the vibrant tech community on one of the world's most
engaging tech sites,!
lammps-users mailing list

Dr. Axel Kohlmeyer  akohlmey@...24...
College of Science & Technology, Temple University, Philadelphia PA, USA
International Centre for Theoretical Physics, Trieste. Italy.