PeriodicVmdReporter.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef SimTK_MOLMODEL_PERIODICVMDREPORTER_H_
00033 #define SimTK_MOLMODEL_PERIODICVMDREPORTER_H_
00034
00035 #include "SimTKsimbody.h"
00036 #include "molmodel/internal/Compound.h"
00037 #include "molmodel/internal/VmdConnection.h"
00038 #include <iostream>
00039 #include <iomanip>
00040 #include <vector>
00041
00042 namespace SimTK {
00043
00046 class PeriodicVmdReporter : public PeriodicEventReporter {
00047 public:
00048 PeriodicVmdReporter(
00049 const CompoundSystem& system,
00050 Real interval,
00051 int localSocketNumber,
00052 bool blockWaitingForVmdConnection = false
00053 )
00054 : PeriodicEventReporter(interval),
00055 system(system),
00056 vmdConnection(localSocketNumber),
00057 blockWaitingForVmdConnection(blockWaitingForVmdConnection)
00058 {}
00059
00060 void handleEvent(const State& state) const
00061 {
00062
00063
00064 static int timeStepNumber = 0;
00065 int checkFrequency = 20;
00066
00067
00068 if (! vmdConnection.clientIsConnected() )
00069 {
00070 if (blockWaitingForVmdConnection)
00071 {
00072 while ( ! vmdConnection.clientIsConnected() )
00073 {
00074 std::cerr << "Waiting for vmd IMD connection on port " << vmdConnection.getSocketNumber() << "..." << std::endl;
00075
00076 vmdConnection.checkForConnection();
00077
00078 sleep(1);
00079
00080 }
00081 }
00082 else
00083 {
00084 if ( 0 == (timeStepNumber % checkFrequency) )
00085 vmdConnection.checkForConnection();
00086 }
00087 }
00088
00089
00090 if ( vmdConnection.clientIsConnected() )
00091 {
00092 system.realize(state, Stage::Position);
00093
00094
00095
00096 std::stringstream pdbString;
00097
00098
00099 int nextAtomSerialNumber = 1;
00100 for (SimTK::Compound::Index c(0); c < system.getNumCompounds(); ++c)
00101 system.getCompound(c).writePdb(state, pdbString, nextAtomSerialNumber);
00102
00103
00104 std::vector<VmdFloat3> vmdCoordinates;
00105 char lineBuffer[102];
00106 while ( ! pdbString.eof() )
00107 {
00108 pdbString.getline(lineBuffer, 100);
00109 std::string line(lineBuffer);
00110
00111 if ( (line.length() > 55) && ((line.substr(0, 6) == "ATOM ") || (line.substr(0, 6) == "HETATM")) )
00112 {
00113 float x, y, z;
00114 std::istringstream(line.substr(30, 8)) >> x;
00115 std::istringstream(line.substr(38, 8)) >> y;
00116 std::istringstream(line.substr(46, 8)) >> z;
00117 vmdCoordinates.push_back(VmdFloat3(x, y, z));
00118 }
00119 }
00120
00121 vmdConnection.sendCoordinates(vmdCoordinates);
00122 }
00123
00124 ++timeStepNumber;
00125
00126 }
00127
00128 private:
00129 const CompoundSystem& system;
00130 mutable VmdConnection vmdConnection;
00131 bool blockWaitingForVmdConnection;
00132 };
00133
00134 }
00135
00136 #endif // SimTK_MOLMODEL_PERIODICVMDREPORTER_H_