Skip to content

Commit b48718a

Browse files
committed
custom functions and system params
1 parent 5fda024 commit b48718a

16 files changed

+1650
-393
lines changed

docs/components/ODE_systems.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Introduction
2+
3+
Systems of ordinary differential equations (ODEs) capture dynamic mechanistic models.
4+
5+
!!! info "Defintion: mechanistic model"
6+
7+
A mechanistic dynamic system model is of the form
8+
9+
$$
10+
\frac{d \underline{x}}{dt} = \underline{f}(\underline{x}, t, \underline{b})
11+
$$
12+
13+
where $\underline{x} = \underline{x}(t)$ are *system states* or *variables* and $\underline{b} = \underline{b}(t)$ are *external dependencies*.
14+
15+
Systems of ODEs are stored as a `VariablePortedObject` class. These ported objects store a set of ODEs, each an instance of the `DifferentialAssignment` class, and can expose the following ports:
16+
17+
- `InputPort`, capturing the external dependencies $\underline{b}$,
18+
- `VariablePort`, which expose one of the variables $\underline{x}$.
19+
20+
The `VariablePortedObject` class is a key user-facing class.
21+
22+
# Example
23+
24+
Consider the following predator-prey system.
25+
26+
!!! example "Example: predator-prey system"
27+
28+
A two-species predator prey system has the form
29+
30+
$$
31+
\begin{align}
32+
\frac{dx}{dt} &= \alpha x - \beta xy \\
33+
\frac{dy}{dt} &= \gamma xy - \delta x
34+
\end{align}
35+
$$
36+
37+
where:
38+
39+
* $\alpha > 0$ is the birth rate of prey population $x$,
40+
* $\delta>0$ is the death rate of predator population $y$,
41+
* $\beta>0$ is the predation rate of $y$ on $x$,
42+
* $\gamma>0$ is the response rate of $y$ from the predation on $x$.
43+
44+
In `psymple`, this system can be captured as follows.
45+
46+
``` py title="predator-prey as a VariablePortedObject"
47+
from psymple.ported_objects import VariablePortedObject
48+
49+
pred_prey = VariablePortedObject(
50+
name="pred_prey",
51+
input_ports=["a","b","c","d"],
52+
variable_ports=["x","y"], # (1)!
53+
assignments=[("x", "a*x - b*x*y"), ("y", "c*x*y - d*x")],
54+
)
55+
```
56+
57+
1. Specifying variable ports does not create variables: the variables are created as the left-hand side of the assignments. Variable ports allow the variables created in assignments to be read or updated by other system components.
58+
59+
In this form, the parameters $a$, $b$, $c$ and $d$ are required inputs. To specify default values, a list of tuples, dictionaries or `InputPort` instances can be passed to the `input_ports` parameter. The following would be equivalent calls in this case:
60+
61+
=== "tuple input"
62+
63+
```py
64+
input_ports = [("a", 10), ("b", 20), ("c", 15), ("d", 5)]
65+
```
66+
67+
=== "dictionary input"
68+
69+
```py
70+
input_ports = [
71+
dict(name="a", default_value=10),
72+
dict(name="b", default_value=20),
73+
dict(name="c", default_value=15),
74+
dict(name="d", default_value=5),
75+
]
76+
```
77+
78+
=== "`InputPort` input"
79+
80+
```py
81+
from psymple.ported_objects import InputPort
82+
83+
input_ports = [
84+
InputPort(name="a", default_value=10),
85+
InputPort(name="b", default_value=20),
86+
InputPort(name="c", default_value=15),
87+
InputPort(name="d", default_value=5),
88+
]
89+
```
Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
11
# Introduction
22

3-
Differential equations, along with functions, are the core building blocks of a `psymple` dynamic system.
3+
Differential equations are a core building blocks of a `psymple` system.
44

55
!!! info "Definition"
66

77
A differential equation in `psymple` is a first-order differential equation of the form
88

99
$$
10-
\frac{dy}{dt} = f(y,t,\underline{x}),
10+
\frac{dx}{dt} = f(x,t,\underline{b}),
1111
$$
1212

13-
where $\underline{x}$ is a vector of to-be-defined dependencies.
13+
where $\underline{b} = \underline{b}(t)$ are external dependencies.
1414

15-
Differential equations are stored as a [DifferentialAssignment](docs.assignments.md#differential-assignment) class
15+
Differential equations are stored as a [DifferentialAssignment](docs.assignments.md#differential-assignment) class. In most cases, users will not interact directly with the `DifferentialAssignment` class, since they can be created automatically.
1616

17+
## Example
18+
19+
The logistic model is given by
20+
21+
$$
22+
\frac{dx}{dt} = rx \left( 1-\frac{x}{k} \right)
23+
$$
24+
25+
In `psymple` this equation is captured as follows.
26+
27+
```py title="Logistic equation as a DifferentialAssignment"
28+
from psymple.variables import Variable
29+
from psymple.ported_objects import DifferentialAssignment
30+
31+
pop_x = Variable(symbol="x",
32+
initial_value=100,
33+
description="Variable for population x",
34+
)
35+
36+
assg_x = DifferentialAssignment(
37+
symbol_container=pop_x,
38+
expression="r*x*(1-x/k)",
39+
)
40+
```
41+
42+
If only the symbol for $x$ needs to be specified, the call for `DifferentialAssignment` can be streamlined.
43+
44+
```py3 title="Logistic equation as a DifferentialAssignment"
45+
from psymple.ported_objects import DifferentialAssignment
46+
47+
assg_x = DifferentialAssignment("x", "r*x*(1-x/k)") # (1)!
48+
```
49+
50+
1. `DifferentialAssignment` accepts the variable and expression as the first and second positional arguments, respectively.

docs/modular_components.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# Modular components
22

3+
4+

docs/systems.md

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,94 @@
11
# Modelling Systems
22

3-
The package `psymple` facilitates the building and simulation of temporal dynamical systems models. It allows for a spectrum of models from purely mechanistic models to purely correlative models to be constructed by allowing arbitrary combinations of ordinary differential equations (ODEs) and multivariate functions to be combined together.
3+
The package `psymple` implements the building and simulation of temporal dynamical systems models. While the package is designed to allow for the implementation of very general modelling paradigms, it was developed in response to the need for hybrid temporal ecological modelling.
44

5-
!!! info "Defintion"
5+
Temporal ecological modelling has classically been approached from two different schools of thought:
6+
7+
1. Mechanistic modelling, in which biological or physical principles are used to capture the flow of energy or resource between different species and systems in order to model population dynamics.
8+
2. Correlative modelling, in which population distribution is modelled as a function of climatic predictor variables such as temperature, humidity and rainfall.
9+
10+
Since biological processes are complex and intricate, mechanistic modelling of ecological systems is not widely persued due to concerns about development time and accuracy. In contrast, correlative modelling is typically derived using multiple regression and machine learning techniques using observational data.
11+
12+
Correlative modelling has a natural limitation when used for predictive purposes in geographic or climatic ranges outside of the observational data. This becomes a particular concern when, for example, using correlative modelling to predict invasive species potential in new environments.
13+
14+
In response, and with the advances in computing capability, there has been growing interest in hybrid or spectrum models, which compose features of both mechanistic and correlative models. This package is designed to facilitate the construction of these models by easily allowing arbitrary combinations of mechanistic and correlative components.
15+
16+
## Mechanistic and correlative models
17+
18+
### Mechanistic models
19+
20+
Mechanistic models are dynamic models in which the evolution of the system is specified as a system of first-order ordinary differential equations (ODEs).
21+
22+
!!! info "Defintion: mechanistic model"
623

724
A mechanistic dynamic system model is of the form
825

926
$$
1027
\frac{d \underline{x}}{dt} = \underline{f}(\underline{x}, t, \underline{b})
1128
$$
1229

13-
where $\underline{x}$ is a vector of *system states* or *variables*.
30+
where $\underline{x} = \underline{x}(t)$ are *system states* or *variables* and $\underline{b} = \underline{b}(t)$ are *external dependencies*.
31+
32+
A familiar example of a mechanistic model is the Lotka-Volterra or predator-prey system.
33+
34+
!!! example "Example: predator-prey system"
35+
36+
A two-species predator prey system has the form
37+
38+
$$
39+
\begin{align}
40+
\frac{dx}{dt} &= \alpha x - \beta xy \\
41+
\frac{dy}{dt} &= \gamma xy - \delta x
42+
\end{align}
43+
$$
44+
45+
where:
46+
47+
* $\alpha > 0$ is the birth rate of prey population $x$,
48+
* $\delta>0$ is the death rate of predator population $y$,
49+
* $\beta>0$ is the predation rate of $y$ on $x$,
50+
* $\gamma>0$ is the response rate of $y$ from the predation on $x$.
51+
52+
### Correlative model
53+
54+
A correlative model is a general term for a model in which the evolution is specified explicitly in terms of time and the system states.
55+
56+
!!! info "Definition: correlative model"
57+
58+
A correlative model is of the form
59+
60+
$$
61+
\underline{y} = \underline{f}(t, \underline{d})
62+
$$
63+
64+
where $\underline{y} = \underline{y}(t)$ is a vector of *system states* and $\underline{d} = \underline{d}(t)$ are *external dependencies*.
65+
66+
Correlative models can be used to approximate the behaviour of a system component with other system states.
67+
68+
!!! example "Example: correlative ecological niche models"
69+
70+
Data may be used to model the prevalance of a certain species $y$ in response to temporal climatic features such as temperature $T$, relative humitidy $H_r$ and precipitation $P$. In this case the model will have the form
71+
72+
$$
73+
y(t) = f(T, H_r, P)
74+
$$
75+
76+
where $T=T(t)$, $H_r = H_r(t)$ and $P=P(t)$ are known in terms of $t$.
77+
78+
79+
## Spectrum models
80+
81+
In `psymple`, spectrum models consistint of building blocks from purely mechanistic models and purely correlative models can be constructed by allowing arbitrary combinations of ordinary differential equations (ODEs) and multivariate functions to be combined together. A system in `psymple` is of the form
82+
83+
$$
84+
\begin{align}
85+
\frac{d \underline{x}}{dt} &= \underline{f}(\underline{x}, \underline{y}, t, \underline{b}) \\
86+
\underline{y} &= \underline{g}(t, \underline{d}) \\
87+
\underline{b} &= \underline{h}(t)
88+
\end{align}
89+
$$
90+
91+
1492

1593
## Principles
1694

@@ -27,7 +105,7 @@ The interface of ported objects and wires provides a separation from the equatio
27105
The python mathematics library [sympy](https://www.sympy.org/en/index.html) allows for system models to be built, stored and manipulated symbolically.
28106

29107
1. Clarity: any system component can produce its own system of equations for easy inspection and checking.
30-
2. Trust: models built using computer algebra removes mistakes made in building systems out of many parts.
108+
2. Dependanbility: models built using computer algebra removes mistakes in combining multiple components.
31109
2. Efficiency: systems built from many parts produce complex equations which can be automatically simplified to aid simulation.
32110

33111
### 3. Data-first design

mkdocs.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,31 @@ site_name: Psymple Docs
33

44
theme:
55
name: "material"
6+
icon:
7+
annotation: material/arrow-right-circle
8+
features:
9+
- content.tooltips
10+
- content.code.copy
11+
- content.code.annotate
612

713
markdown_extensions:
814
- admonition
915
- pymdownx.details
1016
- pymdownx.superfences
1117
- pymdownx.arithmatex:
1218
generic: true
19+
- attr_list
20+
- md_in_html
21+
- abbr
22+
- pymdownx.snippets
23+
- pymdownx.highlight:
24+
anchor_linenums: true
25+
line_spans: __span
26+
pygments_lang_class: true
27+
- pymdownx.inlinehilite
28+
- pymdownx.tabbed:
29+
alternate_style: true
30+
1331

1432
extra_javascript:
1533
- javascripts/mathjax.js
@@ -32,5 +50,6 @@ nav:
3250
- Tutorials: tutorials.md
3351
- Modelling Components:
3452
- Differential Equations: components/differential_equations.md
53+
- Systems of ODEs: components/ODE_systems.md
3554
- Reference:
3655
- Assignments: assignments.md

psymple/globals.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# The dictionary passed to sym.sympify and sym.lambdify to convert custom functions
88
# TODO: This should not be a global property.
99
#sym_custom_ns = {}
10+
global sym_custom_ns
1011
sym_custom_ns = {'DegreeDays': DegreeDays, 'FFTemperature': FFTemperature}
1112

1213

psymple/io/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .create_system import System_Creator

0 commit comments

Comments
 (0)