Using Python In LAMMPS

3y ago
74 Views
8 Downloads
261.87 KB
32 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Bria Koontz
Transcription

Using Python in LAMMPSDr. Richard BergerTemple UniversityLAMMPS Workshop 2017

OutlineModesInstallationComputes / Function CallsPair PythonFix PythonLibrary InterfaceJupyter notebooks

ModesEmbedded PythonILAMMPS binary launches embeddedPython interpreterIexecutes Python code on demandLAMMPSlibpythonPython as DriverIPython starts LAMMPS as libraryIcontrols LAMMPS through library callsPythonliblammps

ModesHybrid (Embedded)IPython code lauched from LAMMPS cancontrol/access it like a libraryLAMMPSlibpythonHybrid (Python as Driver)ILAMMPS can call Python code definedin driver and has access to all globalobjectsPythonliblammps

RequirementsIPYTHON package must be installedILAMMPS must be compiled as shared-libraryI(Optional) -DLAMMPS EXCEPTIONS for better error handlingILAMMPS Python module (lammps.py) must be installed

Step 1: Building LAMMPS as a shared librarycd LAMMPS DIR/srcmake yes-PYTHON# compile shared librarymake mpi mode shlib LMP INC "-DLAMMPS EXCEPTIONS"

Step 2: Installing the LAMMPS Python packagecd LAMMPS DIR/pythonpython install.pyWarning!Recompiling the shared library requires reinstaling the Python package.

CMake (soon)cd LAMMPS DIR/srcmkdir buildcd buildcmake ./cmake -DENABLE PYTHON on \-DBUILD SHARED LIBS on \-DLAMMPS EXCEPTIONS on \-DCMAKE INSTALL PREFIX .makemake install

Adding Python code within a LAMMPS input scriptEmbed Python codepython simple here """def simple():print("Inside simple function")"""python simple invokeCalling existing Python codepython simple file my funcs.pypython simple invoke

Computes: Call Python function and save result in variablefactorial &input 1 v n &return v fact &format ii &here """def factorial(n):if n 1: return 1return n*factorial(n-1)"""pythonvariableIfact python factorialEvaluation of the variable calls the function

Computes: Call Python function and save result in variablevariable n string 10print"Factorial of n {fact}"variable n string 20print"Factorial of n {fact}"

Pair Pythonpair stylepair coeffpython 2.5* * py pot.LJCutMelt ljI(created by Dr. Axel Kohlmeyer)Ifor defining simple additive pair potentials in PythonIPYTHONPATH and LAMMPS POTENTIALS in module search pathIloads class LJCutMelt from user-defined py pot moduleIPython class implements compute force and compute energy functionsImany examples, including hybrid usage in examples/python folder

Pythonclass LJCutMelt(LAMMPSPairPotential):def init (self):super(LJCutMelt, self). init ()# set coeffs: 48*eps*sig**12, 24*eps*sig**6,#4*eps*sig**12, 4*eps*sig**6self.units ’lj’self.coeff {’lj’ : {’lj’ : (48.0,24.0,4.0,4.0)}}def compute force(self, rsq, itype, jtype):coeff v 1.0/rsqr6inv r2inv*r2inv*r2invlj1 coeff[0]lj2 coeff[1]return (r6inv * (lj1*r6inv - lj2))*r2inv

Why do this?

Why do this?Because Python is Awesome!

Why do this?But Python is slow. . .

Performance hit250 timesteps LJ, 4000 atoms15.13run time in seconds151051.860C onlyPython only

Why do this?I Quick prototypingIYou don’t have to recompile LAMMPS to test itIYou don’t have to work in C IAnd you gain the flexibility / simplicity of the Python language to test new things

Workaround: Create a tabulated version# use python pair stylepair style python 2.5pair coeff * * py pot.LJCutMelt lj# generate tabulated potential from python variantpair write1 1 2000 rsq 0.01 2.5 lj 1 1.table LJ# switch pair style to tabulated potentialpair styletable linear 2000pair coeff1 1 lj 1 1.table LJ

Performance hit250 timesteps LJ, 4000 atoms15.13run time in seconds151051.861.99C onlytabulated Python0Python only

Fix PythonIlike any other fix it implements callback functions for certain events in integration loopIexecuted every N time stepsIcurrently limited to post force and end of stepfixfixfix1 all nve2 all python 50 end of step end of step callback3 all python 50 post force post force callback

Accessing LAMMPS from Pythonfrom lammps import lammpsdef end of step callback(lmp):L lammps(ptr lmp)t L.extract global("ntimestep", 0)print("### END OF STEP ###", t)def post force callback(lmp, v):L lammps(ptr lmp)t L.extract global("ntimestep", 0)print("### POST FORCE ###", t)

Python Interfaceslammps.lammpslammps.PyLammpsIuses C-TypesIIdirect memory access to native C datahigher-level abstraction built on top oforiginal C-Types interfaceImanipulation of Python objectsIprovides functions to send and receivedata to LAMMPSIcommunication with LAMMPS is hiddenfrom API userIrequires knowledge of how LAMMPSworksIshorter, more concise Python code

PyLammpsMotivationICreate a simpler, Python-like interface to LAMMPSIAPI should be discoverable (no knowlege of the C code necessary)IIPython notebook integrationUsagefrom lammps import PyLammpsL PyLammps()

CommandsLAMMPS Input Scriptregion box block 0 10 0 5 -0.5 0.5Original Python InterfaceL.command("region box block 0 10 0 5 -0.5 0.5")PyLammpsL.region("box block", 0, 10, 0, 5, -0.5, 0.5)

Commands - Easier parametrizationOriginal Python InterfaceL.command( \"region box block %f %f %f %f %f %f" % \(xlo, xh ylo, yhi, zlo, zhi) \)PyLammpsL.region("box block", xlo, xhi, ylo, yhi, zlo, zhi)

PyLammps interface ExampleLive Demo: python/examples/pylammps/interface usage.ipynb

Validating a Dihedral potentialLive Demo: python/examples/pylammps/dihedrals/dihedral.ipynb

Summary & OutlookWays to call Python from LAMMPSIcompute function mapped to variableIinvoke functionIpairwise potentials (pair python & pair write)Ifix python for end of step and post forceWays to access/control LAMMPS from PythonIlammps.lammpsIlammps.PyLammps (Jupyter Notebooks)Coming soonIfix python/integrate (implement fix nve in Python)Iread and write access to atom properties as numpy arrays

oc/Section python.htmlIhttp://lammps.sandia.gov/doc/tutorial pylammps.htmlExample FoldersI(Pair Python Fix Python): examples/pythonI(PyLammps and Python Interface): python/examples

: Questions?

Pair Python pair_style python 2.5 pair_coeff * * py_pot.LJCutMelt lj I (created by Dr. Axel Kohlmeyer) I for defining simple additive pair potentials in Python I PYTHONPATH and LAMMPS_POTENTIALS in module search path I loads class LJCutMelt from user-defined py_pot module I Python class implements compute_force and compute_energy functions I many examples, including hybrid usage in examples .

Related Documents:

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

Launch Eclipse Install Python plug-in for Eclipse Add a Python Interpreter Create a Python Project Create a Python Program Run a Python Program Debug a Python Program 0 Introduction This tutorial is for students who want to develop Python projects using Eclipse. E

A Python Book A Python Book: Beginning Python, Advanced Python, and Python Exercises Author: Dave Kuhlman Contact: dkuhlman@davekuhlman.org

Tom Sawyer’s observations of his environment and the people he encounters. In addition, students will make their own observations about key aspects of the novel, and use the novel and the journal writing activity to make observations about their own world and the people they are surrounded by. This unit plan will allow students to examine areas of Missouri, both in Hannibal, and in their own .