I am running a fairly large system of ODEs using scipy's odeint. In order to keep the system coherent, easy to read and shorter I have divided most of it into classes to call upon each other. From the readability point of view it seems to work fine, however due to repeated calls to python objects it significantly slows down the actual ODE solver. I looked around into ways of optimizing the code to maintain some level of readability while also making it more efficient at calculations (either using Numba or using Sympy before converting it to numpy-aware lambda functions), however I have not been very successful so far. I was wondering what strategies could be helpful in this case. I have provided a simple version of the code below. Thank you in advance.
class NaL():
g = 0
def I(self, v, E): return self.g * (v - E) #Leak current
class Neuron():
C_m = 1.0 #membrane capacitance
V = 0 # Voltage
m, h = 0, 0 #activating variables
def I_Na(self): #Sodium Currents
return self.NaT.I(self.V, self.m, self.h)+ self.NaL.I(self.V)
def __init__(self):
self.NaT = NaT()
self.NaL = NaL()
self.NaT.g = 3000
self.NaL.g = 20
I1 = Neuron()
def Run(X,t):
I1.V, I1.m, I1.h = X
dydt = [(0 - I1.I_Na()) / I1.C_m, #dV/dt for neuron
I1.NaT.dmdt(I1.V, I1.m), #dm/dt for sodium channel
I1.NaT.dhdt(I1.V, I1.h) #dh/dt for sodium channel
]
return dydt
X = odeint(Run, [-70, 0.0050, 0.9961], t)