<liclass="toctree-l1"><aclass="reference internal"href="Section_modify.html">10. Modifying & extending LAMMPS</a></li>
<liclass="toctree-l1 current"><aclass="current reference internal"href="">11. Python interface to LAMMPS</a><ul>
<liclass="toctree-l2"><aclass="reference internal"href="#overview-of-running-lammps-from-python">11.1. Overview of running LAMMPS from Python</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#overview-of-using-python-from-a-lammps-script">11.2. Overview of using Python from a LAMMPS script</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#building-lammps-as-a-shared-library">11.3. Building LAMMPS as a shared library</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#installing-the-python-wrapper-into-python">11.4. Installing the Python wrapper into Python</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#extending-python-with-mpi-to-run-in-parallel">11.5. Extending Python with MPI to run in parallel</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="#testing-the-python-lammps-interface">11.6. Testing the Python-LAMMPS interface</a><ul>
<liclass="toctree-l3"><aclass="reference internal"href="#test-lammps-and-python-in-serial">11.6.1. <strong>Test LAMMPS and Python in serial:</strong></a></li>
<liclass="toctree-l3"><aclass="reference internal"href="#test-lammps-and-python-in-parallel">11.6.2. <strong>Test LAMMPS and Python in parallel:</strong></a></li>
<h1>11. Python interface to LAMMPS<aclass="headerlink"href="#python-interface-to-lammps"title="Permalink to this headline">¶</a></h1>
<p>LAMMPS can work together with Python in two ways. First, Python can
wrap LAMMPS through the <aclass="reference internal"href="Section_howto.html#howto-19"><span>LAMMPS library interface</span></a>, so that a Python script can
create one or more instances of LAMMPS and launch one or more
simulations. In Python lingo, this is “extending” Python with LAMMPS.</p>
<p>Second, LAMMPS can use the Python interpreter, so that a LAMMPS input
script can invoke Python code, and pass information back-and-forth
between the input script and Python functions you write. The Python
code can also callback to LAMMPS to query or change its attributes.
In Python lingo, this is “embedding” Python in LAMMPS.</p>
<p>This section describes how to do both.</p>
<ulclass="simple">
<li>11.1 <aclass="reference internal"href="#py-1"><span>Overview of running LAMMPS from Python</span></a></li>
<li>11.2 <aclass="reference internal"href="#py-2"><span>Overview of using Python from a LAMMPS script</span></a></li>
<li>11.3 <aclass="reference internal"href="#py-3"><span>Building LAMMPS as a shared library</span></a></li>
<li>11.4 <aclass="reference internal"href="#py-4"><span>Installing the Python wrapper into Python</span></a></li>
<li>11.5 <aclass="reference internal"href="#py-5"><span>Extending Python with MPI to run in parallel</span></a></li>
<li>11.6 <aclass="reference internal"href="#py-6"><span>Testing the Python-LAMMPS interface</span></a></li>
<li>11.7 <aclass="reference internal"href="#py-7"><span>Using LAMMPS from Python</span></a></li>
<li>11.8 <aclass="reference internal"href="#py-8"><span>Example Python scripts that use LAMMPS</span></a></li>
</ul>
<p>If you are not familiar with it, <aclass="reference external"href="http://www.python.org">Python</a> is a
powerful scripting and programming language which can essentially do
anything that faster, lower-level languages like C or C++ can do, but
typically with much fewer lines of code. When used in embedded mode,
Python can perform operations that the simplistic LAMMPS input script
syntax cannot. Python can be also be used as a “glue” language to
drive a program through its library interface, or to hook multiple
pieces of software together, such as a simulation package plus a
visualization package, or to run a coupled multiscale or multiphysics
model.</p>
<p>See <aclass="reference internal"href="Section_howto.html#howto-10"><span>Section_howto 10</span></a> of the manual and
the couple directory of the distribution for more ideas about coupling
LAMMPS to other codes. See <aclass="reference internal"href="Section_howto.html#howto-19"><span>Section_howto 19</span></a> for a description of the LAMMPS
library interface provided in src/library.cpp and src/library.h, and
how to extend it for your needs. As described below, that interface
is what is exposed to Python either when calling LAMMPS from Python or
when calling Python from a LAMMPS input script and then calling back
to LAMMPS from Python code. The library interface is designed to be
easy to add functions to. Thus the Python interface to LAMMPS is also
easy to extend as well.</p>
<p>If you create interesting Python scripts that run LAMMPS or
interesting Python functions that can be called from a LAMMPS input
script, that you think would be useful to other users, please <aclass="reference external"href="http://lammps.sandia.gov/authors.html">email them to the developers</a>. We can
<spanid="py-1"></span><h2>11.1. Overview of running LAMMPS from Python<aclass="headerlink"href="#overview-of-running-lammps-from-python"title="Permalink to this headline">¶</a></h2>
<p>The LAMMPS distribution includes a python directory with all you need
to run LAMMPS from Python. The python/lammps.py file wraps the LAMMPS
library interface, with one wrapper function per LAMMPS library
function. This file makes it is possible to do the following either
from a Python script, or interactively from a Python prompt: create
one or more instances of LAMMPS, invoke LAMMPS commands or give it an
input script, run LAMMPS incrementally, extract LAMMPS results, an
modify internal LAMMPS variables. From a Python script you can do
this in serial or parallel. Running Python interactively in parallel
does not generally work, unless you have a version of Python that
extends standard Python to enable multiple instances of Python to read
what you type.</p>
<p>To do all of this, you must first build LAMMPS as a shared library,
then insure that your Python can find the python/lammps.py file and
the shared library. These steps are explained in subsequent sections
11.3 and 11.4. Sections 11.5 and 11.6 discuss using MPI from a
parallel Python program and how to test that you are ready to use
LAMMPS from Python. Section 11.7 lists all the functions in the
current LAMMPS library interface and how to call them from Python.</p>
<p>Section 11.8 gives some examples of coupling LAMMPS to other tools via
Python. For example, LAMMPS can easily be coupled to a GUI or other
visualization tools that display graphs or animations in real time as
LAMMPS runs. Examples of such scripts are inlcluded in the python
directory.</p>
<p>Two advantages of using Python to run LAMMPS are how concise the
language is, and that it can be run interactively, enabling rapid
development and debugging of programs. If you use it to mostly invoke
costly operations within LAMMPS, such as running a simulation for a
reasonable number of timesteps, then the overhead cost of invoking
LAMMPS thru Python will be negligible.</p>
<p>The Python wrapper for LAMMPS uses the amazing and magical (to me)
“ctypes” package in Python, which auto-generates the interface code
needed between Python and a set of C interface routines for a library.
Ctypes is part of standard Python for versions 2.5 and later. You can
check which version of Python you have installed, by simply typing
<spanid="py-2"></span><h2>11.2. Overview of using Python from a LAMMPS script<aclass="headerlink"href="#overview-of-using-python-from-a-lammps-script"title="Permalink to this headline">¶</a></h2>
<divclass="admonition warning">
<pclass="first admonition-title">Warning</p>
<pclass="last">It is not currently possible to use the
<aclass="reference internal"href="python.html"><em>python</em></a> command described in this section with Python 3,
only with Python 2. The C API changed from Python 2 to 3 and the
LAMMPS code is not compatible with both.</p>
</div>
<p>LAMMPS has a <aclass="reference internal"href="python.html"><em>python</em></a> command which can be used in an
input script to define and execute a Python function that you write
the code for. The Python function can also be assigned to a LAMMPS
python-style variable via the <aclass="reference internal"href="variable.html"><em>variable</em></a> command. Each
time the variable is evaluated, either in the LAMMPS input script
itself, or by another LAMMPS command that uses the variable, this will
trigger the Python function to be invoked.</p>
<p>The Python code for the function can be included directly in the input
script or in an auxiliary file. The function can have arguments which
are mapped to LAMMPS variables (also defined in the input script) and
it can return a value to a LAMMPS variable. This is thus a mechanism
for your input script to pass information to a piece of Python code,
ask Python to execute the code, and return information to your input
script.</p>
<p>Note that a Python function can be arbitrarily complex. It can import
other Python modules, instantiate Python classes, call other Python
functions, etc. The Python code that you provide can contain more
code than the single function. It can contain other functions or
Python classes, as well as global variables or other mechanisms for
storing state between calls from LAMMPS to the function.</p>
<p>The Python function you provide can consist of “pure” Python code that
only performs operations provided by standard Python. However, the
Python function can also “call back” to LAMMPS through its
Python-wrapped library interface, in the manner described in the
previous section 11.1. This means it can issue LAMMPS input script
commands or query and set internal LAMMPS state. As an example, this
can be useful in an input script to create a more complex loop with
branching logic, than can be created using the simple looping and
brancing logic enabled by the <aclass="reference internal"href="next.html"><em>next</em></a> and <aclass="reference internal"href="if.html"><em>if</em></a>
commands.</p>
<p>See the <aclass="reference internal"href="python.html"><em>python</em></a> doc page and the <aclass="reference internal"href="variable.html"><em>variable</em></a>
doc page for its python-style variables for more info, including
examples of Python code you can write for both pure Python operations
and callbacks to LAMMPS.</p>
<p>To run pure Python code from LAMMPS, you only need to build LAMMPS
with the PYTHON package installed:</p>
<p>make yes-python
make machine</p>
<p>Note that this will link LAMMPS with the Python library on your
system, which typically requires several auxiliary system libraries to
also be linked. The list of these libraries and the paths to find
them are specified in the lib/python/Makefile.lammps file. You need
to insure that file contains the correct information for your version
of Python and your machine to successfully build LAMMPS. See the
lib/python/README file for more info.</p>
<p>If you want to write Python code with callbacks to LAMMPS, then you
must also follow the steps overviewed in the preceeding section (11.1)
for running LAMMPS from Python. I.e. you must build LAMMPS as a
shared library and insure that Python can find the python/lammps.py
<spanid="py-3"></span><h2>11.3. Building LAMMPS as a shared library<aclass="headerlink"href="#building-lammps-as-a-shared-library"title="Permalink to this headline">¶</a></h2>
<p>Instructions on how to build LAMMPS as a shared library are given in
<aclass="reference internal"href="Section_start.html#start-5"><span>Section_start 5</span></a>. A shared library is one
that is dynamically loadable, which is what Python requires to wrap
LAMMPS. On Linux this is a library file that ends in ”.so”, not ”.a”.</p>
<spanid="py-4"></span><h2>11.4. Installing the Python wrapper into Python<aclass="headerlink"href="#installing-the-python-wrapper-into-python"title="Permalink to this headline">¶</a></h2>
<p>For Python to invoke LAMMPS, there are 2 files it needs to know about:</p>
<ulclass="simple">
<li>python/lammps.py</li>
<li>src/liblammps.so</li>
</ul>
<p>Lammps.py is the Python wrapper on the LAMMPS library interface.
Liblammps.so is the shared LAMMPS library that Python loads, as
described above.</p>
<p>You can insure Python can find these files in one of two ways:</p>
<ulclass="simple">
<li>set two environment variables</li>
<li>run the python/install.py script</li>
</ul>
<p>If you set the paths to these files as environment variables, you only
have to do it once. For the csh or tcsh shells, add something like
this to your ~/.cshrc file, one line for each of the two files:</p>
<spanid="py-5"></span><h2>11.5. Extending Python with MPI to run in parallel<aclass="headerlink"href="#extending-python-with-mpi-to-run-in-parallel"title="Permalink to this headline">¶</a></h2>
<p>If you wish to run LAMMPS in parallel from Python, you need to extend
your Python with an interface to MPI. This also allows you to
make MPI calls directly from Python in your script, if you desire.</p>
<p>There are several Python packages available that purport to wrap MPI
as a library and allow MPI functions to be called from Python.</p>
<spanid="py-6"></span><h2>11.6. Testing the Python-LAMMPS interface<aclass="headerlink"href="#testing-the-python-lammps-interface"title="Permalink to this headline">¶</a></h2>
<p>To test if LAMMPS is callable from Python, launch Python interactively
<p>If an error occurs, carefully go thru the steps in <aclass="reference internal"href="Section_start.html#start-5"><span>Section_start 5</span></a> and above about building a shared
library and about insuring Python can find the necessary two files
<h3>11.6.1. <strong>Test LAMMPS and Python in serial:</strong><aclass="headerlink"href="#test-lammps-and-python-in-serial"title="Permalink to this headline">¶</a></h3>
<p>To run a LAMMPS test in serial, type these lines into Python
<h3>11.6.2. <strong>Test LAMMPS and Python in parallel:</strong><aclass="headerlink"href="#test-lammps-and-python-in-parallel"title="Permalink to this headline">¶</a></h3>
<p>To run LAMMPS in parallel, assuming you have installed the
<aclass="reference external"href="http://datamining.anu.edu.au/~ole/pypar">Pypar</a> package as discussed
above, create a test.py file containing these lines:</p>
<p>Without the “-i” flag, Python will exit when the script finishes.
With the “-i” flag, you will be left in the Python interpreter when
the script finishes, so you can type subsequent commands. As
mentioned above, you can only run Python interactively when running
Python on a single processor, not in parallel.</p>
</div>
</div>
<divclass="section"id="using-lammps-from-python">
<spanid="py-7"></span><h2>11.7. Using LAMMPS from Python<aclass="headerlink"href="#using-lammps-from-python"title="Permalink to this headline">¶</a></h2>
<p>As described above, the Python interface to LAMMPS consists of a
Python “lammps” module, the source code for which is in
python/lammps.py, which creates a “lammps” object, with a set of
methods that can be invoked on that object. The sample Python code
below assumes you have first imported the “lammps” module in your
<p>These are the methods defined by the lammps module. If you look at
the files src/library.cpp and src/library.h you will see that they
correspond one-to-one with calls you can make to the LAMMPS library
from a C++ or C or Fortran program.</p>
<divclass="highlight-python"><divclass="highlight"><pre><spanclass="n">lmp</span><spanclass="o">=</span><spanclass="n">lammps</span><spanclass="p">()</span><spanclass="c"># create a LAMMPS object using the default liblammps.so library</span>
<spanclass="n">lmp</span><spanclass="o">=</span><spanclass="n">lammps</span><spanclass="p">(</span><spanclass="n">ptr</span><spanclass="o">=</span><spanclass="n">lmpptr</span><spanclass="p">)</span><spanclass="c"># ditto, but use lmpptr as previously created LAMMPS object</span>
<spanclass="n">lmp</span><spanclass="o">=</span><spanclass="n">lammps</span><spanclass="p">(</span><spanclass="s">"g++"</span><spanclass="p">)</span><spanclass="c"># create a LAMMPS object using the liblammps_g++.so library</span>
<spanclass="n">lmp</span><spanclass="o">=</span><spanclass="n">lammps</span><spanclass="p">(</span><spanclass="s">""</span><spanclass="p">,</span><spanclass="nb">list</span><spanclass="p">)</span><spanclass="c"># ditto, with command-line args, e.g. list = ["-echo","screen"]</span>
<divclass="highlight-python"><divclass="highlight"><pre><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">close</span><spanclass="p">()</span><spanclass="c"># destroy a LAMMPS object</span>
</pre></div>
</div>
<divclass="highlight-python"><divclass="highlight"><pre><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">file</span><spanclass="p">(</span><spanclass="nb">file</span><spanclass="p">)</span><spanclass="c"># run an entire input script, file = "in.lj"</span>
<spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">command</span><spanclass="p">(</span><spanclass="n">cmd</span><spanclass="p">)</span><spanclass="c"># invoke a single LAMMPS command, cmd = "run 100"</span>
</pre></div>
</div>
<divclass="highlight-python"><divclass="highlight"><pre><spanclass="n">xlo</span><spanclass="o">=</span><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">extract_global</span><spanclass="p">(</span><spanclass="n">name</span><spanclass="p">,</span><spanclass="nb">type</span><spanclass="p">)</span><spanclass="c"># extract a global quantity</span>
<spanclass="c"># name = "boxxlo", "nlocal", etc</span>
<spanclass="c"># type = 0 = int</span>
<spanclass="c"># 1 = double</span>
</pre></div>
</div>
<divclass="highlight-python"><divclass="highlight"><pre><spanclass="n">coords</span><spanclass="o">=</span><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">extract_atom</span><spanclass="p">(</span><spanclass="n">name</span><spanclass="p">,</span><spanclass="nb">type</span><spanclass="p">)</span><spanclass="c"># extract a per-atom quantity</span>
<spanclass="c"># name = "x", "type", etc</span>
<spanclass="c"># type = 0 = vector of ints</span>
<spanclass="c"># 1 = array of ints</span>
<spanclass="c"># 2 = vector of doubles</span>
<spanclass="c"># 3 = array of doubles</span>
</pre></div>
</div>
<divclass="highlight-python"><divclass="highlight"><pre><spanclass="n">eng</span><spanclass="o">=</span><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">extract_compute</span><spanclass="p">(</span><spanclass="nb">id</span><spanclass="p">,</span><spanclass="n">style</span><spanclass="p">,</span><spanclass="nb">type</span><spanclass="p">)</span><spanclass="c"># extract value(s) from a compute</span>
<spanclass="n">v3</span><spanclass="o">=</span><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">extract_fix</span><spanclass="p">(</span><spanclass="nb">id</span><spanclass="p">,</span><spanclass="n">style</span><spanclass="p">,</span><spanclass="nb">type</span><spanclass="p">,</span><spanclass="n">i</span><spanclass="p">,</span><spanclass="n">j</span><spanclass="p">)</span><spanclass="c"># extract value(s) from a fix</span>
<spanclass="c"># id = ID of compute or fix</span>
<spanclass="c"># style = 0 = global data</span>
<spanclass="c"># 1 = per-atom data</span>
<spanclass="c"># 2 = local data</span>
<spanclass="c"># type = 0 = scalar</span>
<spanclass="c"># 1 = vector</span>
<spanclass="c"># 2 = array</span>
<spanclass="c"># i,j = indices of value in global vector or array</span>
</pre></div>
</div>
<divclass="highlight-python"><divclass="highlight"><pre><spanclass="n">var</span><spanclass="o">=</span><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">extract_variable</span><spanclass="p">(</span><spanclass="n">name</span><spanclass="p">,</span><spanclass="n">group</span><spanclass="p">,</span><spanclass="n">flag</span><spanclass="p">)</span><spanclass="c"># extract value(s) from a variable</span>
<spanclass="c"># name = name of variable</span>
<spanclass="c"># group = group ID (ignored for equal-style variables)</span>
<spanclass="c"># flag = 0 = equal-style variable</span>
<spanclass="c"># 1 = atom-style variable</span>
</pre></div>
</div>
<divclass="highlight-python"><divclass="highlight"><pre><spanclass="n">flag</span><spanclass="o">=</span><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">set_variable</span><spanclass="p">(</span><spanclass="n">name</span><spanclass="p">,</span><spanclass="n">value</span><spanclass="p">)</span><spanclass="c"># set existing named string-style variable to value, flag = 0 if successful</span>
<spanclass="n">natoms</span><spanclass="o">=</span><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">get_natoms</span><spanclass="p">()</span><spanclass="c"># total # of atoms as int</span>
<spanclass="n">data</span><spanclass="o">=</span><spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">gather_atoms</span><spanclass="p">(</span><spanclass="n">name</span><spanclass="p">,</span><spanclass="nb">type</span><spanclass="p">,</span><spanclass="n">count</span><spanclass="p">)</span><spanclass="c"># return atom attribute of all atoms gathered into data, ordered by atom ID</span>
<spanclass="c"># name = "x", "charge", "type", etc</span>
<spanclass="c"># count = # of per-atom values, 1 or 3, etc</span>
<spanclass="n">lmp</span><spanclass="o">.</span><spanclass="n">scatter_atoms</span><spanclass="p">(</span><spanclass="n">name</span><spanclass="p">,</span><spanclass="nb">type</span><spanclass="p">,</span><spanclass="n">count</span><spanclass="p">,</span><spanclass="n">data</span><spanclass="p">)</span><spanclass="c"># scatter atom attribute of all atoms from data, ordered by atom ID</span>
<spanclass="c"># name = "x", "charge", "type", etc</span>
<spanclass="c"># count = # of per-atom values, 1 or 3, etc</span>
</pre></div>
</div>
<hrclass="docutils"/>
<divclass="admonition warning">
<pclass="first admonition-title">Warning</p>
<pclass="last">Currently, the creation of a LAMMPS object from within
lammps.py does not take an MPI communicator as an argument. There
should be a way to do this, so that the LAMMPS instance runs on a
subset of processors if desired, but I don’t know how to do it from
Pypar. So for now, it runs with MPI_COMM_WORLD, which is all the
processors. If someone figures out how to do this with one or more of
the Python wrappers for MPI, like Pypar, please let us know and we
extract_fix(), and extract_variable() methods return values or
pointers to data structures internal to LAMMPS.</p>
<p>For extract_global() see the src/library.cpp file for the list of
valid names. New names could easily be added. A double or integer is
returned. You need to specify the appropriate data type via the type
argument.</p>
<p>For extract_atom(), a pointer to internal LAMMPS atom-based data is
returned, which you can use via normal Python subscripting. See the
extract() method in the src/atom.cpp file for a list of valid names.
Again, new names could easily be added. A pointer to a vector of
doubles or integers, or a pointer to an array of doubles (double <ahref="#id2"><spanclass="problematic"id="id3">**</span></a>)
or integers (int <ahref="#id4"><spanclass="problematic"id="id5">**</span></a>) is returned. You need to specify the appropriate
data type via the type argument.</p>
<p>For extract_compute() and extract_fix(), the global, per-atom, or
local data calulated by the compute or fix can be accessed. What is
returned depends on whether the compute or fix calculates a scalar or
vector or array. For a scalar, a single double value is returned. If
the compute or fix calculates a vector or array, a pointer to the
internal LAMMPS data is returned, which you can use via normal Python
subscripting. The one exception is that for a fix that calculates a
global vector or array, a single double value from the vector or array
is returned, indexed by I (vector) or I and J (array). I,J are
zero-based indices. The I,J arguments can be left out if not needed.
See <aclass="reference internal"href="Section_howto.html#howto-15"><span>Section_howto 15</span></a> of the manual for a
discussion of global, per-atom, and local data, and of scalar, vector,
and array data types. See the doc pages for individual
<aclass="reference internal"href="compute.html"><em>computes</em></a> and <aclass="reference internal"href="fix.html"><em>fixes</em></a> for a description of what
they calculate and store.</p>
<p>For extract_variable(), an <aclass="reference internal"href="variable.html"><em>equal-style or atom-style variable</em></a> is evaluated and its result returned.</p>
<p>For equal-style variables a single double value is returned and the
group argument is ignored. For atom-style variables, a vector of
doubles is returned, one value per atom, which you can use via normal
Python subscripting. The values will be zero for atoms not in the
specified group.</p>
<p>The get_natoms() method returns the total number of atoms in the
simulation, as an int.</p>
<p>The gather_atoms() method returns a ctypes vector of ints or doubles
as specified by type, of length count*natoms, for the property of all
the atoms in the simulation specified by name, ordered by count and
then by atom ID. The vector can be used via normal Python
subscripting. If atom IDs are not consecutively ordered within
LAMMPS, a None is returned as indication of an error.</p>
<p>Note that the data structure gather_atoms(“x”) returns is different
from the data structure returned by extract_atom(“x”) in four ways.
(1) Gather_atoms() returns a vector which you index as x[i];
extract_atom() returns an array which you index as x[i][j]. (2)
Gather_atoms() orders the atoms by atom ID while extract_atom() does
not. (3) Gathert_atoms() returns a list of all atoms in the
simulation; extract_atoms() returns just the atoms local to each
processor. (4) Finally, the gather_atoms() data structure is a copy
of the atom coords stored internally in LAMMPS, whereas extract_atom()
returns an array that effectively points directly to the internal
data. This means you can change values inside LAMMPS from Python by
assigning a new values to the extract_atom() array. To do this with
the gather_atoms() vector, you need to change values in the vector,
then invoke the scatter_atoms() method.</p>
<p>The scatter_atoms() method takes a vector of ints or doubles as
specified by type, of length count*natoms, for the property of all the
atoms in the simulation specified by name, ordered by bount and then
by atom ID. It uses the vector of data to overwrite the corresponding
properties for each atom inside LAMMPS. This requires LAMMPS to have
its “map” option enabled; see the <aclass="reference internal"href="atom_modify.html"><em>atom_modify</em></a>
command for details. If it is not, or if atom IDs are not
consecutively ordered, no coordinates are reset.</p>
<p>The array of coordinates passed to scatter_atoms() must be a ctypes
vector of ints or doubles, allocated and initialized something like
<spanid="py-8"></span><h2>11.8. Example Python scripts that use LAMMPS<aclass="headerlink"href="#example-python-scripts-that-use-lammps"title="Permalink to this headline">¶</a></h2>
<p>These are the Python scripts included as demos in the python/examples
directory of the LAMMPS distribution, to illustrate the kinds of
things that are possible when Python wraps LAMMPS. If you create your
own scripts, send them to us and we can include them in the LAMMPS
distribution.</p>
<tableborder="1"class="docutils">
<colgroup>
<colwidth="27%"/>
<colwidth="73%"/>
</colgroup>
<tbodyvalign="top">
<trclass="row-odd"><td>trivial.py</td>
<td>read/run a LAMMPS input script thru Python</td>
</tr>
<trclass="row-even"><td>demo.py</td>
<td>invoke various LAMMPS library interface routines</td>
</tr>
<trclass="row-odd"><td>simple.py</td>
<td>mimic operation of couple/simple/simple.cpp in Python</td>
</tr>
<trclass="row-even"><td>gui.py</td>
<td>GUI go/stop/temperature-slider to control LAMMPS</td>
</tr>
<trclass="row-odd"><td>plot.py</td>
<td>real-time temeperature plot with GnuPlot via Pizza.py</td>
</tr>
<trclass="row-even"><td>viz_tool.py</td>
<td>real-time viz via some viz package</td>
</tr>
<trclass="row-odd"><td>vizplotgui_tool.py</td>
<td>combination of viz_tool.py and plot.py and gui.py</td>
</tr>
</tbody>
</table>
<hrclass="docutils"/>
<p>For the viz_tool.py and vizplotgui_tool.py commands, replace “tool”
with “gl” or “atomeye” or “pymol” or “vmd”, depending on what
visualization package you have installed.</p>
<p>Note that for GL, you need to be able to run the Pizza.py GL tool,
which is included in the pizza sub-directory. See the <aclass="reference external"href="http://www.sandia.gov/~sjplimp/pizza.html">Pizza.py doc pages</a> for more info:</p>
<p>Note that for AtomEye, you need version 3, and there is a line in the
scripts that specifies the path and name of the executable. See the
AtomEye WWW pages <aclass="reference external"href="http://mt.seas.upenn.edu/Archive/Graphics/A">here</a> or <aclass="reference external"href="http://mt.seas.upenn.edu/Archive/Graphics/A3/A3.html">here</a> for more details:</p>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.