NAMD
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
GlobalMasterIMD Class Reference

#include <GlobalMasterIMD.h>

Inheritance diagram for GlobalMasterIMD:
GlobalMaster

Public Member Functions

 GlobalMasterIMD ()
 
 ~GlobalMasterIMD ()
 
void send_energies (IMDEnergies *)
 
void send_fcoords (int, FloatVector *)
 
- Public Member Functions inherited from GlobalMaster
void processData (AtomIDList::iterator a_i, AtomIDList::iterator a_e, PositionList::iterator p_i, PositionList::iterator g_i, PositionList::iterator g_e, BigRealList::iterator gm_i, BigRealList::iterator gm_e, ForceList::iterator gtf_i, ForceList::iterator gtf_e, IntList::iterator goi_i, IntList::iterator goi_e, BigRealList::iterator gov_i, BigRealList::iterator gov_e, AtomIDList::iterator last_atoms_forced_i, AtomIDList::iterator last_atoms_forced_e, ForceList::iterator last_forces_i, AtomIDList::iterator, AtomIDList::iterator, ForceList::iterator)
 
bool changedAtoms ()
 
const AtomIDListrequestedAtoms ()
 
bool changedForces ()
 
const AtomIDListforcedAtoms ()
 
const ForceListappliedForces ()
 
bool changedGroups ()
 
const ResizeArray< AtomIDList > & requestedGroups ()
 
const ForceListgroupForces ()
 
bool changedGridObjs ()
 
const IntListrequestedGridObjs ()
 
const BigRealListgridObjForces ()
 
bool requestedTotalForces ()
 
void clearChanged ()
 
virtual ~GlobalMaster ()
 
void check () const
 
void setLattice (const Lattice *lat)
 

Protected Member Functions

virtual void calculate ()
 
void get_vmd_forces ()
 
- Protected Member Functions inherited from GlobalMaster
 GlobalMaster ()
 
AtomIDListmodifyRequestedAtoms ()
 
AtomIDListmodifyForcedAtoms ()
 
ForceListmodifyAppliedForces ()
 
ResizeArray< AtomIDList > & modifyRequestedGroups ()
 
ForceListmodifyGroupForces ()
 
IntListmodifyRequestedGridObjects ()
 
BigRealListmodifyGridObjForces ()
 
AtomIDList::const_iterator getAtomIdBegin ()
 
AtomIDList::const_iterator getAtomIdEnd ()
 
PositionList::const_iterator getAtomPositionBegin ()
 
PositionList::const_iterator getGroupPositionBegin ()
 
PositionList::const_iterator getGroupPositionEnd ()
 
ForceList::const_iterator getGroupTotalForceBegin ()
 
ForceList::const_iterator getGroupTotalForceEnd ()
 
IntList::const_iterator getGridObjIndexBegin ()
 
IntList::const_iterator getGridObjIndexEnd ()
 
BigRealList::const_iterator getGridObjValueBegin ()
 
BigRealList::const_iterator getGridObjValueEnd ()
 
AtomIDList::const_iterator getLastAtomsForcedBegin ()
 
AtomIDList::const_iterator getLastAtomsForcedEnd ()
 
ForceList::const_iterator getLastForcesBegin ()
 
AtomIDList::const_iterator getForceIdBegin ()
 
AtomIDList::const_iterator getForceIdEnd ()
 
ForceList::const_iterator getTotalForce ()
 
void requestTotalForce (bool yesno=true)
 
BigRealList::const_iterator getGroupMassBegin ()
 
BigRealList::const_iterator getGroupMassEnd ()
 

Protected Attributes

int IMDwait
 
int IMDignore
 
int IMDignoreForces
 
voidsock
 
ResizeArray< void * > clients
 
float * coordtmp
 
int coordtmpsize
 
- Protected Attributes inherited from GlobalMaster
bool totalForceRequested
 
const Latticelattice
 
AtomIDList::iterator atomIdBegin
 
AtomIDList::iterator atomIdEnd
 
PositionList::iterator atomPositionBegin
 
PositionList::iterator groupPositionBegin
 
PositionList::iterator groupPositionEnd
 
BigRealList::iterator groupMassBegin
 
BigRealList::iterator groupMassEnd
 
ForceList::iterator groupTotalForceBegin
 
ForceList::iterator groupTotalForceEnd
 
IntList::iterator gridObjIndexBegin
 
IntList::iterator gridObjIndexEnd
 
BigRealList::iterator gridObjValueBegin
 
BigRealList::iterator gridObjValueEnd
 
AtomIDList::iterator lastAtomsForcedBegin
 
ForceList::iterator lastForcesBegin
 
AtomIDList::iterator lastAtomsForcedEnd
 
AtomIDList::iterator forceIdBegin
 
AtomIDList::iterator forceIdEnd
 
ForceList::iterator totalForceBegin
 
bool reqAtomsChanged
 
AtomIDList reqAtoms
 
bool appForcesChanged
 
AtomIDList fAtoms
 
ForceList appForces
 
bool reqGroupsChanged
 
ResizeArray< AtomIDListreqGroups
 
ForceList grpForces
 
bool reqGridObjsChanged
 
IntList reqGridObjs
 
BigRealList gridobjForces
 

Friends

class IMDOutput
 

Additional Inherited Members

- Public Attributes inherited from GlobalMaster
int step
 
int old_num_groups_requested
 

Detailed Description

Definition at line 16 of file GlobalMasterIMD.h.

Constructor & Destructor Documentation

GlobalMasterIMD::GlobalMasterIMD ( )

Definition at line 53 of file GlobalMasterIMD.C.

References coordtmp, coordtmpsize, DebugM, endi(), find_free_port(), iINFO(), Node::imd, IMDignore, SimParameters::IMDignore, IMDignoreForces, SimParameters::IMDignoreForces, SimParameters::IMDport, IMDwait, SimParameters::IMDwait, iout, iWARN(), NAMD_die(), Node::Object(), GlobalMaster::requestedGroups(), Node::simParameters, sock, IMDOutput::use_imd(), vmdsock_create(), vmdsock_destroy(), vmdsock_init(), and vmdsock_listen().

53  {
54  DebugM(3,"Constructing\n");
56  int port = simparams->IMDport;
57  IMDwait = simparams->IMDwait;
58  IMDignore = simparams->IMDignore;
59  IMDignoreForces = simparams->IMDignoreForces;
60  coordtmp = NULL;
61  coordtmpsize = 0;
62 
63  if ( vmdsock_init() ) {
64  NAMD_die("Unable to initialize socket interface for IMD.\n");
65  }
66  sock = vmdsock_create();
67  int newport = find_free_port(sock, port);
68  if (newport != port) {
69  iout << iWARN << "Interactive MD failed to bind to port "
70  << port << ".\n" << endi;
71  }
72  if (newport < 0) {
74  NAMD_die("Interactive MD failed to find free port.\n");
75  }
77  iout << iINFO << "Interactive MD listening on port "
78  << newport << ".\n" << endi;
79  DebugM(2,"Done constructing ("<<requestedGroups().size()<<" initial groups)\n");
80 
81  Node::Object()->imd->use_imd(this);
82 }
static Node * Object()
Definition: Node.h:86
void * vmdsock_create(void)
Definition: vmdsock.C:76
std::ostream & iINFO(std::ostream &s)
Definition: InfoStream.C:107
static int find_free_port(void *sock, int defport)
IMDOutput * imd
Definition: Node.h:183
SimParameters * simParameters
Definition: Node.h:178
#define DebugM(x, y)
Definition: Debug.h:59
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:108
#define iout
Definition: InfoStream.h:87
const ResizeArray< AtomIDList > & requestedGroups()
Definition: GlobalMaster.C:149
int vmdsock_init(void)
Definition: vmdsock.C:52
int vmdsock_listen(void *v)
Definition: vmdsock.C:123
void NAMD_die(const char *err_msg)
Definition: common.C:83
void vmdsock_destroy(void *v)
Definition: vmdsock.C:164
infostream & endi(infostream &s)
Definition: InfoStream.C:38
void use_imd(GlobalMasterIMD *)
Definition: IMDOutput.C:19
GlobalMasterIMD::~GlobalMasterIMD ( )

Definition at line 84 of file GlobalMasterIMD.C.

References clients, coordtmp, ResizeArray< T >::size(), sock, and vmdsock_destroy().

84  {
85  if (sock)
87  for (int i=0; i<clients.size(); i++)
89  delete [] coordtmp;
90 }
ResizeArray< void * > clients
void vmdsock_destroy(void *v)
Definition: vmdsock.C:164
int size(void) const
Definition: ResizeArray.h:127

Member Function Documentation

void GlobalMasterIMD::calculate ( )
protectedvirtual

Reimplemented from GlobalMaster.

Definition at line 108 of file GlobalMasterIMD.C.

References ResizeArray< T >::add(), ResizeArray< Elem >::begin(), clients, DebugM, endi(), get_vmd_forces(), iINFO(), IMDignore, IMDignoreForces, IMDwait, SortedArray< Elem >::index(), iout, ResizeArray< T >::item(), iWARN(), GlobalMaster::modifyAppliedForces(), GlobalMaster::modifyForcedAtoms(), GlobalMaster::modifyGroupForces(), my_imd_connect(), ResizeArray< T >::resize(), ResizeArray< Elem >::size(), ResizeArray< T >::size(), sock, vmdsock_accept(), vmdsock_destroy(), and vmdsock_selread().

Referenced by IMDOutput::gather_coordinates().

108  {
109  /* clear out the requested forces first! */
110  if (!IMDignore && !IMDignoreForces) {
114  }
115 
116  // check for incoming connection
117  do {
118  int rc;
119  if (IMDwait && !clients.size()) {
120  iout << iINFO << "INTERACTIVE MD AWAITING CONNECTION\n" << endi;
121  do { rc = vmdsock_selread(sock, 3600); } while (rc <= 0);
122  } else {
123  rc = vmdsock_selread(sock, 0);
124  }
125  if (rc > 0) {
126  void *clientsock = vmdsock_accept(sock);
127  if (!clientsock) {
128  iout << iWARN << "IMD socket accept failed\n" << endi;
129  } else {
130  if (!my_imd_connect(clientsock)) {
131  iout << iWARN << "IMD connection failed\n" << endi;
132  vmdsock_destroy(clientsock);
133  } else {
134  iout << iINFO << "IMD connection opened\n" <<endi;
135  clients.add(clientsock);
136  }
137  }
138  }
139  } while (IMDwait && !clients.size());
140 
141  // Assume for now that the only thing we get from VMD is a set of forces.
142  // Later we'll want to look for and implement more sophisticated control
143  // parameters. ie have a specified protocol
144 
145  // Check/get new forces from VMD
146  get_vmd_forces();
147 
148  // Right now I don't check to see if any new forces were obtained.
149  // An optimization would be cache the results message. However, there
150  // would still be copying since it looks like the messages get deleted
151  // by the receiver.
152 
153  /* set our arrays to be big enough to hold all of the forces */
154  int num = vmdforces.size();
155 
156  DebugM(2,"Setting " << num << " forces.\n");
157 
158  if (!IMDignore && !IMDignoreForces) {
159  modifyForcedAtoms().resize(num);
161 
162  int i;
164  for ( i = 0; i < num; ++i, ++v_i) {
165  modifyForcedAtoms().item(i) = v_i->index;
166  modifyAppliedForces().item(i) = v_i->force;
167  }
168  }
169 }
ForceList & modifyAppliedForces()
Definition: GlobalMaster.C:162
std::ostream & iINFO(std::ostream &s)
Definition: InfoStream.C:107
int vmdsock_selread(void *v, int sec)
Definition: vmdsock.C:177
static int my_imd_connect(void *s)
#define DebugM(x, y)
Definition: Debug.h:59
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:108
#define iout
Definition: InfoStream.h:87
Elem & item(int i)
Definition: ResizeArray.h:115
int index(const Elem &elem)
Definition: SortedArray.h:75
AtomIDList & modifyForcedAtoms()
Definition: GlobalMaster.C:157
ForceList & modifyGroupForces()
Definition: GlobalMaster.C:167
int add(const Elem &elem)
Definition: ResizeArray.h:97
ResizeArray< void * > clients
static UniqueSortedArray< vmdforce > vmdforces
void resize(int i)
Definition: ResizeArray.h:84
void vmdsock_destroy(void *v)
Definition: vmdsock.C:164
int size(void) const
Definition: ResizeArray.h:127
infostream & endi(infostream &s)
Definition: InfoStream.C:38
void * vmdsock_accept(void *v)
Definition: vmdsock.C:128
void GlobalMasterIMD::get_vmd_forces ( )
protected

Definition at line 171 of file GlobalMasterIMD.C.

References UniqueSortedArray< Type >::add(), clients, SortedArray< Elem >::del(), ResizeArray< T >::del(), endi(), SortedArray< Elem >::find(), vmdforce::force, iINFO(), Node::imd, IMD_DISCONNECT, IMD_ENERGIES, IMD_FCOORDS, IMD_IOERROR, IMD_KILL, IMD_MDCOMM, IMD_PAUSE, imd_recv_energies(), imd_recv_fcoords(), imd_recv_header(), imd_recv_mdcomm(), IMD_TRATE, IMDignore, IMDignoreForces, IMDwait, SimParameters::IMDwait, vmdforce::index, iout, iWARN(), NAMD_quit(), Node::Object(), IMDOutput::set_transrate(), Node::simParameters, ResizeArray< T >::size(), vmdsock_destroy(), vmdsock_selread(), Vector::x, Vector::y, and Vector::z.

Referenced by calculate().

171  {
172  IMDType type;
173  int32 length;
174  int32 *vmd_atoms;
175  float *vmd_forces;
176  int paused = 0;
177  int warned = 0;
178  vmdforce *vtest, vnew;
179 
180  // Loop through each socket one at a time. By doing this, rather than
181  // polling all sockets at once, NAMD only has to keep up with one IMD
182  // connection; if it tried to read from all of them, it could more easily
183  // fall behind and never finish draining all the sockets.
184  // It would be better to have a system where VMD couldn't DOS NAMD by
185  // spamming it with messages, but in practice NAMD is able to keep up with
186  // VMD's send rate.
187  for (int i_client=0; i_client<clients.size(); i_client++) {
188  void *clientsock = clients[i_client];
189  while (vmdsock_selread(clientsock,0) > 0 || paused) { // Drain the socket
190  type = imd_recv_header(clientsock, &length);
191  switch (type) {
192  case IMD_MDCOMM:
193  // Expect the msglength to give number of indicies, and the data
194  // message to consist of first the indicies, then the coordinates
195  // in xyz1 xyz2... format.
196  vmd_atoms = new int32[length];
197  vmd_forces = new float[3*length];
198  if (imd_recv_mdcomm(clientsock, length, vmd_atoms, vmd_forces)) {
199  iout << iWARN <<
200  "Error reading IMD forces, killing connection\n" << endi;
201  goto vmdDestroySocket;
202  }
203  if (IMDignore || IMDignoreForces) {
204  if ( ! warned ) {
205  warned = 1;
206  char option[16];
207  if (IMDignore) strcpy(option, "IMDignore");
208  else strcpy(option, "IMDignoreForces");
209  iout << iWARN << "Ignoring IMD forces due to " << option << "\n" << endi;
210  }
211  } else {
212  for (int i=0; i<length; i++) {
213  vnew.index = vmd_atoms[i];
214  if ( (vtest=vmdforces.find(vnew)) != NULL) {
215  // find was successful, so overwrite the old force values
216  if (vmd_forces[3*i] != 0.0f || vmd_forces[3*i+1] != 0.0f
217  || vmd_forces[3*i+2] != 0.0f) {
218  vtest->force.x = vmd_forces[3*i];
219  vtest->force.y = vmd_forces[3*i+1];
220  vtest->force.z = vmd_forces[3*i+2];
221  } else {
222  // or delete it from the list if the new force is ZERO
223  vmdforces.del(vnew);
224  }
225  }
226  else {
227  // Create a new entry in the table if the new force isn't ZERO
228  if (vmd_forces[3*i] != 0.0f || vmd_forces[3*i+1] != 0.0f
229  || vmd_forces[3*i+2] != 0.0f) {
230  vnew.force.x = vmd_forces[3*i];
231  vnew.force.y = vmd_forces[3*i+1];
232  vnew.force.z = vmd_forces[3*i+2];
233  vmdforces.add(vnew);
234  }
235  }
236  }
237  }
238  delete [] vmd_atoms;
239  delete [] vmd_forces;
240  break;
241  case IMD_TRATE:
242  iout << iINFO << "Setting transfer rate to " << length<<'\n'<<endi;
243  Node::Object()->imd->set_transrate(length);
244  break;
245  case IMD_PAUSE:
246  if (IMDignore) {
247  iout << iWARN << "Ignoring IMD pause due to IMDignore\n" << endi;
248  break;
249  }
250  if ( paused ) {
251  iout << iINFO << "Resuming IMD\n" << endi;
253  }
254  paused = ! paused;
255  if ( paused ) {
256  iout << iINFO << "Pausing IMD\n" << endi;
257  IMDwait = 1;
258  }
259  break;
260  case IMD_IOERROR:
261  iout << iWARN << "IMD connection lost\n" << endi;
262  case IMD_DISCONNECT:
263  iout << iINFO << "IMD connection detached\n" << endi;
264  vmdDestroySocket:
265  vmdsock_destroy(clientsock);
266  clients.del(i_client);
267  // Enable the MD to continue after detach
268  if (IMDwait) IMDwait = 0;
269  goto vmdEnd;
270  case IMD_KILL:
271  if (IMDignore) {
272  iout << iWARN << "Ignoring IMD kill due to IMDignore\n" << endi;
273  break;
274  }
275  NAMD_quit("Received IMD kill from client\n");
276  break;
277  case IMD_ENERGIES:
278  IMDEnergies junk;
279  imd_recv_energies(clientsock, &junk);
280  break;
281  case IMD_FCOORDS:
282  vmd_forces = new float[3*length];
283  imd_recv_fcoords(clientsock, length, vmd_forces);
284  delete [] vmd_forces;
285  break;
286  default: ;
287  }
288  }
289  vmdEnd: ;
290  }
291 }
static Node * Object()
Definition: Node.h:86
std::ostream & iINFO(std::ostream &s)
Definition: InfoStream.C:107
Vector force
int vmdsock_selread(void *v, int sec)
Definition: vmdsock.C:177
void NAMD_quit(const char *err_msg)
Definition: common.C:64
short int32
Definition: dumpdcd.c:24
IMDOutput * imd
Definition: Node.h:183
void del(int index, int num=1)
Definition: ResizeArray.h:104
SimParameters * simParameters
Definition: Node.h:178
BigReal z
Definition: Vector.h:66
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:108
Definition: imd.h:20
#define iout
Definition: InfoStream.h:87
IMDType
Definition: imd.h:13
int imd_recv_fcoords(void *s, int32 n, float *coords)
Definition: imd.C:236
int imd_recv_mdcomm(void *s, int32 n, int32 *indices, float *forces)
Definition: imd.C:225
BigReal x
Definition: Vector.h:66
Definition: imd.h:22
ResizeArray< void * > clients
static UniqueSortedArray< vmdforce > vmdforces
void vmdsock_destroy(void *v)
Definition: vmdsock.C:164
BigReal y
Definition: Vector.h:66
Definition: imd.h:21
int imd_recv_energies(void *s, IMDEnergies *energies)
Definition: imd.C:231
int size(void) const
Definition: ResizeArray.h:127
infostream & endi(infostream &s)
Definition: InfoStream.C:38
Definition: imd.h:19
IMDType imd_recv_header(void *s, int32 *length)
Definition: imd.C:214
void set_transrate(int newrate)
Definition: IMDOutput.h:37
void GlobalMasterIMD::send_energies ( IMDEnergies energies)

Definition at line 293 of file GlobalMasterIMD.C.

References clients, imd_send_energies(), ResizeArray< T >::size(), and vmdsock_selwrite().

Referenced by IMDOutput::gather_energies().

293  {
294  for (int i=0; i<clients.size(); i++) {
295  void *clientsock = clients[i];
296  if (!clientsock || !vmdsock_selwrite(clientsock,0)) continue;
297  imd_send_energies(clientsock, energies);
298  }
299 }
int vmdsock_selwrite(void *v, int sec)
Definition: vmdsock.C:194
int imd_send_energies(void *s, const IMDEnergies *energies)
Definition: imd.C:159
ResizeArray< void * > clients
int size(void) const
Definition: ResizeArray.h:127
void GlobalMasterIMD::send_fcoords ( int  N,
FloatVector coords 
)

Definition at line 301 of file GlobalMasterIMD.C.

References clients, coordtmp, coordtmpsize, imd_send_fcoords(), ResizeArray< T >::size(), vmdsock_selwrite(), FloatVector::x, FloatVector::y, and FloatVector::z.

Referenced by IMDOutput::gather_coordinates().

301  {
302  for (int i=0; i<clients.size(); i++) {
303  void *clientsock = clients[i];
304  if (!clientsock || !vmdsock_selwrite(clientsock,0)) continue;
305  if (sizeof(FloatVector) == 3*sizeof(float)) {
306  imd_send_fcoords(clientsock, N, (float *)coords);
307  } else {
308  if (coordtmpsize < N) {
309  delete [] coordtmp;
310  coordtmp = new float[3*N];
311  coordtmpsize = N;
312  }
313  for (int i=0; i<N; i++) {
314  coordtmp[3*i] = coords[i].x;
315  coordtmp[3*i+1] = coords[i].y;
316  coordtmp[3*i+2] = coords[i].z;
317  }
318  imd_send_fcoords(clientsock, N, coordtmp);
319  }
320  }
321 }
int vmdsock_selwrite(void *v, int sec)
Definition: vmdsock.C:194
float y
Definition: Vector.h:18
int imd_send_fcoords(void *s, int32 n, const float *coords)
Definition: imd.C:169
float x
Definition: Vector.h:18
float z
Definition: Vector.h:18
ResizeArray< void * > clients
int size(void) const
Definition: ResizeArray.h:127

Friends And Related Function Documentation

friend class IMDOutput
friend

Definition at line 27 of file GlobalMasterIMD.h.

Member Data Documentation

ResizeArray<void *> GlobalMasterIMD::clients
protected
float* GlobalMasterIMD::coordtmp
protected

Definition at line 50 of file GlobalMasterIMD.h.

Referenced by GlobalMasterIMD(), send_fcoords(), and ~GlobalMasterIMD().

int GlobalMasterIMD::coordtmpsize
protected

Definition at line 51 of file GlobalMasterIMD.h.

Referenced by GlobalMasterIMD(), and send_fcoords().

int GlobalMasterIMD::IMDignore
protected

Definition at line 38 of file GlobalMasterIMD.h.

Referenced by calculate(), get_vmd_forces(), GlobalMasterIMD(), and IMDOutput::use_imd().

int GlobalMasterIMD::IMDignoreForces
protected

Definition at line 41 of file GlobalMasterIMD.h.

Referenced by calculate(), get_vmd_forces(), GlobalMasterIMD(), and IMDOutput::use_imd().

int GlobalMasterIMD::IMDwait
protected

Definition at line 35 of file GlobalMasterIMD.h.

Referenced by calculate(), get_vmd_forces(), and GlobalMasterIMD().

void* GlobalMasterIMD::sock
protected

Definition at line 44 of file GlobalMasterIMD.h.

Referenced by calculate(), GlobalMasterIMD(), and ~GlobalMasterIMD().


The documentation for this class was generated from the following files: