%module vmdimd_raw %{ #define SWIG_FILE_WITH_INIT #include "imd.h" #include "vmdsock.h" #if defined(_WIN32) || defined(__WIN32__) # include # define SLEEP(x) (Sleep((x) * 1000.0)) #else # define SLEEP(x) (sleep((x))) #endif %} %inline %{ static void *clientsock=NULL; static int tryConnection=0; static int tryingConnection=0; void makeConnection(int port) { void *sock; int length; Py_BEGIN_ALLOW_THREADS tryConnection=1; tryingConnection=1; vmdsock_init(); sock = vmdsock_create(); vmdsock_bind(sock, port); vmdsock_listen(sock); clientsock = NULL; while (!clientsock) { SLEEP(0.1); if (tryConnection==0) break; if (vmdsock_selread(sock, 0) > 0) { clientsock = vmdsock_accept(sock); if (imd_handshake(clientsock)) { clientsock = NULL; }; } } if (clientsock && tryConnection==1) { SLEEP(1); if (vmdsock_selread(clientsock, 0) != 1 || imd_recv_header(clientsock, &length) != IMD_GO) { clientsock = NULL; } } tryingConnection=0; Py_END_ALLOW_THREADS } void stopTryingConnection() { tryConnection=0; } int isConnected() { if (!clientsock) return 0; return 1; } void closeConnection() { tryConnection=0; while (tryingConnection==1) { SLEEP(0.05); } if (clientsock) { vmdsock_shutdown(clientsock); vmdsock_destroy(clientsock); } Py_BEGIN_ALLOW_THREADS SLEEP(1); Py_END_ALLOW_THREADS } void sendEnergies(int tstep, float T, float Etot, float Epot, float Evdw, float Eelec, float Ebond, float Eangle, float Edihe, float Eimpr) { IMDEnergies e; if (clientsock) { e.tstep=tstep; e.T=T; e.Etot=Etot; e.Epot=Epot; e.Evdw=Evdw; e.Eelec=Eelec; e.Ebond=Ebond; e.Eangle=Eangle; e.Edihe=Edihe; e.Eimpr=Eimpr; imd_send_energies(clientsock, &e); } } void sendCoords(PyObject *coords) { int i, numAtoms; PyObject *xyz; float *buf; if (clientsock) { numAtoms = PyList_Size(coords); buf = (float *) malloc(sizeof(float) * 3*numAtoms); for (i=0; i