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
00023 #ifdef VMDIMD
00024
00025 #include "CmdIMD.h"
00026 #include "CommandQueue.h"
00027 #include "VMDApp.h"
00028 #include "MoleculeList.h"
00029 #include "IMDMgr.h"
00030
00031
00032 static PyObject *imdconnect(PyObject *self, PyObject *args, PyObject *keywds) {
00033
00034 char *host;
00035 int port;
00036
00037 static char *kwlist[] = {
00038 (char *)"host", (char *)"port", NULL
00039 };
00040
00041 if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"si", kwlist, &host, &port))
00042 return NULL;
00043
00044 VMDApp *app = get_vmdapp();
00045 Molecule *mol = app->moleculeList->top();
00046 if (!mol) {
00047 PyErr_SetString(PyExc_ValueError, (char *)"No molecule loaded");
00048 return NULL;
00049 }
00050 if (app->imdMgr->connected()) {
00051 PyErr_SetString(PyExc_ValueError, (char *)"Can't create new IMD connection: already connected.");
00052 return NULL;
00053 }
00054 if (!app->imd_connect(mol->id(), host, port)) {
00055 PyErr_SetString(PyExc_ValueError, (char *)"Unable to connect to IMD server");
00056 return NULL;
00057 }
00058 Py_INCREF(Py_None);
00059 return Py_None;
00060 }
00061
00062
00063 static PyObject *pause(PyObject *self, PyObject *args) {
00064 if (!PyArg_ParseTuple(args, (char *)""))
00065 return NULL;
00066 VMDApp *app = get_vmdapp();
00067 app->imdMgr->togglepause();
00068 app->commandQueue->runcommand(new CmdIMDSim(CmdIMDSim::PAUSE_TOGGLE));
00069 Py_INCREF(Py_None);
00070 return Py_None;
00071 }
00072
00073
00074 static PyObject *detach(PyObject *self, PyObject *args) {
00075 if (!PyArg_ParseTuple(args, (char *)""))
00076 return NULL;
00077 VMDApp *app = get_vmdapp();
00078 app->imdMgr->detach();
00079 app->commandQueue->runcommand(new CmdIMDSim(CmdIMDSim::DETACH));
00080 Py_INCREF(Py_None);
00081 return Py_None;
00082 }
00083
00084
00085 static PyObject *kill(PyObject *self, PyObject *args) {
00086 if (!PyArg_ParseTuple(args, (char *)""))
00087 return NULL;
00088 VMDApp *app = get_vmdapp();
00089 app->imdMgr->kill();
00090 app->commandQueue->runcommand(new CmdIMDSim(CmdIMDSim::KILL));
00091
00092 Py_INCREF(Py_None);
00093 return Py_None;
00094 }
00095
00096
00097 static PyObject *transfer(PyObject *self, PyObject *args, PyObject *keywds) {
00098
00099 int rate = -1;
00100 static char *kwlist[] = {
00101 (char *)"rate", NULL
00102 };
00103 if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"|i", kwlist, &rate))
00104 return NULL;
00105
00106 VMDApp *app = get_vmdapp();
00107 if (rate > 0) {
00108 app->imdMgr->set_trans_rate(rate);
00109 app->commandQueue->runcommand(
00110 new CmdIMDRate(CmdIMDRate::TRANSFER, rate));
00111 }
00112 return PyInt_FromLong(app->imdMgr->get_trans_rate());
00113 }
00114
00115
00116 static PyObject *keep(PyObject *self, PyObject *args, PyObject *keywds) {
00117
00118 int rate = -1;
00119 static char *kwlist[] = {
00120 (char *)"rate", NULL
00121 };
00122 if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"|i", kwlist, &rate))
00123 return NULL;
00124
00125 VMDApp *app = get_vmdapp();
00126 if (rate >= 0) {
00127 app->imdMgr->set_keep_rate(rate);
00128 app->commandQueue->runcommand(
00129 new CmdIMDRate(CmdIMDRate::KEEP, rate));
00130 }
00131 return PyInt_FromLong(app->imdMgr->get_keep_rate());
00132 }
00133
00134
00135
00136 static PyObject *copyunitcell(PyObject *self, PyObject *args) {
00137
00138 PyObject *boolobj;
00139 if (!PyArg_ParseTuple(args, (char *)"O:imd.copyunitcell", &boolobj))
00140 return NULL;
00141
00142 VMDApp *app = get_vmdapp();
00143 if (PyObject_IsTrue(boolobj)) {
00144 app->imdMgr->set_copyunitcell(1);
00145 app->commandQueue->runcommand(new CmdIMDCopyUnitCell(CmdIMDCopyUnitCell::COPYCELL_ON));
00146 } else {
00147 app->imdMgr->set_copyunitcell(0);
00148 app->commandQueue->runcommand(new CmdIMDCopyUnitCell(CmdIMDCopyUnitCell::COPYCELL_OFF));
00149 }
00150 return Py_None;
00151 }
00152
00153 static PyObject *imdconnected(PyObject *self, PyObject *args) {
00154 if (!PyArg_ParseTuple(args, (char *)"")) return NULL;
00155 VMDApp *app = get_vmdapp();
00156 return Py_BuildValue( "O",
00157 app->imdMgr->connected() ? Py_True : Py_False );
00158 }
00159
00160 static PyMethodDef methods[] = {
00161 {(char *)"connected", (vmdPyMethod)imdconnected, METH_VARARGS,
00162 (char *)"connected() -- True/False" },
00163 {(char *)"connect", (PyCFunction)imdconnect, METH_VARARGS | METH_KEYWORDS,
00164 (char *)"connect(host, port) -- establish IMD connection simulation on host:port"},
00165 {(char *)"pause", (vmdPyMethod)pause, METH_VARARGS,
00166 (char *)"pause() -- pause a running IMD simulation"},
00167 {(char *)"detach", (vmdPyMethod)detach, METH_VARARGS,
00168 (char *)"detach() -- detach from a running IMD simulation"},
00169 {(char *)"kill", (vmdPyMethod)kill, METH_VARARGS,
00170 (char *)"kill() -- halt a running IMD simulation (also detaches)"},
00171 {(char *)"transfer", (PyCFunction)transfer,METH_VARARGS | METH_KEYWORDS,
00172 (char *)"transfer(rate = -1) -- set/get how often timesteps are sent"},
00173 {(char *)"keep", (PyCFunction)keep, METH_VARARGS | METH_KEYWORDS,
00174 (char *)"keep(rate = -1) -- set/get how often timesteps are saved "},
00175 {(char *)"copyunitcell", (PyCFunction)copyunitcell, METH_VARARGS,
00176 (char *)"copyunitcell(True/False) -- copy unitcell information from previous frame"},
00177 {NULL, NULL}
00178 };
00179
00180 #else
00181
00182
00183 static PyMethodDef methods[] = {
00184 {NULL, NULL}
00185 };
00186 #endif
00187
00188 void initimd() {
00189 (void) Py_InitModule((char *)"imd", methods);
00190 }
00191