-
Notifications
You must be signed in to change notification settings - Fork 0
ODEModel
The ODEModel class provides a concrete implementation of an ODE system model, allowing users to define, manipulate, and evaluate systems of ordinary differential equations (ODEs). It extends the abstract base class ODEModelBase to enforce a consistent interface and structure.
This documentation explains the purpose and usage of the ODEModel class, including examples and details of its methods and attributes.
- Abstract Base Class:
ODEModelBase -
Concrete Class:
ODEModel - Examples
- Purpose of the Abstract Base Class
The ODEModelBase serves as a blueprint for ODE models, ensuring any concrete implementation provides the following methods:
-
set_equations: Define the ODE equations. -
set_variables: Define the dependent variables. -
set_parameters: Define the system parameters. -
set_constraints: Define constraints for the model. -
evaluate: Evaluate the ODE system at a given point.
from abc import ABC, abstractmethod
class ODEModelBase(ABC):
def __init__(self):
self.equations = []
self.variables = []
self.parameters = {}
self.constraints = []
self.initial_conditions = {}
@abstractmethod
def set_equations(self, equations):
pass
@abstractmethod
def set_variables(self, variables):
pass
@abstractmethod
def set_parameters(self, parameters):
pass
@abstractmethod
def set_constraints(self, constraints):
pass
@abstractmethod
def evaluate(self, y):
pass-
equations: List ofsympy.Exprobjects representing the ODE system. -
variables: List of strings representing the dependent variables. -
parameters: Dictionary of parameter names and their numeric values. -
constraints: List ofsympyconstraints (Eq,Lt,Gt,Le,Ge). -
initial_conditions: Dictionary mapping variables to their initial values.
Initializes the model with equations, variables, parameters, constraints, and initial conditions.
Parameters:
-
equations: List of strings representing the ODE equations. -
variables: List of variable names as strings. -
parameters: Dictionary of parameter names and their values. -
constraints(optional): List of constraints as strings. -
initial_conditions(optional): Initial conditions as strings or a dictionary.
Example:
model = ODEModel(
equations=["dx/dt = -k * x", "dy/dt = k * x - r * y"],
variables=["x", "y"],
parameters={"k": 0.1, "r": 0.05},
constraints=["k > 0", "r > 0"],
initial_conditions={"x": 1.0, "y": 0.0}
)Loads an ODE model from a JSON file.
Example:
model = ODEModel.from_json("path/to/model.json")Loads an ODE model from a YAML file.
Example:
model = ODEModel.from_yaml("path/to/model.yaml")Loads an ODE model from a dictionary.
Example:
model_data = {
"equations": ["dx/dt = -k * x"],
"variables": ["x"],
"parameters": {"k": 0.1},
"constraints": ["k > 0"]
}
model = ODEModel.from_dict(model_data)Evaluates the ODE system at a given point.
Parameters:
-
y: List of values for the dependent variables.
Returns:
- List of evaluated derivatives.
Example:
values = [1.0, 0.5]
derivatives = model.evaluate(values)Plots the solution of the ODE system.
Parameters:
-
independent_var: Array-like object for the independent variable. -
dependent_vars: List of array-like objects for the dependent variables. - Other optional parameters for customization.
Example:
model.graph(time, [x_values, y_values], title="ODE Solution")The ODEModelBase ensures any concrete implementation of an ODE model provides a consistent API. This is particularly useful for:
- Code Reusability: Enforcing a standard interface across multiple ODE model implementations.
- Flexibility: Allowing different concrete classes to extend the base class and add specific features.
- Error Prevention: Ensuring that essential methods are implemented, reducing the risk of runtime errors.