from sympy import *
from sympy.plotting import plot
init_printing()

"""
Explanation: R1C1 Ecosystems Model Global Stability
In this notebook we explore the global stability of the one resource / one consumer (R1-C1) ecosystem model by looking at the structure of the isoclines.
End of explanation
"""


x = Symbol('x') # biomass of resouce species [biomassX]
y = Symbol('y') # biomass of consumer species [biomassY]

"""
Explanation: We start by defining the variables:
End of explanation
"""


r = Symbol('r') # growth rate [biomassX/(biomassX*second)]
k = Symbol('k') # carrying capacity [biomassX]
a = Symbol('a') # attack rate [biomassY/biomassX]
d = Symbol('d') # death rate due to predation [biomassX/(biomassY*second)]
e = Symbol('e') # conversion efficiency [biomassY/(biomassY*second)]
h = Symbol('h') # half saturaion [biomassY]
m = Symbol('m') # mortality [biomassY/(biomassY*second)]

"""
Explanation: We now define the parameters:
End of explanation
"""


deltaX = r * x * (1 - (x / k)) - d * (a * y * x)/(h+a*x) # [biomassX/second]
deltaY = (e * a * y * x)/(h+a*x) - m * y                 # [biomassY/second]
deltaX, deltaY

"""
Explanation: In the subsequent analyssi, it will be important that all of these parameters are assumed to be positive.
Now we define the r1-c1 equations:
End of explanation
"""


a*e*x*y/(a*x+h) < m*y

a*e*x/(a*x+h) < m

a*e*x < m*(a*x+h)

"""
Explanation: For both the resource and the consumer, the axes, $x = 0$ and $y = 0$, are isoclines. This means that the ecologicaly relevant first quadrant is globally stable; any ecology with initial conditions in the first quadrant (including the axes) will remain in the first quadrant (including the axes) for all future time.
Since the over all resources of the earth are finite, we know that the biomass of all species in an ecology will never get arbitrarily large. This means that, for any ecologically relevant model, the derivatives of the species biomass, $deltaX$ and $deltaY$, should be negative for very large, positive, values of both $x$ and $y$.
To show that the current r1-c1 model is not ecologically irrelevant, we begin by showing that for large $x$ and $y$ the $deltaX$ is eventually negative. To see this consider the term $-\frac{adxy}{ax + h}$. Since all of the parameters as well as the biomasses are all positive, this term is always negative.
Now consider the second term, $rx\left(1 - \frac{x}{k}\right)$ of $deltaX$. With $r$, $k$ and $x$ positive, this term will be negative whenever $k < x$. This means that for all positive values of $a$, $d$, $h$, $k$, and $y$, $deltaX$ will be negative, and hence the resource biomass will be declining, whenever $k < x$.
Now consider, $deltaY$, it will be negative whenever:
End of explanation
"""


