SME Python Shell



The SME jpython shell is a command & control component of the SME java portal. This shell provides a single environment for controlling simulation configuration, execution, visualization and output post-processing. Future versions will interface with globus/CoG kit/CCA/OPIE, etc. to integrate grid/portal functionality.

Invoking the shell

The SME can be started on the local machine with the python interface by invoking:

SME -python local run

Interface

The main Shell panel of the SME jpython interface consists of two panes, a jpyhton shell pane and a selection pane, as shown below ( click on image for an expanded view ).

Selection Pane

The selection pame is used to select & visualize simulation variables and actions and associate then with python objects which can be operated upon in the Python shell. Selecting a variable or action in the Selection pane and then clicking the "choose" button brings up a menu requesting an object name. Once a name is given, a Python object with that name (representing the selected variable or action) is created in the Python shell environment. Selecting a variable and clicking the "view" button brings up a viewer displaying the variable's data.

Shell Pane

The Python shell gives the user access to all the standard python functionality plus a selection of SME-specific commands described in the "Basic Shell Commands" section below.

It is also possible to create Python objects which represent simulation objects such as variables, actions, grids, and points. The available operations on these objects are described in the "SME Python Objects" section below. Python variable and action objects are created using the "choose" butt as described in the previous paragraph or by using the "choose" command in the Python shell as described in the "Interface Control" section below. Grid objects are created by calling Python variable "grid" method as described in the "Variable Object Methods" section below.

Examples of the use of various shell commands are provided in the "Python Shell Script Examples" section below.

Menus

The file menu has options for loading and saving Python scripts. The "Load script" option loads a script directly from disk into the shell. The "Script editor" option brings up a Python script editor which can be used to create new scripts froms scratch or edit existing scripts. The "Save Script" commands save the contents of the Python shell pane to a Python script file.

Basic Shell Commands

The SME python shell gives the user access to all the standard python functionality. In addition, the following SME-specific commands are available:

Simulation Configuration

conf( String cmd ): Execute configuration command cmd for the selected variable or module.
conf(): Start up the SME configuration panel.

Simulation Execution

open(): Open the current simulation ( called by first step() or run() command ).

step(): Step the simulation one timestep.

run( float t ): Run the simulation up to time t.

halt(): Pause a running simulation.

restart(): Restart the current simulation.

Simulation Visualization

view(): View data for selected variable.

display(): Pipe data for selected variable to ViewServer.

Interface Control

dep(): Add dependencies & action objects to selected variable.

eqn(): Display equation for selected action.

choose(): Returns a Python variable object representing the selected variable.

choose( String id ): As above but gives the Python variable object the name id.

SME Python Objects

Variable Object Methods

In the following, v, v0, & v1 represents Python variable objects.
v.view(): View data for variable v.

v.grid(): Returns a Python grid object representing variable v's grid.

v.dep(): Add dependencies to v.

v.setID(String id): Set variable's name to id.

v.config( String cmd ): Execute configuration command cmd for v.

v.actions(): Add Actions to v.

v.value( int index ): Returns value at grid point indexed by index.

v.value( Point p ): Returns value at grid point indexed by p.

v.data( int r, int c ): Returns value at grid point indexed by (r,c).

v.setValue( int index, float value ): Set value at grid point indexed by index.

v.setValue( Point p, float value ): Set value at grid point indexed by p.
 

For the following mathematical operations, a new variable object is returned with the same grid as the (spatial) operands, whose data represents the point-by-point (PbP) application of the mathematical operation to the operands. In most of the operations any of the operands can be replaced by interger or floating-point numbers. v0 + v1: Returns new variable object representing the addition of v0 and v1.
 
v0 - v1: PbP Subtraction of v1 from v0.

v0 * v1: PbP Multiplication of v0 and v1.

v0 / v1: PbP Division of v0 by v1.

v0 + v1: PbP Addition of v0 and v1.

v0 ^ v1: PbP Raise v0 to v1 power.

v0.le(v1): = ( 1 at points p where v0(p) <= v1(p); 0 otherwise )

v0.lt(v1): = ( 1 at points p where v0(p) < v1(p); 0 otherwise )

v0.ge(v1): = ( 1 at points p where v0(p) >= v1(p); 0 otherwise )

v0.gt(v1): = ( 1 at points p where v0(p) > v1(p); 0 otherwise )

v0.eq(v1): = ( 1 at points p where v0(p) = v1(p); 0 otherwise )

v0.or(v1): = ( 1 at points p where (v0(p) > 0) or (v1(p) > 0); 0 otherwise )

v0.and(v1): = ( 1 at points p where (v0(p) > 0) and (v1(p) > 0); 0 otherwise )
 

Action Object Methods

In the following, a represents a Python action object.
a.eqn(): Display the equation associated with action a.

Grid Object Methods

In the following, g represents a Python grid object.
g.n4(): Returns a PyList containing 4 nearest neighbor Point translations.

g.n8(): Returns a PyList containing 8 nearest neighbor Point translations.

g.trans( Point p, Point t ): Returns a Point representing p translated by t.

g.trans( Point p, int r, int c ): Returns a Point representing p translated by (r,c).

g.getPoint( int r, int c ): Returns Point object corresponding to (r,c)

g.var(String id, float fill): Returns a variable object with grid g, name id, and initial value fill.

Point Object Methods

In the following, p represents a Point object.
p.grid(): Returns p's grid.

p.row(): Returns p's row coordinate.

p.col(): Returns p's col coordinate.

p.index(): Returns p's grid index.

p.trans( Point t ): Returns a Point representing p translated by t.

p.trans( int r, int c ): Returns a Point representing p translated by (r,c).

p0 + p1: Returns a new point representing the addition of p0 and p1.

p0 - p1: Returns a new point representing the subtraction of p0 and p1.

Python Shell Script Examples

The following code defines a Python function which prints the coordinates of the 4 nearest neighbors of each point in Variable v's grid.

def test_grid( v ):

g = v.grid()
n = g.n4()
for p in g:
for pn in n:
print p + pn


The argument (v) to the function should be a Variable object. The object g is a grid. The object n is a list of the 4 nearest neighbor translations (which are Point objects with relative coordinates). Every grid object is also a list of point, so the Python command "for p in g:" iterates over the points in the grid. Once this code has been entered into the shell, the function "test_grid" can be executed, for example:

>>> test_grid( v0 )

This will print many lines of coordinates. The following slightly more complex example defines a function which computes a simple spatial average of a spatial variable.

def spatial_ave( v ):

g = v.grid()
v_ave = g.var("spatial_ave");
n = g.n8()
for p in g:
v_ave[p] = v[p]
neighbor_cnt = 1
for pn in n:
px = p + pn
if( px != None ):
v_ave[p] = v_ave[p] + v[ px ]
neighbor_cnt = neighbor_cnt + 1
v_ave[p] = v_ave[p]/neighbor_cnt
return v_ave
The argument (v) to the function should be a Variable object. The object v_ave is a Variable object with the same grid as v, with name "spatial_ave". The object g is a grid. The object n is a list of the 8 nearest neighbor translations. The operator v[p]returns the value of v at point p. If the translation yields a point that is not in the study area, then px is set to the Python null object None. Once this code has been entered into the shell, the function "test_grid" can be executed, for example:

>>> v1 = spatial_ave( v0 )