experiment.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Import code for model simulation:
  2. from pypdevs.simulator import Simulator
  3. # Import the model to be simulated
  4. from sccd.compiler.sccdc import xmlToSccd
  5. from sccdDEV import SCCD
  6. # ======================================================================
  7. # 1. Instantiate the (Coupled or Atomic) DEVS at the root of the
  8. # hierarchical model. This effectively instantiates the whole model
  9. # thanks to the recursion in the DEVS model constructors (__init__).
  10. #
  11. sccdObject = xmlToSccd("input.xml")
  12. sccdDEV = SCCD(sccdObject)
  13. # ======================================================================
  14. # 2. Link the model to a DEVS Simulator:
  15. # i.e., create an instance of the 'Simulator' class,
  16. # using the model as a parameter.
  17. sim = Simulator(sccdDEV)
  18. # ======================================================================
  19. # 3. Perform all necessary configurations, the most commonly used are:
  20. # A. Termination time (or termination condition)
  21. # Using a termination condition will execute a provided function at
  22. # every simulation step, making it possible to check for certain states
  23. # being reached.
  24. # It should return True to stop simulation, or Falso to continue.
  25. #def terminate_whenStateIsReached(clock, model):
  26. # return model.trafficLight.state.get() == "manual"
  27. #sim.setTerminationCondition(terminate_whenStateIsReached)
  28. # A termination time is prefered over a termination condition,
  29. # as it is much simpler to use.
  30. # e.g. to simulate until simulation time 400.0 is reached
  31. sim.setTerminationTime(400.0)
  32. # B. Set the use of a tracer to show what happened during the simulation run
  33. # Both writing to stdout or file is possible:
  34. # pass None for stdout, or a filename for writing to that file
  35. sim.setVerbose("Trace.txt")
  36. # C. Use Classic DEVS instead of Parallel DEVS
  37. # If your model uses Classic DEVS, this configuration MUST be set as
  38. # otherwise errors are guaranteed to happen.
  39. # Without this option, events will be remapped and the select function
  40. # will never be called.
  41. sim.setClassicDEVS()
  42. # ======================================================================
  43. # 4. Simulate the model
  44. sim.simulate()
  45. # ======================================================================
  46. # 5. (optional) Extract data from the simulated model
  47. print("Simulation terminated")