Exemplo n.º 1
0
def test_rt_slow_sim_default_behavior(log):
    """By default, SimPy should raise an error if a simulation is too
    slow for the selected real-time factor."""
    env = RealtimeEnvironment(factor=0.05)
    env.process(process(env, log, 0.1, 1))

    err = pytest.raises(RuntimeError, env.run, 3)
    assert 'Simulation too slow for real time (0.05' in err.value.args[0]
    assert log == []
Exemplo n.º 2
0
def test_rt_sync(log):
    """Test resetting the internal wall-clock reference time."""
    env = RealtimeEnvironment(factor=0.05)
    env.process(process(env, log, 0.01))
    time.sleep(0.06)  # Simulate massiv workload :-)
    env.sync()
    env.run(3)
Exemplo n.º 3
0
    def __init__(self):
        frequencyBand = FrequencyBand([FsplAttenuation])
        super(InvertedPendulumEnv, self).__init__(frequencyBand, deviceCount=2)

        # Observation depends on plant angle
        self.observation_space = spaces.Discrete(180)

        # Realtime environment for visualization
        SimMan.env = RealtimeEnvironment()

        # Setup plant and devices
        plant = SlidingPendulum(visualized=True)
        controller = InvertedPendulumPidController("Controller", 0, -1,
                                                   frequencyBand)
        sensor = AngleSensor("Sensor", frequencyBand, plant,
                             controller.macAddr, 0.001)  # 1 ms sample interval
        controller.sensorAddr = sensor.macAddr
        actuator = WagonActuator("Actuator", frequencyBand, plant)
        controller.actuatorAddr = actuator.macAddr
        self.plant = plant
        self.sensor = sensor
        self.actuator = actuator
        self.controller = controller

        self.deviceIndexToMacDict = {0: sensor.macAddr, 1: controller.macAddr}

        interpreter = InvertedPendulumInterpreter(self)
        self.rrm = SimpleRrmDevice("RRM", 0, 1, self.frequencyBand,
                                   self.deviceIndexToMacDict, interpreter)
Exemplo n.º 4
0
def test_rt_sync(log):
    """Test resetting the internal wall-clock reference time."""
    env = RealtimeEnvironment(factor=0.05)
    env.process(process(env, log, 0.01))
    time.sleep(0.06)  # Simulate massiv workload :-)
    env.sync()
    env.run(3)
Exemplo n.º 5
0
def test_rt_slow_sim_no_error(log):
    """Test ignoring slow simulations."""
    env = RealtimeEnvironment(factor=0.05, strict=False)
    env.process(process(env, log, 0.1, 1))

    start = monotonic()
    env.run(2)
    duration = monotonic() - start

    assert check_duration(duration, 2 * 0.1)
    assert log == [1]
Exemplo n.º 6
0
def test_rt(log, factor):
    """Basic tests for run()."""
    env = RealtimeEnvironment(factor=factor)
    env.process(process(env, log, 0.01, 1))
    env.process(process(env, log, 0.02, 1))

    start = monotonic()
    env.run(2)
    duration = monotonic() - start

    assert check_duration(duration, 2 * factor)
    assert log == [1, 1]
Exemplo n.º 7
0
def test_rt_multiple_call(log):
    """Test multiple calls to run()."""
    env = RealtimeEnvironment(factor=0.05)
    start = monotonic()

    env.process(process(env, log, 0.01, 2))
    env.process(process(env, log, 0.01, 3))

    env.run(5)
    duration = monotonic() - start

    # assert almost_equal(duration, 0.2)
    assert check_duration(duration, 5 * 0.05)
    assert log == [2, 3, 4]

    env.run(12)
    duration = monotonic() - start

    assert check_duration(duration, 12 * 0.05)
    assert log == [2, 3, 4, 6, 6, 8, 9, 10]
Exemplo n.º 8
0
    task_instance = task.popleft()
    finished = env.process(cpu.run_task_instance(task_instance))
    yield cpu.free
    if len(task):
        env.process(schedule(env, task, cpu))


if __name__ == "__main__":
    from simpy.rt import RealtimeEnvironment
    from simulations.job import Task
    from simulations.config import TaskConfig
    from collections import deque
    from simulations.config import LOG_BASE

    # print(LOG_BASE)
    cpu_config = CPUConfig(speed=1,
                           frequency=2.3e9,
                           cache=4e6,
                           power_consumption=65,
                           runtime=None)
    env = RealtimeEnvironment(factor=0.01, strict=False)
    cpu = CPU(cpu_config, env)
    task = deque([])
    taskconfigs = []
    for i in range(10):
        taskconfigs.append([i, 3000000, 20, None, 70, 10000, 10000, None])
    taskconfig = TaskConfig(1, 0, taskconfigs)
    onetask = Task(env, taskconfig)
    env.process(schedule(env, deque(onetask.task_instances), cpu))
    env.run()
Exemplo n.º 9
0
def test_rt_illegal_until():
    """Test illegal value for *until*."""
    env = RealtimeEnvironment()
    err = pytest.raises(ValueError, env.run, -1)
    assert err.value.args[0] == ('until(=-1.0) should be > the current '
                                 'simulation time.')
Exemplo n.º 10
0
        if IS_LOG_ON:
            print '{:^75}'.format('SIMTIME: {}s \n'.format(env.now))

        if env.now == SIM_TIME - 1:
            print_results(bottle)

        yield env.timeout(1)


### CONFIG SIM ###
if IS_FAST_MODE:
    from simpy import Environment
    env = Environment()
else:
    env = RealtimeEnvironment(strict=False)

sim_state = SimState()
bottle = Bottle(env)

### REGISTER ALL PROCESSES ###
env.process(conductor(env, bottle, sim_state))


# If running from GUI, we want to start the sim in a different thread
def start():
    env.run(until=SIM_TIME)


if __name__ == '__main__':
    start()
Exemplo n.º 11
0
def test_run_with_untriggered_event(env):
    env = RealtimeEnvironment(factor=0.05)
    excinfo = pytest.raises(RuntimeError, env.run, until=env.event())
    assert str(excinfo.value).startswith('No scheduled events left but "until"'
                                         ' event was not triggered:')
Exemplo n.º 12
0
def test_run_with_untriggered_event(env):
    env = RealtimeEnvironment(factor=0.05)
    excinfo = pytest.raises(RuntimeError, env.run, until=env.event())
    assert str(excinfo.value).startswith('No scheduled events left but "until"'
                                         ' event was not triggered:')
Exemplo n.º 13
0
def test_rt_illegal_until():
    """Test illegal value for *until*."""
    env = RealtimeEnvironment()
    err = pytest.raises(ValueError, env.run, -1)
    assert str(err.value) == ('until(=-1) must be > the current '
                              'simulation time.')