Model base class

pynamics.models.base

This module contains the model base class, which forms the template for every plant model supported by this package.

Classes:

Name Description
BaseModel

Model base class.

BaseModel(initial_state, input_dim, output_dim, input_labels=None, output_labels=None)

Bases: ABC

This is the parent class for every plant model supported by pynamics. While custom models are supported, they must all inherit from this class.

Parameters:

Name Type Description Default
initial_state ndarray

The system's initial state. Should be an array shaped (n, 1), where n is the number of state variables.

required
input_dim int

Number of system inputs.

required
output_dim int

Number of system outputs.

required
input_labels list[str] | None

List of input labels. If None is passed, the inputs will be given generic names.

None
output_labels list[str] | None

List of output labels. If None is passed, the outputs will be given generic names.

None

Attributes:

Name Type Description
x ndarray

The system's state vector.

input_dim int

Number of system inputs.

output_dim int

Number of system outputs.

state_dim int

Number of states in the state vector.

input_labels list[str]

List of input labels.

output_labels list[str]

List of output labels.

Methods:

Name Description
get_state

Get the current state vector.

get_output

Compute the system's output from the current state.

get_input

Get the current inputs.

set_input

Pass new inputs to the system.

update_state

Assign new values to the system's state vector.

eval

Compute the system's state derivative.

Warning

This is an abstract base class. It should not be used directly.

Source code in pynamics/models/base.py
def __init__(self, 
             initial_state: np.ndarray, 
             input_dim: int, 
             output_dim: int,
             input_labels: list[str] | None=None, 
             output_labels: list[str] | None=None) -> None:

    """
    Class constructor.
    """

    super().__init__();
    self.x = initial_state;
    self._dim_checks(input_dim, output_dim);
    self.input_dim = input_dim;
    self.output_dim = output_dim;
    self.state_dim = self.x.shape[0];
    self.input_labels = self._labels_check(input_labels, self.input_dim, "u");
    self.output_labels = self._labels_check(output_labels, self.output_dim, "y");

    return;

eval() abstractmethod

Compute the system's state derivative.

Abstract method. Implementation details may vary with the model.

Source code in pynamics/models/base.py
@abstractmethod
def eval(self) -> np.ndarray:
    """
    Compute the system's state derivative.

    Abstract method. Implementation details may vary with the model.
    """

    pass

get_input() abstractmethod

Access the system's input.

Abstract method. Implementation details may vary with the model.

Source code in pynamics/models/base.py
@abstractmethod
def get_input(self) -> np.ndarray:
    """
    Access the system's input.

    Abstract method. Implementation details may vary with the model.
    """

    pass

get_output() abstractmethod

Compute the system's output from the current state vector.

Abstract method. Implementation details may vary with the model.

Source code in pynamics/models/base.py
@abstractmethod
def get_output(self) -> np.ndarray:
    """
    Compute the system's output from the current state vector.

    Abstract method. Implementation details may vary with the model.
    """

    pass

get_state() abstractmethod

Access the system's state.

Abstract method. Implementation details may vary with the model.

Source code in pynamics/models/base.py
@abstractmethod
def get_state(self) -> np.ndarray:
    """
    Access the system's state.

    Abstract method. Implementation details may vary with the model.
    """

    pass

set_input(u) abstractmethod

Pass a new set of inputs (references, control actions, etc.) to the system.

Abstract method. Implementation details may vary with the model.

Source code in pynamics/models/base.py
@abstractmethod
def set_input(self, u: np.ndarray | float) -> None:
    """
    Pass a new set of inputs (references, control actions, etc.) to the system.

    Abstract method. Implementation details may vary with the model.
    """

    pass

update_state() abstractmethod

Assign new values to the system's state vector.

Abstract method. Implementation details may vary with the model.

Source code in pynamics/models/base.py
@abstractmethod
def update_state(self) -> None:
    """
    Assign new values to the system's state vector.

    Abstract method. Implementation details may vary with the model.
    """

    pass