dinjo.model module

class dinjo.model.ModelIVP(state_variables: List[dinjo.model.StateVariable], parameters: List[dinjo.model.Parameter], t_span: Optional[List[float]] = None, t_steps: int = 50, t_eval: Optional[List[float]] = None)[source]

Bases: object

Defines and integrates an initial value problem.

state_variables
Type

list[StateVariable]

parameters
Type

list[Parameter]

t_span

Interval of integration (t0, tf). The solver starts with t=t0 and integrates until it reaches t=tf.

Type

2-tuple of floats

t_steps

The solver will get the solution for t_steps equally separated times from t0 to tf.

Type

int

t_span

List containing the time values in which the user wants to evaluate the solution. All the values must be within the interval defined by t_span.

Type

list[float]

__init__(state_variables: List[dinjo.model.StateVariable], parameters: List[dinjo.model.Parameter], t_span: Optional[List[float]] = None, t_steps: int = 50, t_eval: Optional[List[float]] = None)None[source]

Initialize self. See help(type(self)) for accurate signature.

build_model(t, y, *args)[source]

Defines the differntial equations of the model.

Override this method so that it contains the differential equations of your IVP. The signature of the method must be build_model(self, t, y, *args) where t is the time, y is the state vector, and args are other parameters of the system.

Parameters
  • t (float) – time at which the differential equation must be evaluated.

  • y (list[float]) – state vector at which the differential must be evaluated.

  • *args (any) – other parameters of the differential equation

Returns

  • The method must return the time derivative of the

  • state vector evaluated at a given time.

Note

The parameters must be defined in the same order in which the parameters are stored in ModelIVP.parameters.

Note

The state variable vector must be defined in the same order in which the state variables are stored in ModelIVP.state_variables.

Example

For example if you want to simulate a harmonic oscillator of frequency \(\omega\) and mass \(m\) this method must be implemented as follows:

def build_model(self, t, y, w, m):
    q, p = y

    # Hamilton's equations
    dydt = [
        p,                      # dq/dt
        - (w ** 2) * q          # dp/dt
    ]

    return dydt
property parameters_init_vals

Get the values of the model’s parameters initial values in theorder they are currently stored in self.parameters.

run_model(parameters: Optional[List[float]] = None, method: str = 'RK45')[source]

Integrate model using scipy.integrate.solve_ivp

Parameters
  • parameters (list[float]) – List of the values of the paramters of the initial value problem.

  • method (srt) – Integration method. Must be one of the methods accepted by scipy.integrate.solve_ivp

Returns

  • Bunch object with the following fields defined (same return type

  • as scipy.integrate.solve_ivp)

  • t (ndarray, shape (n_points,)) – Time points.

  • y (ndarray, shape (n, n_points)) – Values of the solution at t.

  • sol (OdeSolution or None) – Found solution as OdeSolution instance; None if dense_output was set to False.

  • t_events (list of ndarray or None) – Contains for each event type a list of arrays at which an event of that type event was detected. None if events was None.

  • y_events (list of ndarray or None) – For each value of t_events, the corresponding value of the solution. None if events was None.

  • nfev (int) – Number of evaluations of the right-hand side.

  • njev (int) – Number of evaluations of the Jacobian.

  • nlu (int) – Number of LU decompositions.

  • status (int) – Reason for algorithm termination:

property state_variables_init_vals: List[float]

Get the values of the model’s state variables initial values in the order they are currently stored in self.state_variables.

class dinjo.model.Parameter(name: str, representation: str, initial_value: float = 0, bounds: Optional[List[float]] = None, *args, **kwargs)[source]

Bases: dinjo.model.Variable

Represents a parameter of the differential equations definining an initial value problem.

In addition to the attributes defined in dinjo.model.Variable

bounds

list containing the minimum and maximum values that the parameter can take (min, max).

Type

2-tuple of floats.

__init__(name: str, representation: str, initial_value: float = 0, bounds: Optional[List[float]] = None, *args, **kwargs)None[source]

Initialize self. See help(type(self)) for accurate signature.

property bounds
class dinjo.model.StateVariable(name: str, representation: str, initial_value: float = 0, *args, **kwargs)[source]

Bases: dinjo.model.Variable

Represents a State Variable of an initial value problem.

class dinjo.model.Variable(name: str, representation: str, initial_value: float = 0, *args, **kwargs)[source]

Bases: object

Represents a variable

name

name of the variable.

Type

str

representation

string representing the state variable (could be the same as name.)

Type

str

initial_value

reference value of the variable being represented.

Type

float

__init__(name: str, representation: str, initial_value: float = 0, *args, **kwargs)None[source]

Initialize self. See help(type(self)) for accurate signature.