SinGen_experiment.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/python3
  2. # This file was automatically generated from drawio2cbd with the command:
  3. # /home/red/git/DrawioConvert/__main__.py SinGen.xml -fav -F CBD -e SinGen -E delta=0.1
  4. from CBD.lib.interface.plottinghandler import PlotHandler, PlotManager, LinePlot
  5. from CBD.simulator import Simulator
  6. from SinGen import *
  7. import matplotlib.pyplot as plt
  8. DELTA_T = 0.1
  9. fig = plt.figure(figsize=(15, 5), dpi=100)
  10. ax = fig.add_subplot(111)
  11. ax.set_ylim((-1, 1))
  12. # ax = None
  13. cbd = SinGen("SinGen")
  14. manager = PlotManager()
  15. manager.register("sin", cbd.findBlock("plot")[0], (fig, ax), LinePlot())
  16. manager.connect('sin', 'update_event', lambda d, axis=ax: axis.set_xlim(PlotHandler.follow(d[0], 10.0, 0.0)))
  17. plt.show(block=False)
  18. # plotter = cbd.getBlockByName("plot")
  19. def term(*_):
  20. plt.draw()
  21. plt.pause(0.01)
  22. return not manager.is_opened()
  23. # Run the Simulation
  24. time = 10.0
  25. sim = Simulator(cbd)
  26. sim.setRealTime(True)
  27. sim.setDeltaT(DELTA_T)
  28. sim.setTerminationCondition(term)
  29. # plotter.set_animation(fig)
  30. # plotter.start_animation(fig)
  31. sim.run(time)
  32. # plotter.end_animation()
  33. log = sim.getDurationLog()
  34. fig2 = plt.figure()
  35. ax2 = fig2.subplots()
  36. ax2.set_title("Block Computation [Plotting Manager] (T = {:.2f}, dt = {:.2f})".format(time, DELTA_T))
  37. ax2.set_xlabel("Iterations")
  38. ax2.set_ylabel("Time")
  39. ax2.plot([-1, len(log)], [DELTA_T, DELTA_T], c='red')
  40. ax2.plot([-1, len(log)], [0.01, 0.01], c='green')
  41. ax2.bar(range(len(log)), log)
  42. plt.show()