00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00029 extern "C" {
00030
00031
00032
00033
00034
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;
00043 int frame = -1;
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;
00064 int frame = -1;
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
00082
00083
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
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