Introduction to iPython

Robert D. French
User Support Specialist
Oak Ridge Leadership Computing Facility
Email: frenchrd@ornl.gov

  • MATLAB / Mathematica style interactive IDE
  • Good for prototyping / verifying new codes
  • Good for analyzing output from HPC codes
    • Easy plots & charts
    • Basic stats and data management capabilities

In [100]:
x = linspace(-10, 10., 1000)
plot(x, sin(x))


Out[100]:
[<matplotlib.lines.Line2D at 0xaf36ef0>]

In [101]:
bins = [n/10.0 for n in range(0,11)]
hist(rand(100),bins);


Setup

  • All-in-one Distributions are easiest:
    • Canopy by Enthought (Good for OS X)
    • Linux distros, check repo for iPython

Notebooks

  • Cell-based programming makes learning easy
  • Distribute content to students / colleagues easily
  • Display graphics and results inline
  • Supports Markdown for text styling
  • Notebooks are just JSON documents!

In [59]:
import json
notebook = json.load(open("/Users/rf9/Projects/iPython-Molecular-Dynamics/presentation.ipynb"))
notebook["worksheets"][0]["cells"][4]


Out[59]:
{u'cell_type': u'markdown',
 u'metadata': {},
 u'source': [u'# Notebooks\n',
  u'\n',
  u'* Cell-based programming makes learning easy\n',
  u'* Distribute content to students / colleagues easily\n',
  u'* Display graphics and results inline\n',
  u'* Supports [Markdown](http://daringfireball.net/projects/markdown/) for text styling\n',
  u'* Notebooks are just JSON documents!']}

Vector Arithmetic

Vectors can be created by passing a python list to the array() function


In [43]:
x = array([1,2,3])
x


Out[43]:
array([1, 2, 3])

Vector addition is now supported via the + operator


In [64]:
y = array([1,0,1])
x + y


Out[64]:
array([2, 2, 4])

Standard tools from linear algebra such as Euclidean inner products and norms


In [65]:
x.dot(y)


Out[65]:
4

In [66]:
numpy.linalg.norm(x - y)


Out[66]:
2.8284271247461903

Scipy functions are automatically applied to each element in the list:


In [36]:
sin(x)**2


Out[36]:
array([ 0.70807342,  0.82682181,  0.01991486])

This includes any operation of scalar arithmetic, and extends to everyone's favorite trig identity:


In [37]:
sin(x)**2 + cos(x)**2


Out[37]:
array([ 1.,  1.,  1.])

Example Use Case: Verifying a new MD Code

  • Prototyped a simple MD system in Python (available here)
  • Want to verify that output is correct
  • We can use the Pandas library to analyze the data

Example 1: Particle B drags Particle A

  • 2 particle system, simple spring potential
  • Particle A has zero initial velocity
  • Particle B has small initial velocity along y-axis
  • Particle B should "drag" Particle A

Step 1: Verify the input file


In [69]:
input_config = json.load(open("/Users/rf9/Projects/iPython-Molecular-Dynamics/ExampleInput.dragging.json"))
input_config["particles"]


Out[69]:
[{u'mass': 1, u'position': [0, 0, 0], u'velocity': [0.0, 0.0, 0.0]},
 {u'mass': 1, u'position': [0, 1, 0], u'velocity': [0.0, 0.5, 0.0]}]

Step 2: Verify the structure of the output file


In [74]:
import pandas
output = pandas.read_csv("/Users/rf9/Projects/iPython-Molecular-Dynamics/dragging_output.csv")
output.head()


Out[74]:
time x1 y1 z1 x2 y2 z2
0 0.00 0 0.000000 0 0 1.000000 0
1 0.01 0 0.000000 0 0 1.005000 0
2 0.02 0 0.000000 0 0 1.009999 0
3 0.03 0 0.000002 0 0 1.014998 0
4 0.04 0 0.000005 0 0 1.019995 0

Step 3: Visualize the output data


In [90]:
plot(output["time"],output["y1"], label='Particle A')
plot(output["time"],output["y2"], label='Particle B')
legend(loc='upper left')


Out[90]:
<matplotlib.legend.Legend at 0xdb0ee70>

Example 2: Particles Oscillate "in place"

  • 2 particle system, simple spring potential
  • Particles have same initial velocity, but in opposite directions
  • Particles should oscillate with no net change in position

Step 1: Verify Input data


In [91]:
input_config = json.load(open("/Users/rf9/Projects/iPython-Molecular-Dynamics/ExampleInput.oscillation.json"))
input_config["particles"]


Out[91]:
[{u'mass': 1, u'position': [0, 0, 0], u'velocity': [0.0, -0.5, 0.0]},
 {u'mass': 1, u'position': [0, 1, 0], u'velocity': [0.0, 0.5, 0.0]}]

Step 2: Verify structure of output data


In [92]:
output = pandas.read_csv("/Users/rf9/Projects/iPython-Molecular-Dynamics/oscillation_output.csv")
output.head()


Out[92]:
time x1 y1 z1 x2 y2 z2
0 0.00 0 0.000000 0 0 1.000000 0
1 0.01 0 -0.005000 0 0 1.005000 0
2 0.02 0 -0.009999 0 0 1.009999 0
3 0.03 0 -0.014996 0 0 1.014996 0
4 0.04 0 -0.019990 0 0 1.019990 0

Step 3: Visualize data


In [93]:
plot(output["time"],output["y1"], label='Particle A')
plot(output["time"],output["y2"], label='Particle B')
legend(loc='upper left')


Out[93]:
<matplotlib.legend.Legend at 0xdbcfbf0>

Step 4: Calculate difference between average position and initial position


In [98]:
mean(output["y1"]) - input_config["particles"][0]["position"][1]


Out[98]:
-0.025606218562874289

In [99]:
mean(output["y2"]) - input_config["particles"][1]["position"][1]


Out[99]:
0.025606218562873595

In [ ]: