Core¶
The base primitives every other subpackage builds on.
SimEnvironment¶
Owns the simulation clock, the priority event queue, and the list of
registered entities. Stepped via env.run() or env.run(until=t).
Entity¶
The base class for anything that lives in a SimEnvironment. Override
on_register(env) for setup, tick(env) for per-step work, and
on_unregister(env) for teardown. Entities also carry a history
list that subclasses populate however they like.
Clock and EventQueue¶
Lower-level building blocks. Most users never touch these directly —
the SimEnvironment wraps them. They become useful when you want to
schedule a one-shot event in the future:
API¶
Core runtime: clock, scheduler, entity, environment, logging.
Clock
dataclass
¶
Fixed-step simulation clock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
float
|
Initial simulation time. |
0.0
|
dt
|
float
|
Fixed timestep. Units are whatever the rest of the model uses (typically seconds, but can be hours, days, or dimensionless ticks). |
1.0
|
end
|
float | None
|
Optional end time. If provided, :meth: |
None
|
EventQueue
¶
Min-heap priority queue of scheduled callbacks.
Source code in src/simweave/core/scheduler.py
schedule
¶
schedule(time: float, callback: Callable[..., Any], *args: Any, **kwargs: Any) -> ScheduledEvent
Schedule callback(*args, **kwargs) to fire at simulation time.
Source code in src/simweave/core/scheduler.py
peek_time
¶
pop_due
¶
Yield every non-cancelled event whose time is <= now.
Source code in src/simweave/core/scheduler.py
cancel
¶
cancel(event: ScheduledEvent) -> None
ScheduledEvent
dataclass
¶
ScheduledEvent(time: float, seq: int, callback: Callable[..., Any], args: tuple = (), kwargs: dict = dict(), cancelled: bool = False)
Heap-ordered event record.
seq is a tiebreaker so that two events at the same time fire in
insertion order, giving deterministic behaviour for simultaneous events.
Entity
¶
Base class for simulation entities.
Source code in src/simweave/core/entity.py
on_register
¶
tick
¶
Advance this entity by dt simulation seconds.
Subclasses should call super().tick(dt, env) first (to keep the
age counter correct) before doing their own logic.
Source code in src/simweave/core/entity.py
has_work
¶
Report whether this entity has pending work.
The default is False -- plain items waiting in a queue are not
themselves "doing work" in the skip-idle-gaps sense; the queue/service
holding them is.
Source code in src/simweave/core/entity.py
reset_id_counter
classmethod
¶
SimEnvironment
¶
Container for a clock, an event queue, and registered processes.
Source code in src/simweave/core/environment.py
register
¶
Register a process so it is ticked each simulation step.
Source code in src/simweave/core/environment.py
step
¶
Process events due at t, tick every process, then advance.
Source code in src/simweave/core/environment.py
run
¶
Run the simulation until until or clock.end.
Source code in src/simweave/core/environment.py
Process
¶
Bases: Protocol
A process that participates in the tick loop.
SimTimeAxis
¶
SimTimeAxis(start: str | datetime, tick_unit: str = 'days', tick_size: float = 1.0, date_format: str | None = None)
Map simulation tick time to real-world calendar dates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
str | datetime
|
Calendar date/time that corresponds to simulation |
required |
tick_unit
|
str
|
Duration of one simulation time unit. Must be one of
|
'days'
|
tick_size
|
float
|
Scale factor: how many |
1.0
|
date_format
|
str | None
|
:func: |
None
|
Examples:
Source code in src/simweave/core/time_axis.py
to_datetime
¶
Convert a scalar simulation time to a :class:~datetime.datetime.
to_datetimes
¶
Convert an iterable of simulation times to :class:~datetime.datetime objects.
The returned list is suitable for Plotly's x parameter; Plotly
renders datetime objects natively with automatic axis formatting.
Source code in src/simweave/core/time_axis.py
label
¶
labels
¶
apply_to_figure
¶
Replace numeric tick values on axis with calendar dates.
Iterates through every trace in fig.data and, wherever the
nominated axis data is a numeric array, substitutes
:class:~datetime.datetime objects. Plotly then renders the axis
as a date axis with its own smart tick-label formatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Any
|
A |
required |
axis
|
str
|
|
'x'
|
title
|
str | None
|
Optional replacement axis title. If |
None
|
Returns:
| Type | Description |
|---|---|
The same figure object (modified in-place), so calls can be chained::
|
fig = plot_fleet_availability(rec) fig = time_axis.apply_to_figure(fig, title="Calendar date") fig.show() |
Source code in src/simweave/core/time_axis.py
tick_for_date
¶
Return the simulation tick corresponding to a given calendar date.
Useful for scheduling events at specific real-world dates::
t_start = tax.tick_for_date("2027-03-01")
env.schedule_at(t_start, my_callback)
Source code in src/simweave/core/time_axis.py
configure
¶
configure(level: int | str = INFO, fmt: str = '%(asctime)s [%(levelname)s] %(name)s: %(message)s', force: bool = False) -> None
Configure the simweave logger hierarchy. Safe to call many times.
Source code in src/simweave/core/logging.py
get_logger
¶
Return a logger under the simweave namespace.