Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

IMDSimBlocking.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2019 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: IMDSimBlocking.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.18 $       $Date: 2019/01/17 21:20:59 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *  A single-threaded implementation of the interactive MD 
00019  *  coordinate/force update communication loop 
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;  // Don't need to read data 
00049     }
00050   }
00051 }
00052 
00053 void IMDSimBlocking::process_coordinates(int32 length) {
00054   if (numcoords < length) { // Need to resize
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 /* length */) {
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 // This should never happen, but I'll handle it in case it does
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   // Total data sent will be one int and three floats for each atom 
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 

Generated on Fri Nov 8 02:44:41 2024 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002