def __init__(self, config): super(SimEnvironment, self).__init__() #: The configuration dictionary. self.config = config #: The pseudo-random number generator; an instance of #: :class:`random.Random`. self.rand = random.Random() seed = config.setdefault('sim.seed', None) if six.PY3: self.rand.seed(seed, version=1) else: self.rand.seed(seed) timescale_str = self.config.setdefault('sim.timescale', '1 s') #: Simulation timescale ``(magnitude, units)`` tuple. The current #: simulation time is ``now * timescale``. self.timescale = parse_time(timescale_str) duration = config.setdefault('sim.duration', '0 s') #: The intended simulation duration, in units of :attr:`timescale`. self.duration = scale_time(parse_time(duration), self.timescale) #: The simulation runs "until" this event. By default, this is the #: configured "sim.duration", but may be overridden by subclasses. self.until = self.duration #: From 'meta.sim.index', the simulation's index when running multiple #: related simulations or `None` for a standalone simulation. self.sim_index = config.get('meta.sim.index') #: :class:`TraceManager` instance. self.tracemgr = TraceManager(self)
def __init__(self, config): super(SimEnvironment, self).__init__() #: The configuration dictionary. self.config = config #: The pseudo-random number generator; an instance of #: :class:`random.Random`. self.rand = random.Random() seed = config.setdefault('sim.seed', None) if six.PY3: self.rand.seed(seed, version=1) else: self.rand.seed(seed) timescale_str = self.config.setdefault('sim.timescale', '1 s') #: Simulation timescale ``(magnitude, units)`` tuple. The current #: simulation time is ``now * timescale``. self.timescale = parse_time(timescale_str) duration = config.setdefault('sim.duration', '0 s') #: The intended simulation duration, in units of :attr:`timescale`. self.duration = scale_time(parse_time(duration), self.timescale) #: The simulation runs "until" this event. By default, this is the #: configured "sim.duration", but may be overridden by subclasses. self.until = self.duration #: From 'meta.sim.index', the simulation's index when running multiple #: related simulations or `None` for a standalone simulation. self.sim_index = config.get('meta.sim.index') #: :class:`TraceManager` instance. self.tracemgr = TraceManager(self)
def __init__(self, config): super(SimEnvironment, self).__init__() #: The configuration dictionary. self.config = config #: The pseudo-random number generator; an instance of #: :class:`random.Random`. self.rand = random.Random() if six.PY3: self.rand.seed(config['sim.seed'], version=1) else: self.rand.seed(config['sim.seed']) #: Simulation timescale `(magnitude, units)` tuple. The current #: simulation time is `env.now * env.timescale`. self.timescale = parse_time(self.config['sim.timescale']) #: The intended simulation duration, in units of `timescale`. self.duration = scale_time(parse_time(config['sim.duration']), self.timescale)
def time(self, t=None, unit='s'): """The current simulation time scaled to specified unit. :param float t: Time in simulation units. Default is :attr:`now`. :param str unit: Unit of time to scale to. Default is 's' (seconds). :returns: Simulation time scaled to to `unit`. """ target_scale = parse_time(unit) ts_mag, ts_unit = self.timescale sim_time = ((self.now if t is None else t) * ts_mag, ts_unit) return scale_time(sim_time, target_scale)
def time(self, t=None, unit='s'): """The current simulation time scaled to specified unit. :param float t: Time in simulation units. Default is :attr:`now`. :param str unit: Unit of time to scale to. Default is 's' (seconds). :returns: Simulation time scaled to to `unit`. """ target_scale = parse_time(unit) ts_mag, ts_unit = self.timescale sim_time = ((self.now if t is None else t) * ts_mag, ts_unit) return scale_time(sim_time, target_scale)
def _get_interval_period_s(config): period_str = config.setdefault('sim.progress.update_period', '1 s') return scale_time(parse_time(period_str), (1, 's'))
def test_parse_time_default(): assert parse_time('123', default_unit='ms') == (123, 'ms')
def test_parse_time_except(test_input): with pytest.raises(ValueError) as exc_info: parse_time(test_input) assert 'float' not in str(exc_info.value)
def test_parse_time(test_input, expected): m, u = parse_time(test_input) assert (m, u) == expected assert isinstance(m, type(expected[0]))
def test_parse_time_default(): assert parse_time('123', default_unit='ms') == (123, 'ms')
def test_parse_time_except(test_input): with pytest.raises(ValueError) as exc_info: parse_time(test_input) assert 'float' not in str(exc_info.value)
def test_parse_time(test_input, expected): m, u = parse_time(test_input) assert (m, u) == expected assert isinstance(m, type(expected[0]))
def _get_interval_period_s(config: ConfigDict) -> Union[int, float]: period_str: str = config.setdefault('sim.progress.update_period', '1 s') return scale_time(parse_time(period_str), (1, 's'))
def _get_interval_period_s(config): period_str = config.setdefault('sim.progress.update_period', '1 s') return scale_time(parse_time(period_str), (1, 's'))