Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

py_numeric.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2011 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: py_numeric.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.20 $       $Date: 2011/03/05 04:57:37 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *  Python interface to the Python Numeric module.
00019  ***************************************************************************/
00020 
00021 #include "py_commands.h"
00022 #include "numpy/ndarrayobject.h"
00023 
00024 #include "AtomSel.h"
00025 #include "VMDApp.h"
00026 #include "MoleculeList.h"
00027 
00028 // make python happier
00029 extern "C" {
00030 
00031 /* 
00032  * Note: changes to the timestep method now return an N x 3 array
00033  * rather than flat 3N arrays.  This may break older plugins like IED
00034  * if they are not kept up-to-date with VMD.
00035  */ 
00036 static char timestep_doc[] = 
00037   "timestep(molid = -1, frame = -1) -> NumPy (N x 3) float32 array\n"
00038   "Returns zero-copy reference to atom coordinates.  molid defaults to\n"
00039   "top molecule; frame defaults to current frame.  Array has shape\n"
00040   "N x 3 where N is the number of atoms in the molecule.";
00041 static PyObject *timestep(PyObject *self, PyObject *args, PyObject *kwds) {
00042   int molid = -1;  // default : top
00043   int frame = -1;  // default : current frame 
00044   static char *kwlist[] = { (char *)"molid", (char *)"frame", NULL };
00045   if (!PyArg_ParseTupleAndKeywords(args, kwds, (char *)"|ii", kwlist,
00046         &molid, &frame))
00047     return NULL;
00048   Timestep *ts = parse_timestep(get_vmdapp(), molid, frame);
00049   if (!ts) return NULL;
00050   npy_intp dims[2] = { ts->num, 3 };
00051   PyArrayObject *result = (PyArrayObject *)PyArray_SimpleNewFromData(
00052       2, dims, NPY_FLOAT, (char *)ts->pos);
00053   return PyArray_Return(result);
00054 }
00055 
00056 static char velocities_doc[] = 
00057   "velocities(molid = -1, frame = -1) -> NumPy (N x 3) float32 array\n"
00058   "Returns zero-copy reference to atom velocities.  molid defaults to\n"
00059   "top molecule; frame defaults to current frame.  Array has shape\n"
00060   "N x 3 where N is the number of atoms in the molecule.  If timestep\n"
00061   "holds no velocities, None is returned.";
00062 static PyObject *velocities(PyObject *self, PyObject *args, PyObject *kwds) {
00063   int molid = -1;  // default : top
00064   int frame = -1;  // default : current frame 
00065   static char *kwlist[] = { (char *)"molid", (char *)"frame", NULL };
00066   if (!PyArg_ParseTupleAndKeywords(args, kwds, (char *)"|ii", kwlist,
00067         &molid, &frame))
00068     return NULL;
00069   Timestep *ts = parse_timestep(get_vmdapp(), molid, frame);
00070   if (!ts) return NULL;
00071   if (!ts->vel) {
00072     Py_INCREF(Py_None);
00073     return Py_None;
00074   }
00075   npy_intp dims[2] = { ts->num, 3 };
00076   PyArrayObject *result = (PyArrayObject *)PyArray_SimpleNewFromData(
00077       2, dims, NPY_FLOAT, (char *)ts->vel);
00078   return PyArray_Return(result);
00079 }
00080  
00081 // Return an array of ints representing flags for on/off atoms in an atom
00082 // selection.
00083 // atomselect(molid, frame, selection) -> array
00084 static PyObject *atomselect(PyObject *self, PyObject *args) {
00085   int molid = 0;
00086   int frame = 0;
00087   char *sel = 0;
00088   if (!PyArg_ParseTuple(args, (char *)"iis", &molid, &frame, &sel))
00089     return NULL;
00090   VMDApp *app = get_vmdapp();
00091   DrawMolecule *mol = app->moleculeList->mol_from_id(molid);
00092   if (!mol) {
00093     PyErr_SetString(PyExc_ValueError, "invalid molid");
00094     return NULL;
00095   }
00096 
00097   AtomSel *atomSel = new AtomSel(app->atomSelParser, mol->id());
00098   atomSel->which_frame = frame;
00099   if (atomSel->change(sel, mol) == AtomSel::NO_PARSE) {
00100     PyErr_SetString(PyExc_ValueError, "cannot parse atom selection text");
00101     delete atomSel;
00102     return NULL;
00103   }
00104   npy_intp dims[1]; dims[0] = mol->nAtoms;
00105   PyArrayObject *result = (PyArrayObject *)PyArray_SimpleNew(
00106       1, dims, NPY_INT);
00107   memcpy(result->data, atomSel->on, mol->nAtoms*sizeof(int));
00108   delete atomSel;
00109   return PyArray_Return(result);
00110 }
00111 
00112 // end of extern "C" block
00113 }
00114 
00115 static PyMethodDef Methods[] = {
00116   {(char *)"timestep", (PyCFunction)timestep, METH_VARARGS | METH_KEYWORDS, timestep_doc},
00117   {(char *)"positions", (PyCFunction)timestep, METH_VARARGS | METH_KEYWORDS, timestep_doc},
00118   {(char *)"velocities", (PyCFunction)velocities, METH_VARARGS | METH_KEYWORDS, velocities_doc},
00119   {(char *)"atomselect", atomselect, METH_VARARGS, (char *)"Create atom selection flags"},
00120   {NULL, NULL}
00121 };
00122 
00123 void initvmdnumpy() {
00124   if (_import_array() < 0) {
00125     PyErr_SetString(PyExc_ValueError, "vmdnumpy module not available.");
00126     return;
00127   }
00128   (void)Py_InitModule((char *)"vmdnumpy", Methods);
00129 }
00130 

Generated on Tue May 21 01:46:40 2013 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002