| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?xml version="1.0" ?>
- <diagram name="CBDSimulator" author="Sadaf Mustafiz and Claudio Gomes and Simon Van Mierlo">
- <description>
- A CBD simulator.
- </description>
-
- <inport name="user_input" />
- <inport name="user_output" />
-
- <top>
- from sccd.runtime.libs.ui import *
- from sccd.runtime.libs.utils import *
- from CBD_Controller import CBDController
- import Options
- import sccd.runtime.accurate_time as accurate_time
-
- </top>
-
- <class name="CBDSimulator" default="True">
- <attribute name="iteration"/>
- <attribute name="delta"/>
- <attribute name="clock"/>
- <attribute name="state"/>
- <attribute name="model"/>
- <attribute name="depGraph"/>
- <attribute name="strongComponentList"/>
- <attribute name="currentCompIdx"/>
- <attribute name="cbdController"/>
- <attribute name="options"/>
- <method name="CBDSimulator">
- <parameter name="options"/>
- <parameter name="model"/>
- <body>
- <![CDATA[
- self.options = options
- self.delta = self.options.getDeltaT() * 1000.0 # in miliseconds for real-time simulation
- self.model = model
- ]]>
- </body>
- </method>
- <method name="initializeSimulation">
- <body>
- <![CDATA[
- self.iteration = 0
- self.clock = 0
- self.time_next = self.delta
- self.cbdController = CBDController(self.model, self.delta / 1000.0)
- self.cbdController.initSimulation()
- self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
- ]]>
- </body>
- </method>
- <method name="initializeBigStep">
- <body>
- <![CDATA[
- self.currentCompIdx = 0
- self.depGraph = self.cbdController.createDepGraph(self.iteration)
- self.strongComponentList = self.cbdController.createStrongComponents(self.depGraph, self.iteration)
- ]]>
- </body>
- </method>
- <method name="initializeSmallStep">
- <body>
- <![CDATA[
- self.small_step_executed = False
- ]]>
- </body>
- </method>
- <method name="finalizeSimulation">
- <body>
- <![CDATA[
- from bokeh.plotting import figure, output_file, show
- times = []
- values = []
- for timeValuePair in self.model.getSignal("neg"):
- times.append(timeValuePair.time)
- values.append(timeValuePair.value)
- output_file("./plot.html", title="Plot")
- p = figure(title="Something vs Otherthing", x_axis_label="Time", y_axis_label="Values")
- p.line(times, values, legend="Something", line_width=1, line_color="red")
- show(p)
- print 'Simulation finalized.'
- ]]>
- </body>
- </method>
- <method name="finalizeBigStep">
- <body>
- <![CDATA[
- self.advanceTime()
- ]]>
- </body>
- </method>
- <method name="finalizeSmallStep">
- <body>
- <![CDATA[
- self.currentCompIdx = self.currentCompIdx + 1
- ]]>
- </body>
- </method>
- <method name="endConditionSimulation">
- <body>
- <![CDATA[
- return self.iteration >= self.options.getMaxIterations()
- ]]>
- </body>
- </method>
- <method name="endConditionBigStep">
- <body>
- <![CDATA[
- return self.currentCompIdx >= len(self.strongComponentList)
- ]]>
- </body>
- </method>
- <method name="endConditionSmallStep">
- <body>
- <![CDATA[
- return self.small_step_executed
- ]]>
- </body>
- </method>
- <method name="advanceTime">
- <body>
- <![CDATA[
- self.iteration = self.iteration + 1
- self.clock = self.time_next
- self.cbdController.advanceTimeStep()
- self.time_next = self.clock + self.delta
- ]]>
- </body>
- </method>
- <method name="currentComponentIsCycle">
- <body>
- <![CDATA[
- return self.cbdController.componentIsCycle(self.strongComponentList[self.currentCompIdx], self.depGraph)
- ]]>
- </body>
- </method>
- <method name="computeBlock">
- <body>
- <![CDATA[
- if self.currentComponentIsCycle():
- self.cbdController.computeNextAlgebraicLoop(self.strongComponentList[self.currentCompIdx], self.iteration)
- else:
- self.cbdController.computeNextBlock(self.strongComponentList[self.currentCompIdx], self.iteration)
- self.state = {b.getBlockName(): b.getSignal() for b in self.model.getBlocks()}
- ]]>
- </body>
- </method>
- <scxml initial="Main" final="SimulationComplete">
- <parallel id="Main">
- <state id="ExecuteSimulation" initial="Started">
- <state id="Stopped"/>
- <state id="Started">
- <transition target="../Initialized">
- <script>
- <![CDATA[
- self.initializeSimulation()
- ]]>
- </script>
- </transition>
- </state>
- <state id="Initialized">
- <transition target="../CheckTermination" />
- </state>
- <state id="CheckTermination">
- <transition target="../InitializingChild" cond="not self.endConditionSimulation()">
- <raise event="BigStep.Reset" />
- </transition>
- <transition target="../Stopped" cond="self.endConditionSimulation()">
- <script>
- print 'Simulation finished'
- self.finalizeSimulation()
- </script>
- </transition>
- </state>
- <state id="InitializingChild">
- <transition target="../Executing" event="BigStep.Initialized">
- <raise event="BigStep.Execute" />
- </transition>
- </state>
- <state id="Executing">
- <transition target="../CheckTermination" event="BigStep.Finished" />
- </state>
- </state>
- <state id="ExecuteBigStep" initial="Stopped">
- <state id="Stopped">
- <transition target="../Started" event="BigStep.Reset" />
- </state>
- <state id="Started">
- <transition target="../Initialized">
- <raise event="BigStep.Initialized" />
- <script>
- <![CDATA[
- self.initializeBigStep()
- ]]>
- </script>
- </transition>
- </state>
- <state id="Initialized">
- <transition target="../CheckTermination" event="BigStep.Execute" />
- </state>
- <state id="CheckTermination">
- <transition target="../InitializingChild" cond="not self.endConditionBigStep()">
- <raise event="SmallStep.Reset" />
- </transition>
- <transition target="../Stopped" cond="self.endConditionBigStep()">
- <script>
- print 'big step executed'
- self.finalizeBigStep()
- print 'Iteration: ' + str(self.iteration)
- </script>
- <raise event="BigStep.Finished" />
- </transition>
- </state>
- <state id="InitializingChild">
- <transition target="../Executing" event="SmallStep.Initialized">
- <raise event="SmallStep.Execute" />
- </transition>
- </state>
- <state id="Executing">
- <transition target="../CheckTermination" event="SmallStep.Finished" />
- </state>
- </state>
- <state id="ExecuteSmallStep" initial="Stopped">
- <state id="Stopped">
- <transition target="../Started" event="SmallStep.Reset" />
- </state>
- <state id="Started">
- <transition target="../Initialized">
- <raise event="SmallStep.Initialized" />
- <script>
- <![CDATA[
- self.initializeSmallStep()
- ]]>
- </script>
- </transition>
- </state>
- <state id="Initialized">
- <transition target="../CheckTermination" event="SmallStep.Execute"/>
- </state>
- <state id="CheckTermination">
- <transition target="../InitializingChild" cond="not self.endConditionSmallStep()">
- <raise event="Block.Reset" />
- </transition>
- <transition target="../Stopped" cond="self.endConditionSmallStep()">
- <script>
- print 'small step executed'
- self.finalizeSmallStep()
- </script>
- <raise event="SmallStep.Finished" />
- </transition>
- </state>
- <state id="InitializingChild">
- <transition target="../Executing" event="Block.Initialized">
- <raise event="Block.Execute" />
- </transition>
- </state>
- <state id="Executing">
- <transition target="../CheckTermination" event="Block.Finished">
- <script>
- self.small_step_executed = True
- </script>
- </transition>
- </state>
- </state>
- <state id="ExecuteBlock" initial="Stopped">
- <state id="Stopped">
- <transition target="../Started" event="Block.Reset" />
- </state>
- <state id="Started">
- <transition target="../Initialized">
- <raise event="Block.Initialized" />
- </transition>
- </state>
- <state id="Initialized">
- <transition target="../Executing" event="Block.Execute" />
- </state>
- <state id="Executing">
- <transition target="../Stopped">
- <script>
- self.computeBlock()
- </script>
- <raise event="Block.Finished" />
- </transition>
- </state>
- </state>
- </parallel>
- </scxml>
- </class>
- </diagram>
|