OverSim
ConnectivityProbeQuon.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
25 #include "ConnectivityProbeQuon.h"
26 
28 
30 {
31  this->moduleID = moduleID;
32  visited = false;
33 }
34 
36 {
37  return check_and_cast<Quon*>(simulation.getModule(moduleID));
38 }
39 
41 {
43  probeIntervall = par("connectivityProbeIntervall");
44  plotIntervall = par("visualizeNetworkIntervall");
45  startPlotTime = par("startPlotTime");
46  plotPeriod = par("plotPeriod");
47  probeTimer = new cMessage("probeTimer");
48  plotTimer = new cMessage("plotTimer");
49  plotConnections = par("plotConnections");
50  plotBindings = par("plotBindings");
51  plotMissing = par("plotMissing");
52 
53  if(probeIntervall > 0.0) {
54  scheduleAt(simTime() + probeIntervall, probeTimer);
55 
56  cOV_NodeCount.setName("total node count");
57  cOV_MaximumComponent.setName("largest connected component");
58  cOV_MaxConnectivity.setName("connectivity in percent");
59  cOV_ZeroMissingNeighbors.setName("neighbor-error free nodes");
60  cOV_AverageMissingNeighbors.setName("average missing neighbors per node");
61  cOV_MaxMissingNeighbors.setName("largest missing neighbors count");
62  cOV_AverageDrift.setName("average drift");
63  }
64 
65  if(plotIntervall > 0.0) {
66  if(startPlotTime == 0.0) {
67  scheduleAt(simTime() + plotIntervall, plotTimer);
68  }
69  else {
70  scheduleAt(simTime() + startPlotTime, plotTimer);
71  }
72  }
73 }
74 
76 {
77  // fill topology with all QUONP modules
79 
80  if(Topology.size() == 0) {
81  return;
82  }
83 
84  // catch self timer messages
85  if(msg->isName("probeTimer")) {
86  //reset timer
87  cancelEvent(probeTimer);
88  scheduleAt(simTime() + probeIntervall, msg);
89 
90  unsigned int maxComponent = 0;
91  for(QuonTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
92  unsigned int count = getComponentSize(itTopology->second.getModule()->getKey());
93  if(count > maxComponent) {
94  maxComponent = count;
95  }
97  if(count == Topology.size()) {
98  break;
99  }
100  }
101 
102  cOV_NodeCount.record((double)Topology.size());
103  cOV_MaximumComponent.record((double)maxComponent);
104  cOV_MaxConnectivity.record((double)maxComponent * 100.0 / (double)Topology.size());
105  RECORD_STATS (
106  globalStatistics->addStdDev("ConnectivityProbe: max connectivity", (double)maxComponent * 100.0 / (double)Topology.size());
107  );
108 
109  int mnMax = 0;
110  int mnZero = 0;
111  int driftCount = 0;
112  double mnAverage = 0.0;
113  double drift = 0.0;
114 
115  for(QuonTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
116  QuonAOI AOI(itTopology->second.getModule()->getPosition(), itTopology->second.getModule()->getAOI());
117  int missing = 0;
118  for(QuonTopology::iterator itI = Topology.begin(); itI != Topology.end(); ++itI) {
119  if(itI != itTopology && AOI.collide(itI->second.getModule()->getPosition())) {
120  QuonSiteMap::iterator currentSite = itTopology->second.getModule()->Sites.find(itI->second.getModule()->getKey());
121  if(currentSite == itTopology->second.getModule()->Sites.end()) {
122  ++missing;
123  }
124  else {
125  drift += sqrt(currentSite->second->position.distanceSqr(itI->second.getModule()->getPosition()));
126  ++driftCount;
127  }
128  }
129  }
130 
131  mnAverage += missing;
132  if(mnMax < missing) {
133  mnMax = missing;
134  }
135  if(missing == 0) {
136  ++mnZero;
137  }
138  }
139  mnAverage /= (double)Topology.size();
140  if(driftCount > 0) {
141  drift /= (double)driftCount;
142  }
143 
144  cOV_ZeroMissingNeighbors.record((double)mnZero);
145  RECORD_STATS (
146  globalStatistics->addStdDev("ConnectivityProbe: percentage zero missing neighbors", (double)mnZero * 100.0 / (double)Topology.size());
147  globalStatistics->addStdDev("ConnectivityProbe: average drift", drift);
148  );
149  cOV_AverageMissingNeighbors.record(mnAverage);
150  cOV_MaxMissingNeighbors.record((double)mnMax);
151  cOV_AverageDrift.record(drift);
152  }
153  else if(msg->isName("plotTimer")) {
154  //reset timer
155  cancelEvent(plotTimer);
156  if(plotPeriod == 0.0 || simTime() <= startPlotTime + plotPeriod) {
157  scheduleAt(simTime() + plotIntervall, msg);
158  }
159 
160  bool missingFound = false;
161  if(plotMissing) {
162  for(QuonTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
163  QuonAOI AOI(itTopology->second.getModule()->getPosition(), itTopology->second.getModule()->getAOI());
164  for(QuonTopology::iterator itI = Topology.begin(); itI != Topology.end(); ++itI) {
165  if(itI != itTopology && AOI.collide(itI->second.getModule()->getPosition())) {
166  QuonSiteMap::iterator currentSite = itTopology->second.getModule()->Sites.find(itI->second.getModule()->getKey());
167  if(currentSite == itTopology->second.getModule()->Sites.end()) {
168  missingFound = true;
169  }
170  }
171  }
172  }
173  }
174 
175  if(!plotMissing || missingFound) {
176  int range = (int)Topology.begin()->second.getModule()->getAreaDimension();
177  std::stringstream oss;
178  std::string filename;
179  int simTimeInt, stellen = 1;
180  simTimeInt = (int)SIMTIME_DBL(simTime());
181  oss << "plot";
182  for(int i=0; i<6; i++) {
183  if(!(simTimeInt / stellen)) {
184  oss << "0";
185  }
186  stellen *= 10;
187  }
188  oss << simTimeInt;
189 
190  // open / write plot file
191  filename = oss.str() + ".plot";
192  pltNetwork.open(filename.c_str(), std::ios::out);
193  pltNetwork << "set xrange [0:" << range << "]" << endl;
194  pltNetwork << "set yrange [0:" << range << "]" << endl;
195  pltNetwork << "set nokey" << endl;
196 
197  // open point file
198  filename = oss.str() + ".point";
199  pltData.open(filename.c_str(), std::ios::out);
200 
201  pltNetwork << "plot '" << filename << "' using 1:2 with points pointtype 7,\\" << endl;
202 
203  // open vector file
204  filename = oss.str() + ".arrow";
205  pltVector.open(filename.c_str(), std::ios::out);
206 
207  pltNetwork << " '" << filename << "' using 1:2:3:4 with vectors linetype 1" << endl;
208  pltNetwork.close();
209 
210  // write point data file
211  for(QuonTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
212  pltData << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << endl;
213  }
214  pltData.close();
215 
216  //write arrow data file
217  if(!plotMissing) {
218  for(QuonTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
219  for(QuonSiteMap::iterator itSites = itTopology->second.getModule()->Sites.begin(); itSites != itTopology->second.getModule()->Sites.end(); ++itSites) {
220  if(plotBindings && itSites->second->type != QBINDING && !itSites->second->softNeighbor) {
221  continue;
222  }
223  if(plotConnections) {
224  QuonTopology::iterator destNode = Topology.find(itSites->second->address.getKey());
225  if(destNode != Topology.end()) {
226  Vector2D relPos = destNode->second.getModule()->getPosition() - itTopology->second.getModule()->getPosition();
227  pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
228  << relPos.x << "\t" << relPos.y << endl;
229  }
230  }
231  else {
232  Vector2D relPos = itSites->second->position - itTopology->second.getModule()->getPosition();
233  pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
234  << relPos.x << "\t" << relPos.y << endl;
235  }
236  }
237  }
238  }
239  else {
240  for(QuonTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
241  QuonAOI AOI(itTopology->second.getModule()->getPosition(), itTopology->second.getModule()->getAOI());
242  for(QuonTopology::iterator itI = Topology.begin(); itI != Topology.end(); ++itI) {
243  if(itI != itTopology && AOI.collide(itI->second.getModule()->getPosition())) {
244  QuonSiteMap::iterator currentSite = itTopology->second.getModule()->Sites.find(itI->second.getModule()->getKey());
245  if(currentSite == itTopology->second.getModule()->Sites.end()) {
246  Vector2D relPos = itI->second.getModule()->getPosition() - itTopology->second.getModule()->getPosition();
247  pltVector << itTopology->second.getModule()->getPosition().x << "\t"
248  << itTopology->second.getModule()->getPosition().y << "\t"
249  << relPos.x << "\t" << relPos.y << "\t"
250  << itTopology->second.getModule()->getParentModule()->getParentModule()->getFullName() << ":"
251  << itTopology->second.getModule()->getKey().toString(16) << "\t"
252  << itI->second.getModule()->getParentModule()->getParentModule()->getFullName() << ":"
253  << itI->second.getModule()->getKey().toString(16) << endl;
254  }
255  }
256  }
257  }
258  }
259  pltVector.close();
260  }
261  }
262  Topology.clear();
263 }
264 
266 {
267  for(int i=0; i<=simulation.getLastModuleId(); i++) {
268  cModule* module = simulation.getModule(i);
269  if(module && dynamic_cast<Quon*>(module)) {
270  Quon* quonp = check_and_cast<Quon*>(module);
271  if(quonp->getState() == QREADY) {
272  QuonTopologyNode temp(i);
273  Topology.insert(std::make_pair(quonp->getKey(), temp));
274  }
275  }
276  }
277 }
278 
280 {
281  for(QuonTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
282  itTopology->second.visited = false;
283  }
284 }
285 
287 {
288  QuonTopology::iterator itEntry = Topology.find(key);
289  if(itEntry != Topology.end() && itEntry->second.visited == false) {
290  int count = 1;
291  itEntry->second.visited = true;
292  Quon* quonp = itEntry->second.getModule();
293  for(QuonSiteMap::iterator itSites = quonp->Sites.begin(); itSites != quonp->Sites.end(); ++itSites) {
294  count += getComponentSize(itSites->first);
295  }
296  return count;
297  }
298  return 0;
299 }
300 
302 {
303  // destroy self timer messages
304  cancelAndDelete(probeTimer);
305  cancelAndDelete(plotTimer);
306 }