// This should never happen, but I'll handle it in case it does
void IMDSimBlocking::process_mdcomm(int32 length) {
  int32 *ind = new int32[length];
  float *f = new float[3*length];
  if (imd_recv_mdcomm(sock, length, ind, f)) {
    msgErr << "Error reading MDComm-style forces!" << sendmsg;
    disconnect();
  }
  delete [] ind;
  delete [] f;
}
Exemple #2
0
void IMD::receive(){

  if(!connected) return;

  if(clientsock){
    IMDType type;
    int length;
    int itype;
    while (vmdsock_selread(clientsock,0) > 0) {
      type = imd_recv_header(clientsock, &length);
      if(type==IMD_MDCOMM){
        int32* vmd_atoms = new int32[length];
        float* vmd_forces = new float[3*length];
        imd_recv_mdcomm(clientsock, length, vmd_atoms, vmd_forces);
        for(int i=0;i<length;i++){
          forces[3*vmd_atoms[i]+0]=vmd_forces[3*i+0];
          forces[3*vmd_atoms[i]+1]=vmd_forces[3*i+1];
          forces[3*vmd_atoms[i]+2]=vmd_forces[3*i+2];
        }
        delete [] vmd_atoms;
        delete [] vmd_forces;
        itype=0;
        comm.Bcast(&itype,1,0);
        comm.Bcast(&forces[0],forces.size(),0);
      }else if(type==IMD_DISCONNECT){
        vmdsock_destroy(clientsock);
        clientsock=NULL;
        for(unsigned i=0;i<forces.size();i++) forces[i]=0.0;
        connected=false;
        itype=1;
        comm.Bcast(&itype,1,0);
        break;
      }else if(type==IMD_TRATE){
        if(length<1) length=1;
        itype=2;
        log.printf("IMD: setting transfer rate to %d\n",length);
        transferRate=length;
        comm.Bcast(&itype,1,0);
        comm.Bcast(&transferRate,1,0);
      }else if(type==IMD_KILL){
        log.printf("IMD: killing simulation\n");
        itype=3;
        comm.Bcast(&itype,1,0);
        plumed.exit();
      }
    }
    itype=-1;
    comm.Bcast(&itype,1,0);
  }

  if(comm.Get_rank()!=0){
    int itype;
    while(true){
      comm.Bcast(&itype,1,0);
      if(itype==-1)break;
      else if(itype==0) comm.Bcast(&forces[0],forces.size(),0);
      else if(itype==1) {
        for(unsigned i=0;i<forces.size();i++) forces[i]=0.0;
        connected=false;
      }
      else if(itype==2) comm.Bcast(&transferRate,1,0);
      else if(itype==3) plumed.exit();
      else plumed_error();
    }
  }

}