00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <string.h>
00023 #include "vmdsock.h"
00024 #include "IMDMgr.h"
00025 #include "IMDSimBlocking.h"
00026 #include "Inform.h"
00027
00028 IMDSimBlocking::IMDSimBlocking(const char *host, int port)
00029 : IMDSim(host, port) {
00030 curpos = NULL;
00031 }
00032
00033 IMDSimBlocking::~IMDSimBlocking() {
00034 delete [] curpos;
00035 }
00036
00037 void IMDSimBlocking::update() {
00038 if (!isConnected()) return;
00039 IMDType type;
00040 int32 length;
00041 while (isConnected() && vmdsock_selread(sock,0)) {
00042 type = imd_recv_header(sock, &length);
00043 switch (type) {
00044 case IMD_FCOORDS: process_coordinates(length); break;
00045 case IMD_ENERGIES: process_energies(length); break;
00046 case IMD_MDCOMM: process_mdcomm(length); break;
00047 case IMD_IOERROR: disconnect(); break;
00048 default: break;
00049 }
00050 }
00051 }
00052
00053 void IMDSimBlocking::process_coordinates(int32 length) {
00054 if (numcoords < length) {
00055 delete [] curpos;
00056 curpos = new float[3L*length];
00057 }
00058 numcoords = length;
00059 if (imd_recv_fcoords(sock, numcoords, curpos)) {
00060 msgErr << "Error reading remote coordinates!" << sendmsg;
00061 disconnect();
00062 } else {
00063 new_coords_ready = 1;
00064 }
00065 }
00066
00067 void IMDSimBlocking::process_energies(int32 ) {
00068 if (imd_recv_energies(sock, &imdEnergies)) {
00069 msgErr << "Error reading energies!" << sendmsg;
00070 disconnect();
00071 }
00072 else {
00073 if (need2flip) swap4_aligned(&imdEnergies, sizeof(imdEnergies) / 4);
00074 }
00075 }
00076
00077
00078 void IMDSimBlocking::process_mdcomm(int32 length) {
00079 int32 *ind = new int32[length];
00080 float *f = new float[3L*length];
00081 if (imd_recv_mdcomm(sock, length, ind, f)) {
00082 msgErr << "Error reading MDComm-style forces!" << sendmsg;
00083 disconnect();
00084 }
00085 delete [] ind;
00086 delete [] f;
00087 }
00088
00089 void IMDSimBlocking::get_next_ts(float *pos, IMDEnergies *buf) {
00090 new_coords_ready = 0;
00091 memcpy(pos, curpos, 3L*numcoords*sizeof(float));
00092 memcpy(buf, &imdEnergies, sizeof(IMDEnergies));
00093 if (need2flip) swap4_aligned(pos, 3L*numcoords);
00094 }
00095
00096 void IMDSimBlocking::send_forces(int num, int *ind, float *forces) {
00097
00098 if (!isConnected()) return;
00099 if (need2flip) {
00100 swap4_aligned(ind, num);
00101 swap4_aligned(forces, 3L*num);
00102 }
00103 if (imd_send_mdcomm(sock, num, ind, forces)) {
00104 msgErr << "Error sending MDComm indices+forces" << sendmsg;
00105 disconnect();
00106 }
00107 }
00108
00109 void IMDSimBlocking::pause() {
00110 if (isConnected() && (getSimState() == IMDRUNNING)) {
00111 simstate = IMDOFFLINE;
00112 imd_pause(sock);
00113 }
00114 }
00115
00116 void IMDSimBlocking::unpause() {
00117 if (isConnected() && (getSimState() == IMDPAUSED)) {
00118 simstate = IMDRUNNING;
00119 imd_pause(sock);
00120 }
00121 }
00122
00123 void IMDSimBlocking::detach() {
00124 if (isConnected()) {
00125 simstate = IMDOFFLINE;
00126 imd_disconnect(sock);
00127 }
00128 disconnect();
00129 }
00130
00131 void IMDSimBlocking::kill() {
00132 if (isConnected()) {
00133 simstate = IMDOFFLINE;
00134 imd_kill(sock);
00135 }
00136 disconnect();
00137 }
00138
00139 void IMDSimBlocking::set_transrate(int rate) {
00140 if (isConnected())
00141 imd_trate(sock, rate);
00142 }
00143