Sfoglia il codice sorgente

Worked on reaching deadlines

rparedis 3 anni fa
parent
commit
ee870edd7e

+ 32 - 4
examples/scripts/BouncingBall/BouncingBall_experiment.py

@@ -5,7 +5,9 @@ import matplotlib.pyplot as plt
 
 bb = BouncingBall()
 
-TIME = 20.0
+DELTA = 0.1
+# TIME = 15.0
+TIME = 4
 
 fig = plt.figure(figsize=(5, 5), dpi=100)
 ax = fig.add_subplot(111)
@@ -19,15 +21,41 @@ manager.register("height", bb.getBlockByName('plot'), plot, LinePlot(color='red'
 from CBD.state_events import StateEvent, Direction
 from CBD.state_events.locators import ITPStateEventLocator
 
-def bounce(t, model):
-	print(t)
+def bounce(_, model):
 	model.bounce()
 
+import time
+pre = 0
+times = []
+lengs = []
+def prestep():
+	global pre
+	pre = time.time()
+
+def post():
+	times.append(sim.getTime())
+	lengs.append(time.time() - pre)
+
 sim = Simulator(bb)
 sim.setStateEventLocator(ITPStateEventLocator())
 sim.registerStateEvent(StateEvent("height", direction=Direction.FROM_ABOVE, event=bounce))
+sim.connect("prestep", prestep)
+sim.connect("poststep", post)
 sim.setRealTime()
-sim.setDeltaT(0.1)
+sim.setDeltaT(DELTA)
 sim.run(TIME)
 
 plt.show()
+
+fig = plt.figure(figsize=(5, 5), dpi=100)
+ax = fig.add_subplot(111)
+ax.set_xlim((0, TIME))
+
+ax.bar(times[2:], lengs[2:], width=0.05)
+ax.plot((0, TIME), (DELTA, DELTA), c='red', ls='--')
+
+ax2 = ax.twinx()
+ax2.set_ylim((0, 105))
+ax2.plot(*bb.getBlockByName('plot').data_xy, c='green')
+
+plt.show()

BIN
examples/scripts/BouncingBall/durations-no-stel.png


BIN
examples/scripts/BouncingBall/durations.png


+ 4 - 9
src/CBD/depGraph.py

@@ -166,7 +166,6 @@ class DepGraph:
 			if not self.hasMember(dependent):
 				raise ValueError("Specified dependent object is not member of this graph")
 			if not self.hasMember(influencer):
-				print(influencer)
 				raise ValueError("Specified influencer object is not member of this graph")
 
 	def hasDependency(self, dependent, influencer):
@@ -178,14 +177,10 @@ class DepGraph:
 			dependent:  The object which depends on the other
 			influencer: The object which influences the other
 		"""
-		if self.hasMember(dependent) and self.hasMember(influencer):
-			return influencer in self.__influencers[dependent] and \
-			       dependent in self.__dependents[influencer]
-		else:
-			if not self.hasMember(dependent):
-				raise ValueError("Specified dependent object is not member of this graph")
-			if not self.hasMember(influencer):
-				raise ValueError("Specified influencer object is not member of this graph")
+		assert self.hasMember(dependent), "Specified dependent object is not member of this graph"
+		assert self.hasMember(influencer), "Specified influencer object is not member of this graph"
+		return influencer in self.__influencers[dependent] and \
+		       dependent in self.__dependents[influencer]
 
 	def unsetDependency(self, dependent, influencer):
 		""" Removes a dependency between two objects.

+ 5 - 8
src/CBD/simulator.py

@@ -518,7 +518,6 @@ class Simulator:
 		# TODO: Must be set to "every time" instead.
 		if curIt < 2 or self.__sim_data[0] is None:
 			self.__sim_data[0] = createDepGraph(self.model, curIt)
-			# self.__sim_data[1] = self.__sim_data[0].getStrongComponents(curIt)
 		self.__sim_data[1] = self.__scheduler.obtain(self.__sim_data[0], curIt, self.getTime())
 		self.__computeBlocks(self.__sim_data[1], self.__sim_data[0], self.__sim_data[2])
 		self.__sim_data[2] += 1
@@ -527,10 +526,12 @@ class Simulator:
 			if not event.fired and self.__stel.detect_signal(event.output_name, event.level, event.direction):
 				event.fired = True
 				t = self.__stel.run(event.output_name, event.level, event.direction)
-				event.event(t, self.model)
 
 				# Reset the model
 				self.model._rewind()
+				event.event(t, self.model)
+
+				# reset to allow for new IC computation
 				self.model.clearSignals()
 				self.model.getClock().setStartTime(t)
 				self.__sim_data[0] = None
@@ -590,7 +591,6 @@ class Simulator:
 				self.__finish()
 				break
 
-			# self._update_clock()
 			self._do_single_step()
 
 			if self.__threading_backend_subsystem == Platform.GLA:
@@ -638,11 +638,8 @@ class Simulator:
 		assert len(component) >= 1, "A component should have at least one element"
 		if len(component) > 1:
 			return True
-		else:  # a strong component of size one may still have a cycle: a self-loop
-			if depGraph.hasDependency(component[0], component[0]):
-				return True
-			else:
-				return False
+		# a strong component of size one may still have a cycle: a self-loop
+		return depGraph.hasDependency(component[0], component[0])
 
 	def __progress_update(self):
 		"""