Basic
Basic#
We will define a simple model of water creation from hydrogen and oxygen.
First,
we need to import EmptyCompartment and Species,
a Synthesis reaction,
and a Simulator to integrate the equations.
import numpy as np
from simbio.components import EmptyCompartment, Species
from simbio.reactions.single import Synthesis
from simbio.simulator import Simulator
To define the model,
we create 3 species,
corresponding to
hydrogen \(H₂\), oxygen \(O₂\), and water \(H₂O\),
with initial conditions of 1, 1 and 0,
respectively.
And we add a create_water reaction,
corresponding to \(2 H₂ + O₂ → 2 H₂ O\),
with a rate of 1.
class Model(EmptyCompartment):
H2: Species = 1
O2: Species = 1
H2O: Species = 0
create_water = Synthesis(A=2 * H2, B=O2, AB=2 * H2O, rate=1)
To simulate it,
we feed the model named Model into Simulator,
and use the .run() method.
It will compile and integrate an ODE-based simulation,
returning a pandas.DataFrame with the result:
simulator = Simulator(Model)
t = np.linspace(0, 10, 100)
df = simulator.run(t)
df.head()
| H2 | O2 | H2O | |
|---|---|---|---|
| time | |||
| 0.00000 | 1.000000 | 1.000000 | 0.000000 |
| 0.10101 | 0.838051 | 0.919025 | 0.161949 |
| 0.20202 | 0.728306 | 0.864153 | 0.271694 |
| 0.30303 | 0.647931 | 0.823965 | 0.352069 |
| 0.40404 | 0.585968 | 0.792984 | 0.414032 |
To visualize the time evolution,
we can use the DataFrame.plot method:
df.plot()
<AxesSubplot: xlabel='time'>