git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@15492 f3b2605a-c512-4ea7-a41b-209d697bcdaa

This commit is contained in:
sjplimp
2016-08-23 22:44:48 +00:00
parent 1e1f68c30d
commit 662335db13
538 changed files with 7448 additions and 14043 deletions

View File

@ -8,7 +8,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>11. Python interface to LAMMPS &mdash; LAMMPS documentation</title>
<title>Python interface to LAMMPS &mdash; LAMMPS documentation</title>
@ -32,9 +32,7 @@
<link rel="top" title="LAMMPS documentation" href="index.html"/>
<link rel="next" title="12. Errors" href="Section_errors.html"/>
<link rel="prev" title="10. Modifying &amp; extending LAMMPS" href="Section_modify.html"/>
<link rel="top" title="LAMMPS documentation" href="index.html"/>
<script src="_static/js/modernizr.min.js"></script>
@ -73,37 +71,26 @@
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="Section_intro.html">1. Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_start.html">2. Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_commands.html">3. Commands</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_packages.html">4. Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_accelerate.html">5. Accelerating LAMMPS performance</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_howto.html">6. How-to discussions</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_example.html">7. Example problems</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_perf.html">8. Performance &amp; scalability</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_tools.html">9. Additional tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_modify.html">10. Modifying &amp; extending LAMMPS</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">11. Python interface to LAMMPS</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#overview-of-running-lammps-from-python">11.1. Overview of running LAMMPS from Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="#overview-of-using-python-from-a-lammps-script">11.2. Overview of using Python from a LAMMPS script</a></li>
<li class="toctree-l2"><a class="reference internal" href="#building-lammps-as-a-shared-library">11.3. Building LAMMPS as a shared library</a></li>
<li class="toctree-l2"><a class="reference internal" href="#installing-the-python-wrapper-into-python">11.4. Installing the Python wrapper into Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="#extending-python-with-mpi-to-run-in-parallel">11.5. Extending Python with MPI to run in parallel</a></li>
<li class="toctree-l2"><a class="reference internal" href="#testing-the-python-lammps-interface">11.6. Testing the Python-LAMMPS interface</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#test-lammps-and-python-in-serial">11.6.1. <strong>Test LAMMPS and Python in serial:</strong></a></li>
<li class="toctree-l3"><a class="reference internal" href="#test-lammps-and-python-in-parallel">11.6.2. <strong>Test LAMMPS and Python in parallel:</strong></a></li>
<li class="toctree-l3"><a class="reference internal" href="#running-python-scripts">11.6.3. <strong>Running Python scripts:</strong></a></li>
<!-- Local TOC -->
<div class="local-toc"><ul>
<li><a class="reference internal" href="#">Python interface to LAMMPS</a><ul>
<li><a class="reference internal" href="#overview-of-running-lammps-from-python">Overview of running LAMMPS from Python</a></li>
<li><a class="reference internal" href="#overview-of-using-python-from-a-lammps-script">Overview of using Python from a LAMMPS script</a></li>
<li><a class="reference internal" href="#building-lammps-as-a-shared-library">Building LAMMPS as a shared library</a></li>
<li><a class="reference internal" href="#installing-the-python-wrapper-into-python">Installing the Python wrapper into Python</a></li>
<li><a class="reference internal" href="#extending-python-with-mpi-to-run-in-parallel">Extending Python with MPI to run in parallel</a></li>
<li><a class="reference internal" href="#testing-the-python-lammps-interface">Testing the Python-LAMMPS interface</a><ul>
<li><a class="reference internal" href="#test-lammps-and-python-in-serial"><strong>Test LAMMPS and Python in serial:</strong></a></li>
<li><a class="reference internal" href="#test-lammps-and-python-in-parallel"><strong>Test LAMMPS and Python in parallel:</strong></a></li>
<li><a class="reference internal" href="#running-python-scripts"><strong>Running Python scripts:</strong></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#using-lammps-from-python">11.7. Using LAMMPS from Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="#example-python-scripts-that-use-lammps">11.8. Example Python scripts that use LAMMPS</a></li>
<li><a class="reference internal" href="#using-lammps-from-python">Using LAMMPS from Python</a></li>
<li><a class="reference internal" href="#example-python-scripts-that-use-lammps">Example Python scripts that use LAMMPS</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="Section_errors.html">12. Errors</a></li>
<li class="toctree-l1"><a class="reference internal" href="Section_history.html">13. Future and history</a></li>
</ul>
</div>
</div>
@ -126,7 +113,7 @@
<ul class="wy-breadcrumbs">
<li><a href="Manual.html">Docs</a> &raquo;</li>
<li>11. Python interface to LAMMPS</li>
<li>Python interface to LAMMPS</li>
<li class="wy-breadcrumbs-aside">
@ -137,21 +124,12 @@
</ul>
<hr/>
<div class="rst-footer-buttons" style="margin-bottom: 1em" role="navigation" aria-label="footer navigation">
<a href="Section_errors.html" class="btn btn-neutral float-right" title="12. Errors" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="Section_modify.html" class="btn btn-neutral" title="10. Modifying &amp; extending LAMMPS" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="python-interface-to-lammps">
<h1>11. Python interface to LAMMPS</h1>
<h1>Python interface to LAMMPS</h1>
<p>LAMMPS can work together with Python in two ways. First, Python can
wrap LAMMPS through the <a class="reference internal" href="Section_howto.html#howto-19"><span class="std std-ref">LAMMPS library interface</span></a>, so that a Python script can
create one or more instances of LAMMPS and launch one or more
@ -197,7 +175,7 @@ interesting Python functions that can be called from a LAMMPS input
script, that you think would be useful to other users, please <a class="reference external" href="http://lammps.sandia.gov/authors.html">email them to the developers</a>. We can
include them in the LAMMPS distribution.</p>
<div class="section" id="overview-of-running-lammps-from-python">
<span id="py-1"></span><h2>11.1. Overview of running LAMMPS from Python</h2>
<span id="py-1"></span><h2>Overview of running LAMMPS from Python</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
@ -237,7 +215,7 @@ check which version of Python you have installed, by simply typing
<hr class="docutils" />
</div>
<div class="section" id="overview-of-using-python-from-a-lammps-script">
<span id="py-2"></span><h2>11.2. Overview of using Python from a LAMMPS script</h2>
<span id="py-2"></span><h2>Overview of using Python from a LAMMPS script</h2>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">It is not currently possible to use the <a class="reference internal" href="python.html"><span class="doc">python</span></a>
@ -300,9 +278,9 @@ file and the shared library.</p>
<hr class="docutils" />
</div>
<div class="section" id="building-lammps-as-a-shared-library">
<span id="py-3"></span><h2>11.3. Building LAMMPS as a shared library</h2>
<span id="py-3"></span><h2>Building LAMMPS as a shared library</h2>
<p>Instructions on how to build LAMMPS as a shared library are given in
<a class="reference internal" href="Section_start.html#start-5"><span class="std std-ref">Section_start 5</span></a>. A shared library is one
<span class="xref std std-ref">Section_start 5</span>. 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 &#8221;.so&#8221;, not &#8221;.a&#8221;.</p>
<p>From the src directory, type</p>
@ -321,12 +299,12 @@ most recently built version.</p>
auxiliary libraries (used by various packages), then all of these
extra libraries must also be shared libraries. If the LAMMPS
shared-library build fails with an error complaining about this, see
<a class="reference internal" href="Section_start.html#start-5"><span class="std std-ref">Section_start 5</span></a> for more details.</p>
<span class="xref std std-ref">Section_start 5</span> for more details.</p>
</div>
<hr class="docutils" />
</div>
<div class="section" id="installing-the-python-wrapper-into-python">
<span id="py-4"></span><h2>11.4. Installing the Python wrapper into Python</h2>
<span id="py-4"></span><h2>Installing the Python wrapper into Python</h2>
<p>For Python to invoke LAMMPS, there are 2 files it needs to know about:</p>
<ul class="simple">
<li>python/lammps.py</li>
@ -388,7 +366,7 @@ environment variable as described above.</p>
<hr class="docutils" />
</div>
<div class="section" id="extending-python-with-mpi-to-run-in-parallel">
<span id="py-5"></span><h2>11.5. Extending Python with MPI to run in parallel</h2>
<span id="py-5"></span><h2>Extending Python with MPI to run in parallel</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>
@ -526,7 +504,7 @@ the right one.</p>
<hr class="docutils" />
</div>
<div class="section" id="testing-the-python-lammps-interface">
<span id="py-6"></span><h2>11.6. Testing the Python-LAMMPS interface</h2>
<span id="py-6"></span><h2>Testing the Python-LAMMPS interface</h2>
<p>To test if LAMMPS is callable from Python, launch Python interactively
and type:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">lammps</span> <span class="k">import</span> <span class="n">lammps</span>
@ -549,11 +527,11 @@ first importing from the lammps.py file:</p>
<span class="gp">&gt;&gt;&gt; </span><span class="n">CDLL</span><span class="p">(</span><span class="s2">&quot;liblammps.so&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If an error occurs, carefully go thru the steps in <a class="reference internal" href="Section_start.html#start-5"><span class="std std-ref">Section_start 5</span></a> and above about building a shared
<p>If an error occurs, carefully go thru the steps in <span class="xref std std-ref">Section_start 5</span> and above about building a shared
library and about insuring Python can find the necessary two files
it needs.</p>
<div class="section" id="test-lammps-and-python-in-serial">
<h3>11.6.1. <strong>Test LAMMPS and Python in serial:</strong></h3>
<h3><strong>Test LAMMPS and Python in serial:</strong></h3>
<p>To run a LAMMPS test in serial, type these lines into Python
interactively from the bench directory:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">lammps</span> <span class="k">import</span> <span class="n">lammps</span>
@ -573,7 +551,7 @@ typed something like:</p>
</div>
</div>
<div class="section" id="test-lammps-and-python-in-parallel">
<h3>11.6.2. <strong>Test LAMMPS and Python in parallel:</strong></h3>
<h3><strong>Test LAMMPS and Python in parallel:</strong></h3>
<p>To run LAMMPS in parallel, assuming you have installed the
<a class="reference external" href="Pypar">Pypar</a> package as discussed above, create a test.py file
containing these lines:</p>
@ -620,7 +598,7 @@ script should be pypar.finalize(), to insure MPI is shut down
correctly.</p>
</div>
<div class="section" id="running-python-scripts">
<h3>11.6.3. <strong>Running Python scripts:</strong></h3>
<h3><strong>Running Python scripts:</strong></h3>
<p>Note that any Python script (not just for LAMMPS) can be invoked in
one of several ways:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">%</span> <span class="n">python</span> <span class="n">foo</span><span class="o">.</span><span class="n">script</span>
@ -647,7 +625,7 @@ Python on a single processor, not in parallel.</p>
</div>
</div>
<div class="section" id="using-lammps-from-python">
<span id="py-7"></span><h2>11.7. Using LAMMPS from Python</h2>
<span id="py-7"></span><h2>Using LAMMPS from Python</h2>
<p>As described above, the Python interface to LAMMPS consists of a
Python &#8220;lammps&#8221; module, the source code for which is in
python/lammps.py, which creates a &#8220;lammps&#8221; object, with a set of
@ -855,7 +833,7 @@ Python script. Isn&#8217;t ctypes amazing?</li>
</ul>
</div>
<div class="section" id="example-python-scripts-that-use-lammps">
<span id="py-8"></span><h2>11.8. Example Python scripts that use LAMMPS</h2>
<span id="py-8"></span><h2>Example Python scripts that use LAMMPS</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
@ -976,15 +954,6 @@ different visualization package options. Click to see larger images:</p>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="Section_errors.html" class="btn btn-neutral float-right" title="12. Errors" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="Section_modify.html" class="btn btn-neutral" title="10. Modifying &amp; extending LAMMPS" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>