Commit c4f90b38 authored by sjplimp's avatar sjplimp Committed by GitHub
Browse files

Merge pull request #449 from rbberger/python_refactoring

Add Python 3 compatibility and expand Python interface availability
parents a73402ad f8af7edf
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -118,11 +118,6 @@ check which version of Python you have installed, by simply typing

11.2 Overview of using Python from a LAMMPS script :link(py_2),h4

NOTE: It is not currently possible to use the "python"_python.html
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.

LAMMPS has a "python"_python.html 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
+3 −17
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ def factorial(n):
  return n * factorial(n-1)
 """ :pre

python loop input 1 SELF return v_value format -f here """
python loop input 1 SELF return v_value format pf here """
def loop(lmpptr,N,cut0):
  from lammps import lammps
  lmp = lammps(ptr=lmpptr) :pre
@@ -67,11 +67,6 @@ def loop(lmpptr,N,cut0):

[Description:]

NOTE: It is not currently possible to use the "python"_python.html
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.

Define a Python function or execute a previously defined function.
Arguments, including LAMMPS variables, can be passed to the function
from the LAMMPS input script and a value returned by the Python
@@ -479,15 +474,6 @@ consistent (built from the same source code files) in order for this
to work.  If the two have been built at different times using
different source files, problems may occur. 

As described above, you can use the python command to invoke a Python
function which calls back to LAMMPS through its Python-wrapped library
interface.  However you cannot do the opposite.  I.e. you cannot call
LAMMPS from Python and invoke the python command to "callback" to
Python and execute a Python function.  LAMMPS will generate an error
if you try to do that.  Note that we think there actually should be a
way to do that, but haven't yet been able to figure out how to do it
successfully.

[Related commands:]

"shell"_shell.html, "variable"_variable.html
+1 −1
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ using the generated {auto} Makefile.
cd $LAMMPS_DIR/src :pre

# generate custom Makefile
python2 Make.py -jpg -png -s ffmpeg exceptions -m mpi -a file :pre
python Make.py -jpg -png -s ffmpeg exceptions -m mpi -a file :pre

# add packages if necessary
make yes-MOLECULE :pre
+4 −2
Original line number Diff line number Diff line
# Python function that implements a loop of short runs
# calls back to LAMMPS via "lmp" instance
# lammps() must be called with ptr=lmpptr for this to work
from __future__ import print_function

def loop(N,cut0,thresh,lmpptr):
  print "LOOP ARGS",N,cut0,thresh,lmpptr
  print("LOOP ARGS",N,cut0,thresh,lmpptr)
  from lammps import lammps
  lmp = lammps(ptr=lmpptr)
  natoms = lmp.get_natoms()
@@ -12,11 +13,12 @@ def loop(N,cut0,thresh,lmpptr):
    cut = cut0 + i*0.1

    lmp.set_variable("cut",cut)                 # set a variable in LAMMPS

    lmp.command("pair_style lj/cut ${cut}")     # LAMMPS command
    #lmp.command("pair_style lj/cut %d" % cut)  # LAMMPS command option

    lmp.command("pair_coeff * * 1.0 1.0")       # ditto
    lmp.command("run 10")                       # ditto
    pe = lmp.extract_compute("thermo_pe",0,0)   # extract total PE from LAMMPS
    print "PE",pe/natoms,thresh
    print("PE",pe/natoms,thresh)
    if pe/natoms < thresh: return
+5 −4
Original line number Diff line number Diff line
@@ -25,13 +25,14 @@ run 10
# example of catching a syntax error

python          simple here """
from __future__ import print_function

def simple():
  import exceptions
  print "Inside simple function"
  print("Inside simple function")
  try:
    foo += 1
  except Exception, e:
    print "FOO error:",e
  except Exception as e:
    print("FOO error:", e)
"""

python          simple invoke
Loading