dynode¶
A framework for modelling and simulation of dynamical systems in the form of ordinary differential equations.
-
class
dynode.
Recorder
¶ Generic recorder functor.
Can be used as an observer in conjunction with a Simulation.
-
property
results
¶ The recorded results
- Returns
The recorded results as a dict
- Return type
dict
-
store
(system: Type[dynode.system.SystemInterface], attribute: str, alias: Optional[str] = None) → None¶ Register a variable/parameter of a system for storing.
- Parameters
system (Type[SystemInterface]) – System that owns this variable/parameter
attribute (str) – A string of the form x.y.z pointing to the variable/parameter to be stored
alias (Optional[str], optional) – A string under which the stored attribute will be available at. Defaults to None.
- Raises
AttributeError – If the provided attribute does not exist on system
-
property
-
class
dynode.
Simulation
¶ A simulation.
Representing a set of connected or unconnected dynamical systems that can be stepped forward in time by a numerical integration scheme.
-
add_observer
(observer: Callable) → None¶ Add an observer to the simulation.
An observer is a callable of the form:
def observer(t : int, states : np.ndarray) -> None:
An observer can return True to signal that the simulation should break early:
def observer(t : int, states : np.ndarray) -> bool: return True
- Parameters
observer (Callable) – A callable that will be called every observer_dt.
- Raises
ValueError – If observer has already been added to the simulation.
- Returns
- A de-registering function. By calling this, observer
will be removed from the list of observers.
- Return type
Callable
-
add_system
(system: Type[dynode.system.SystemInterface]) → None¶ Adds a system to the simulation.
- Parameters
system (Type[SystemInterface]) – The system to add, adhering to the interface provided by SystemInterface.
- Raises
ValueError – If system has already been added to the simulation.
-
simulate
(t: float, observer_dt: float, fixed_step: bool = False, integrator: str = 'dopri5', **kwargs) → int¶ Perform simulation.
Step the collection of systems forward in time, t seconds while informing any observer`s about the progress every `observer_dt interval. If fixed_step=True, observer_dt is also used as the internal step size of the solver, leaving the user in charge of choosing a reasonable step size for the problem at hand.
- Parameters
t (float) – Total time to progress
observer_dt (float) – Timestep with which the observers will be invoked.
fixed_step (bool, optional) – If True, observer_dt is used as the internal step size of the solver. Defaults to False.
integrator (str, optional) – Which solver to use, this argument is directly forwarded to scipy.integrate.ode. Defaults to “dopri5”.
- Raises
RuntimeError – If there are no systems added to the simulation
RuntimeError – If there are no states/ders to be integrated
RuntimeError – The solver fails due to numerical instabilities
- Returns
The current time of the simulation.
- Return type
int
-
property
systems
¶ Systems part of this simulation
- Returns
A list of the systems added to this simulation
- Return type
List[SystemInterface]
-
-
class
dynode.
SystemInterface
¶ Abstract Base Class (ABC) defining the System Interface.
A child class must implement the do_step method.
-
add_post_connection
(connection_func: Callable, dependees: Optional[List[Callable]] = None) → Callable¶ Add a post-connection to the system.
connection_func is a callable of the form:
def connection_func(system : SystemInterface, time : int): pass
- Parameters
connection_func (Callable) – The connection function to add
dependees (Optional[List[Callable]], optional) – A list of dependees that connection_func depends on. A topological sort will be performed to ensure all post-connections are executed in the correct order. Defaults to None.
- Raises
ValueError – If connection_func has already been added.
- Returns
- A de-registering function. By calling this, connection_func
will be removed from the list of post-connections.
- Return type
Callable
-
add_pre_connection
(connection_func: Callable, dependees: Optional[List[Callable]] = None) → Callable¶ Add a pre-connection to the system.
connection_func is a callable of the form:
def connection_func(system : SystemInterface, time : int): pass
- Parameters
connection_func (Callable) – The connection function to add
dependees (Optional[List[Callable]], optional) – A list of dependees that connection_func depends on. A topological sort will be performed to ensure all pre-connections are executed in the correct order. Defaults to None.
- Raises
ValueError – If connection_func has already been added.
- Returns
- A de-registering function. By calling this, connection_func
will be removed from the list of pre-connections.
- Return type
Callable
-
add_subsystem
(sub_system: Type[dynode.system.SystemInterface]) → None¶ Add a subsystem to this system
- Parameters
sub_system (Type[SystemInterface]) – The subsystem to add
- Raises
ValueError – If the subsystem is self
-
property
connections
¶ All connection callbacks registered to this system.
Sorted in topological order
- Returns
Sorted list of connection callbacks
- Return type
List[Callable]
-
property
ders
¶ The derivatives of this system
Readable and writeable as both attributes and keys. Must be of scalar or sequence type!
- Returns
Derivatives of this system
- Return type
VariableContainer
-
abstract
do_step
(time: float)¶ Abstract method to be implemented by child classes!
- Parameters
time (float) – Current simulation time
-
property
inputs
¶ The inputs of this system
Readable and writeable as both attributes and keys. Can be any type.
- Returns
Inputs of this system
- Return type
ParameterContainer
-
property
outputs
¶ The outputs of this system
Readable and writeable as both attributes and keys. Can be any type.
- Returns
Outputs of this system
- Return type
ParameterContainer
-
property
states
¶ The states of this system
Readable and writeable as both attributes and keys. Must be of scalar or sequence type!
- Returns
States of this system
- Return type
VariableContainer
-
property
subsystems
¶ The subsystems of this system
- Returns
- A list of subsystems adhering to the
SystemInterface interface
- Return type
List[Type[SystemInterface]]
-
-
dynode.
connect_signals
(first: Union[dynode.containers.VariableContainer, dynode.containers.ParameterContainer], first_key: str, second: Union[dynode.containers.VariableContainer, dynode.containers.ParameterContainer], second_key: str) → Callable¶ Explicitly connect two variables/parameters.
Returns a callable that connects first_key of first to second_key of second. The callable can be used as a pre-step and/or post-step connection. Connecting state x on sys1 to input a on sys2:
sys1.add_post_connection( connect_signals(sys1.states, "x", sys2.inputs, "a") )
- Parameters
first (Union[VariableContainer, ParameterContainer]) – The container to connect from
first_key (str) – The variable name in first to connect from
second (Union[VariableContainer, ParameterContainer]) – The container to connect to
second_key (str) – The variable name in second to connect to
- Returns
A connection callable
- Return type
Callable