cbdsim_parallel.xml 12 KB

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