celltracing.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ..
  2. Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  3. McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. Cell Tracing
  14. ============
  15. Cell tracing is somewhat different from the *verbose* and *XML* tracers, in that it is only applicable for several models. The models that support it however, can profit from it, as this is a very visual and easy to understand tracer.
  16. .. note:: For those familiar with Cell DEVS (and particularly CD++): this tracer aims at providing the same tracing functionality for 2D DEVS models as CD++ provides for them. This does introduce parts of Cell DEVS in Parallel DEVS, but of course the only affected parts are the actual tracing and not model construction and simulation.
  17. There are only 2 requirements on the model for Cell tracing:
  18. * The models have an *x* and *y* attribute, which signify the location where the cell will be drawn.
  19. * The states have an *toCellState()* method, which should return the value to be shown in the matrix.
  20. .. note:: If two models have unique coordinates, only one of them will be considered in the tracing. There is no support for dimensions above 2D.
  21. Thus a very simple cell would look like this::
  22. class CellState(object):
  23. def __init__(self, value):
  24. self.value = value
  25. def toCellState(self):
  26. # Simply return the value, but could also first
  27. # perform some operations on the value
  28. return value
  29. class Cell(AtomicDEVS):
  30. def __init__(self, x, y):
  31. AtomicDEVS.__init__(self, 'Cell(%i, %i)' % (x, y))
  32. self.x = x
  33. self.y = y
  34. self.state = CellState()
  35. The coupled model would then be responsible for assigning unique coordinates to every cell. The following configurations now still need to be set in the experiment file::
  36. # Save the coordinates of the model somewhere, as we need them later
  37. x, y = 20, 20
  38. model = MyCoupledCellModel(x, y)
  39. sim = Simulator(model)
  40. sim.setCell(True, x, y, cell_file="celltrace", multifile=False)
  41. sim.simulate()
  42. This will then generate a file with a matrix of the current state at every timestep. The matrices are simply appended with a delimiter between them. Sadly, this kind of data is not directly usable in gnuplot, which is why the *multifile* option comes in handy.
  43. When setting *multifile=True*, every timestep will have its own file, which contains only the matrix and can be directly drawn using *gnuplot*. Simulator-defined files are not that handy, because you probably want it in a slightly different format. For this reason, the *cell_file* parameter will be interpreted as a *format string* when *multifile* is set to *True*. An example invocation of this could be::
  44. sim.setCell(True, x, y, cell_file="celltrace-%05d", multifile=True)
  45. This will then create the files *celltrace-00001*, *celltrace-00002*, ... until the termination time is reached.
  46. .. note:: Remember that the *cell_file* parameter will be used as a format string if *multifile* is *True*!
  47. After these files are generated, we can simply plot them with e.g. *gnuplot* as follows:
  48. .. code-block:: gnuplot
  49. set pm3d map
  50. set pm3d interpolate 0, 0
  51. splot 'celltrace-00001' matrix
  52. This will generate in interpolated version (which is slightly nicer than the original version). Naturally, scripting immediately comes to mind when a lot of files with similar data are created. It is now even possible to create an animation of the simulation, thus visualizing the change over time. This can be done in e.g. bash as follows:
  53. .. code-block:: bash
  54. for f in `ls -1 celltrace-*`
  55. do
  56. echo "set view map" > plot
  57. echo "set terminal png size 400, 300 enhanced" >> plot
  58. echo "set pm3d map" >> plot
  59. echo "set pm3d interpolate 0, 0" >> plot
  60. echo "set output '$f.png'" >> plot
  61. echo "splot '$f' matrix" >> plot
  62. gnuplot plot
  63. rm plot
  64. done
  65. avconv -i celltrace-%5d.png out.avi
  66. Which will create an animation visualizing e.g. a *fire spread* model. Since *gnuplot* will automatically determine the range of colors to use, it might be interesting to provide this range yourself, as to keep it consistent between all different images. This can be done by adding the following line right before the *splot* line:
  67. .. code-block:: bash
  68. echo "set cbrange [27:800]" >> plot
  69. Each individual file will then look something like:
  70. .. image:: celldevs.png
  71. :alt: Cell view
  72. :width: 100%
  73. And an animated version looks like:
  74. .. image:: celldevs.gif
  75. :alt: Cell view animation
  76. :width: 100%
  77. .. note:: It is technically possible to visualize this data in (semi)-realtime. If local simulation is done, each trace file will be written as soon as possible. It would require some additional lines of scripting to actually poll for these files and render them of course. In distributed simulation, visualisation at run time is not so simple, as these files will only be written at GVT boundaries.