Coverage for D:\Ralf Gerlich\git\modypy\modypy\model\states.py : 38%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2States represent the memory of a dynamical system. In MoDyPy, states are always
3real-valued, and may be multi-dimensional.
5For each state, a derivative function may be defined which describes the
6evolution of the value of the state over time. The value of that derivative
7function may depend on the value of any system state or signal, which are made
8accessible by the :class:`DataProvider <modypy.model.evaluation.DataProvider>`
9object passed to it.
11States may also be updated by :mod:`event listeners <modypy.model.events>`.
13States are represented as instances of the :class:`State` class. In addition,
14:class:`SignalState` instances are states that are also signals.
15"""
16import functools
17import numpy as np
18import operator
19from modypy.model.ports import AbstractSignal, ShapeType
22class State:
23 """A state describes a portion of the state of a block.
25 Args:
26 owner: The owner of the state
27 derivative_function: The derivative function of the state
28 (Default: 0)
29 shape: The shape of the state (Default: 1)
30 initial_condition: The initial value of the state (Default: 0)
31 """
33 def __init__(self,
34 owner,
35 derivative_function=None,
36 shape=(),
37 initial_condition=None):
38 self.owner = owner
39 self.derivative_function = derivative_function
40 if isinstance(shape, int):
41 self.shape = (shape,)
42 else:
43 self.shape = shape
44 if initial_condition is None:
45 self.initial_condition = np.zeros(self.shape)
46 else:
47 self.initial_condition = np.asarray(initial_condition)
49 self.size = functools.reduce(operator.mul, self.shape, 1)
51 self.state_index = self.owner.system.allocate_state_lines(self.size)
52 self.owner.system.states.append(self)
54 @property
55 def state_slice(self):
56 """A slice object that represents the indices of this state in the
57 states vector."""
59 return slice(self.state_index,
60 self.state_index + self.size)
62 def __call__(self, system_state):
63 return system_state.get_state_value(self)
65 def set_value(self, system_state, value):
66 """
67 Update the value of this state in the given system state.
69 Args:
70 system_state: The system state to update
71 value: The value to set this state to
72 """
73 system_state.set_state_value(self, value)
76class SignalState(State, AbstractSignal):
77 """A state that also provides itself as an output signal."""
79 def __init__(self,
80 owner,
81 derivative_function=None,
82 shape: ShapeType = (),
83 initial_condition=None):
84 State.__init__(self,
85 owner=owner,
86 derivative_function=derivative_function,
87 shape=shape,
88 initial_condition=initial_condition)
89 AbstractSignal.__init__(self, shape)