%matplotlib inline
from ecell4.prelude import *

"""
Explanation: Action Potentials in Neurons
End of explanation
"""


citation(12991237)

Q10 = 3.0
GNa = 120.0 # mS/cm^2
GK = 36.0 # mS/cm^2
gL = 0.3 # mS/cm^2
EL = -64.387 # mV
ENa = 40.0 # mV
EK = -87.0 # mV
Cm = 1.0 # uF/cm^2

T = 6.3 # degrees C
Iext = 10.0 # nA

with reaction_rules():
    Q = Q10 ** ((T - 6.3) / 10)

    alpha_m = -0.1 * (Vm + 50) / (exp(-(Vm + 50) / 10) - 1)
    beta_m = 4 * exp(-(Vm + 75) / 18)
    ~m > m | Q * (alpha_m * (1 - m) - beta_m * m)

    alpha_h = 0.07 * exp(-(Vm + 75) / 20)
    beta_h = 1.0 / (exp(-(Vm + 45) / 10) + 1)
    ~h > h | Q * (alpha_h * (1 - h) - beta_h * h)

    alpha_n = -0.01 * (Vm + 65) / (exp(-(Vm + 65) / 10) - 1)
    beta_n = 0.125 * exp(-(Vm + 75) / 80)    
    ~n > n | Q * (alpha_n * (1 - n) - beta_n * n)

    gNa = (m ** 3) * h * GNa
    INa = gNa * (Vm - ENa)
    gK = (n ** 4) * GK
    IK = gK * (Vm - EK)
    IL = gL * (Vm - EL)
    ~Vm > Vm | (Iext - (IL + INa + IK)) / Cm

hhm = get_model()

show(hhm)

ret = run_simulation(100, ndiv=1000, model=hhm, y0={'Vm': -75})
ret.plot(y="Vm", ylabel="Membrane Potential")

"""
Explanation: Hodgkin-Huxley Model
End of explanation
"""


a = 0.7
b = 0.8
c = 12.5
Iext = 0.5

with reaction_rules():
    ~u > u | -v + u - (u ** 3) / 3 + Iext
    ~v > v | (u - b * v + a) / c

fnm = get_model()

show(fnm)

ret = run_simulation(200, ndiv=501, model=fnm)

ret

ret.plot(x='u', y='v', xlabel='u', ylabel='v', figsize=[6.0, 6.0])

"""
Explanation: FitzHugh–Nagumo Model
FitzHugh R, Mathematical models of threshold phenomena in the nerve membrane. Bull. Math. Biophysics, 17, 257—278, 1955.
End of explanation
"""


