cbdsim_parallel.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?xml version="1.0" ?>
  2. <diagram name="CBDSimulator" author="Sadaf Mustafiz and Claudio Gomes and Simon Van Mierlo">
  3. <description>
  4. A CBD simulator.
  5. </description>
  6. <inport name="user_input" />
  7. <inport name="user_output" />
  8. <top>
  9. from sccd.runtime.libs.ui import *
  10. from sccd.runtime.libs.utils import *
  11. from CBD_Controller import CBDController
  12. import Options
  13. import sccd.runtime.accurate_time as accurate_time
  14. </top>
  15. <class name="CBDSimulator" default="True">
  16. <attribute name="iteration"/>
  17. <attribute name="delta"/>
  18. <attribute name="clock"/>
  19. <attribute name="state"/>
  20. <attribute name="model"/>
  21. <attribute name="depGraph"/>
  22. <attribute name="strongComponentList"/>
  23. <attribute name="currentCompIdx"/>
  24. <attribute name="cbdController"/>
  25. <attribute name="options"/>
  26. <method name="CBDSimulator">
  27. <parameter name="options"/>
  28. <parameter name="model"/>
  29. <body>
  30. <![CDATA[
  31. self.options = options
  32. self.delta = self.options.getDeltaT() * 1000.0 # in miliseconds for real-time simulation
  33. self.model = model
  34. ]]>
  35. </body>
  36. </method>
  37. <method name="initializeSimulation">
  38. <body>
  39. <![CDATA[
  40. self.iteration = 0
  41. self.clock = 0
  42. self.time_next = self.delta
  43. self.cbdController = CBDController(self.model, self.delta / 1000.0)
  44. self.cbdController.initSimulation()
  45. self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
  46. ]]>
  47. </body>
  48. </method>
  49. <method name="initializeBigStep">
  50. <body>
  51. <![CDATA[
  52. self.currentCompIdx = 0
  53. self.depGraph = self.cbdController.createDepGraph(self.iteration)
  54. self.strongComponentList = self.cbdController.createStrongComponents(self.depGraph, self.iteration)
  55. ]]>
  56. </body>
  57. </method>
  58. <method name="initializeSmallStep">
  59. <body>
  60. <![CDATA[
  61. self.small_step_executed = False
  62. ]]>
  63. </body>
  64. </method>
  65. <method name="finalizeSimulation">
  66. <body>
  67. <![CDATA[
  68. from bokeh.plotting import figure, output_file, show
  69. times = []
  70. values = []
  71. for timeValuePair in self.model.getSignal("neg"):
  72. times.append(timeValuePair.time)
  73. values.append(timeValuePair.value)
  74. output_file("./plot.html", title="Plot")
  75. p = figure(title="Something vs Otherthing", x_axis_label="Time", y_axis_label="Values")
  76. p.line(times, values, legend="Something", line_width=1, line_color="red")
  77. show(p)
  78. print 'Simulation finalized.'
  79. ]]>
  80. </body>
  81. </method>
  82. <method name="finalizeBigStep">
  83. <body>
  84. <![CDATA[
  85. self.advanceTime()
  86. ]]>
  87. </body>
  88. </method>
  89. <method name="finalizeSmallStep">
  90. <body>
  91. <![CDATA[
  92. self.currentCompIdx = self.currentCompIdx + 1
  93. ]]>
  94. </body>
  95. </method>
  96. <method name="endConditionSimulation">
  97. <body>
  98. <![CDATA[
  99. return self.iteration >= self.options.getMaxIterations()
  100. ]]>
  101. </body>
  102. </method>
  103. <method name="endConditionBigStep">
  104. <body>
  105. <![CDATA[
  106. return self.currentCompIdx >= len(self.strongComponentList)
  107. ]]>
  108. </body>
  109. </method>
  110. <method name="endConditionSmallStep">
  111. <body>
  112. <![CDATA[
  113. return self.small_step_executed
  114. ]]>
  115. </body>
  116. </method>
  117. <method name="advanceTime">
  118. <body>
  119. <![CDATA[
  120. self.iteration = self.iteration + 1
  121. self.clock = self.time_next
  122. self.cbdController.advanceTimeStep()
  123. self.time_next = self.clock + self.delta
  124. ]]>
  125. </body>
  126. </method>
  127. <method name="currentComponentIsCycle">
  128. <body>
  129. <![CDATA[
  130. return self.cbdController.componentIsCycle(self.strongComponentList[self.currentCompIdx], self.depGraph)
  131. ]]>
  132. </body>
  133. </method>
  134. <method name="computeBlock">
  135. <body>
  136. <![CDATA[
  137. if self.currentComponentIsCycle():
  138. self.cbdController.computeNextAlgebraicLoop(self.strongComponentList[self.currentCompIdx], self.iteration)
  139. else:
  140. self.cbdController.computeNextBlock(self.strongComponentList[self.currentCompIdx], self.iteration)
  141. self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
  142. ]]>
  143. </body>
  144. </method>
  145. <scxml initial="Main" final="SimulationComplete">
  146. <parallel id="Main">
  147. <state id="ExecuteSimulation" initial="Started">
  148. <state id="Stopped"/>
  149. <state id="Started">
  150. <transition target="../Initialized">
  151. <script>
  152. <![CDATA[
  153. self.initializeSimulation()
  154. ]]>
  155. </script>
  156. </transition>
  157. </state>
  158. <state id="Initialized">
  159. <transition target="../CheckTermination" />
  160. </state>
  161. <state id="CheckTermination">
  162. <transition target="../InitializingChild" cond="not self.endConditionSimulation()">
  163. <raise event="BigStep.Reset" />
  164. </transition>
  165. <transition target="../Stopped" cond="self.endConditionSimulation()">
  166. <script>
  167. print 'Simulation finished'
  168. self.finalizeSimulation()
  169. </script>
  170. </transition>
  171. </state>
  172. <state id="InitializingChild">
  173. <transition target="../Executing" event="BigStep.Initialized">
  174. <raise event="BigStep.Execute" />
  175. </transition>
  176. </state>
  177. <state id="Executing">
  178. <transition target="../CheckTermination" event="BigStep.Finished" />
  179. </state>
  180. </state>
  181. <state id="ExecuteBigStep" initial="Stopped">
  182. <state id="Stopped">
  183. <transition target="../Started" event="BigStep.Reset" />
  184. </state>
  185. <state id="Started">
  186. <transition target="../Initialized">
  187. <raise event="BigStep.Initialized" />
  188. <script>
  189. <![CDATA[
  190. self.initializeBigStep()
  191. ]]>
  192. </script>
  193. </transition>
  194. </state>
  195. <state id="Initialized">
  196. <transition target="../CheckTermination" event="BigStep.Execute" />
  197. </state>
  198. <state id="CheckTermination">
  199. <transition target="../InitializingChild" cond="not self.endConditionBigStep()">
  200. <raise event="SmallStep.Reset" />
  201. </transition>
  202. <transition target="../Stopped" cond="self.endConditionBigStep()">
  203. <script>
  204. print 'big step executed'
  205. self.finalizeBigStep()
  206. print 'Iteration: ' + str(self.iteration)
  207. </script>
  208. <raise event="BigStep.Finished" />
  209. </transition>
  210. </state>
  211. <state id="InitializingChild">
  212. <transition target="../Executing" event="SmallStep.Initialized">
  213. <raise event="SmallStep.Execute" />
  214. </transition>
  215. </state>
  216. <state id="Executing">
  217. <transition target="../CheckTermination" event="SmallStep.Finished" />
  218. </state>
  219. </state>
  220. <state id="ExecuteSmallStep" initial="Stopped">
  221. <state id="Stopped">
  222. <transition target="../Started" event="SmallStep.Reset" />
  223. </state>
  224. <state id="Started">
  225. <transition target="../Initialized">
  226. <raise event="SmallStep.Initialized" />
  227. <script>
  228. <![CDATA[
  229. self.initializeSmallStep()
  230. ]]>
  231. </script>
  232. </transition>
  233. </state>
  234. <state id="Initialized">
  235. <transition target="../CheckTermination" event="SmallStep.Execute"/>
  236. </state>
  237. <state id="CheckTermination">
  238. <transition target="../InitializingChild" cond="not self.endConditionSmallStep()">
  239. <raise event="Block.Reset" />
  240. </transition>
  241. <transition target="../Stopped" cond="self.endConditionSmallStep()">
  242. <script>
  243. print 'small step executed'
  244. self.finalizeSmallStep()
  245. </script>
  246. <raise event="SmallStep.Finished" />
  247. </transition>
  248. </state>
  249. <state id="InitializingChild">
  250. <transition target="../Executing" event="Block.Initialized">
  251. <raise event="Block.Execute" />
  252. </transition>
  253. </state>
  254. <state id="Executing">
  255. <transition target="../CheckTermination" event="Block.Finished">
  256. <script>
  257. self.small_step_executed = True
  258. </script>
  259. </transition>
  260. </state>
  261. </state>
  262. <state id="ExecuteBlock" initial="Stopped">
  263. <state id="Stopped">
  264. <transition target="../Started" event="Block.Reset" />
  265. </state>
  266. <state id="Started">
  267. <transition target="../Initialized">
  268. <raise event="Block.Initialized" />
  269. </transition>
  270. </state>
  271. <state id="Initialized">
  272. <transition target="../Executing" event="Block.Execute" />
  273. </state>
  274. <state id="Executing">
  275. <transition target="../Stopped">
  276. <script>
  277. self.computeBlock()
  278. </script>
  279. <raise event="Block.Finished" />
  280. </transition>
  281. </state>
  282. </state>
  283. </parallel>
  284. </scxml>
  285. </class>
  286. </diagram>