Technical Documentation
SCRIPTING THE YAMMP MODULE: Python

This page presents just enough Python to get a novice into trouble.

Starting

On graphical systems activate the Python application, typically by double-clicking on the icon. On command line systems, type python (usually). In both cases you will get a copyright notice and a prompt (usually >>>)

despina 1% python
Python 1.6a2 (#10, Jul 12 2000, 11:59:05) [C] on irix646
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> 

Operators

The standard arithmetic and logical operators are available. Operators may be overloaded, e.g. + applies to numbers and strings but with different meanings. Results of logical operations are 0 for false and 1 for true. Note the multiple comparisons in the last example. It is legal and correct.

>>> ( 3.5 + 4.1 * 3 ) / 2
7.8999999999999995
>>> "a" + "bc"
'abc'
>>> ( 1 - 3j ) * ( 1 + 3j )
(10+0j)
>>> 100 > 1000
0
>>> 100 < 1000
1
>>> 1 < 2 < 3
1

Variables

Variables can be given names of any length. The names must start with a letter or underscore followed by more letters, underscore or digits. Names are case-sensitive. Names with certain combinations of leading or final underscores are treated specially by Python. You need not declare variables, they are created on the fly.

>>> deg_F = 98
>>> deg_C = ( deg_F - 32 ) * 5 / 9
>>> deg_C
36

Tuples

Python has complex data types and one of these is called a tuple. It is like an array but a tuple may contain items of different types and even other complex data. The items of a tuple are enclosed by parentheses.

>>> person = ( "Name", 55, ( 100000, "Job" ) )
>>> person[0]
'Name'
>>> person[2]
(100000, 'Job')

Output

To print the value of an object, just write its name. Or use the simple print statement: just list what you want to print and print will space the items and terminate the line with a carriage return. You can also tailor your output using format strings and the % operator.

>>> deg_F
98
>>> print deg_F, "degree F =", deg_C, "degree C"
98 degree F = 36 degree C
>>> print "%g degree F = %g degree C" % ( deg_F, deg_C )
98 degree F = 36 degree C

Conditionals

Note the colon at the end of each condition and the code indentation to set off blocks. Note also the secondary prompt of three dots and the empty line to signify the end of the block. The empty line is needed only in interactive use.

>>> if deg_C > deg_F:
...     print deg_C, "deg C is greater than", deg_F, "deg F"
... else:
...     print deg_C, "deg C is less than or equal to", deg_F, "deg F"
... 
36 deg C is less than or equal to 98 deg F

Loops

Example of use of the for loop. range() is a built-in function that returns objects in the given range. There is also a while loop.

>>> total = 0
>>> for count in range( 5 ):
...     total = total + count
...     print "Total now is", total
... 
Total now is 0
Total now is 1
Total now is 3
Total now is 6
Total now is 10

Functions and Modules

Functions are defined interactively or in a separate file in which case the file becomes a module. The following is contained in a file named metrics.py. Note the indentation of the function body and the further indentation of the body of the for loop. The formal arguments are named in the header line which ends with a colon. There are two arguments, both of which are assumed to be tuples of length 3. The second argument has a predefined value. This means that the function can be used without specifying the second point in which case you will get the distance to the origin. The quoted string right after the function header is called the documentation string. This is only an illustration; a real function should have error checking.

metrics.py
def distance( a, b = ( 0., 0., 0. ) ):
        "distance( a, b ) - distance between points a and b"
        import math
        d = a[0] - b[0]
        s = d * d
        for i in ( 1, 2 ):
                d = a[i] - b[i]
                s = s + d * d
        return math.sqrt( s )

Function call

From the last example, this is how we use a function. First, we import the function from the module. Note we can print the documentation string - in this case a brief reminder on usage. Note the second use of the function using the default values for the second point.

>>> from metrics import distance
>>> print distance.__doc__
distance( a, b ) - distance between points a and b 
>>> distance( ( 1., 2.5, 0. ), ( 0.1, 3., 2.1 ) )
2.3388031127053002
>>> distance( ( 3., 4., 5. ) )
7.0710678118654755

Functions can be defined interactively, in which case we do not have to import a module. Modules may be written in Python as in this example, or they may be written in C and compiled. Both types of modules are used in the same way. The yammp module is written in C.

Stopping

If you are running a script, Python will automatically quit at the end of your script. In an interactive session, you can type the end-of-file indicator (Control-D on UNIX) or call the exit() function from the sys module:

>>> import sys
>>> sys.exit()
despina 2% 

Introduction
Python
Examples

setup()
setenviron()
editdynamics()
editminimizers()
editlinesearch()
editmontecarlo()
editnonbonds()
editconstraint()
editcontra()
Run

Technical
Introduction
Directory
Vectors
Energy
Model
Assembly
Methods
FPF
FFF
AVF
TaroScript
YammpScript
Python
Utilities

Home
Information
News
User
Technical
Programmer
iYup
Download
Showcase
ETC