Commit ab325127 authored by Jan Fousek's avatar Jan Fousek

template-based generation of tvb-like classes

parent bbb48dab
Pipeline #2428 failed with stages
from ppi import G2DO
from mako.template import Template
template = Template(filename='template.py')
print(template.render( name='Generic2D',
const=G2DO.const,
limit=G2DO.limit,
sv=G2DO.state.split(),
drift=G2DO.drift,
input=G2DO.input.split() ) )
......@@ -4,9 +4,27 @@ from scipy.integrate import odeint
TRAJ_STEPS = 4096
class G2DO:
"Generic nonlinear 2-D (phase plane) oscillator."
state = 'W V'
limit = (-5, 5), (-5, 5)
input = 'c_0'
param = 'a'
const = {'tau': 1.0, 'I': 0.0, 'a': -2.0, 'b': -10.0, 'c': 0.0, 'd': 0.02,
'e': 3.0, 'f': 1.0, 'g': 0.0, 'alpha': 1.0, 'beta': 1.0,
'gamma': 1.0}
drift = (
'd * tau * (alpha*W - f*V**3 + e*V**2 + g*V + gamma*I + gamma*c_0)',
'd * (a + b*V + c*V**2 - beta*W) / tau'
)
diffs = 1e-3, 1e-3
obsrv = 'W', 'V'
class Oscillator:
def __init__(self, eta=0.07674, gamma=1.21, epsilon=12.3):
self.limit = (-5, 5), (-5, 5)
self.eta = eta
self.gamma = gamma
self.epsilon = epsilon
......@@ -20,6 +38,7 @@ class Oscillator:
return [dpsi1, dpsi2]
class ODEintAdapter:
def __init__(self, model):
self.model = model
......@@ -28,49 +47,48 @@ class ODEintAdapter:
return model.dfun(state)
model = Oscillator()
model_ode = ODEintAdapter(model)
y1 = np.linspace(-2.0, 2.0, 20)
y2 = np.linspace(-2.0, 2.0, 20)
if __name__ == '__main__':
model = Oscillator()
model_ode = ODEintAdapter(model)
Y1, Y2 = np.meshgrid(y1, y2)
y1 = np.linspace(-2.0, 2.0, 20)
y2 = np.linspace(-2.0, 2.0, 20)
Y1, Y2 = np.meshgrid(y1, y2)
u, v = np.zeros(Y1.shape), np.zeros(Y2.shape)
NI, NJ = Y1.shape
u, v = np.zeros(Y1.shape), np.zeros(Y2.shape)
for i in range(NI):
for j in range(NJ):
x = Y1[i, j]
y = Y2[i, j]
yprime = model.dfun([x, y])
u[i,j] = yprime[0]
v[i,j] = yprime[1]
fig, ax = plt.subplots()
NI, NJ = Y1.shape
Q = ax.quiver(Y1, Y2, u, v, color='r')
for i in range(NI):
for j in range(NJ):
x = Y1[i, j]
y = Y2[i, j]
yprime = model.dfun([x, y])
u[i,j] = yprime[0]
v[i,j] = yprime[1]
fig, ax = plt.subplots()
def plot_trajectory(x0, model):
tspan = np.linspace(0, 200, TRAJ_STEPS)
ys = odeint(model.dfun, x0, tspan)
ax.plot(ys[:,0], ys[:,1], 'b-') # path
ax.plot([ys[0,0]], [ys[0,1]], 'o') # start
ax.plot([ys[-1,0]], [ys[-1,1]], 's') # end
Q = ax.quiver(Y1, Y2, u, v, color='r')
def plot_trajectory(x0, model):
tspan = np.linspace(0, 200, TRAJ_STEPS)
ys = odeint(model.dfun, x0, tspan)
ax.plot(ys[:,0], ys[:,1], 'b-') # path
ax.plot([ys[0,0]], [ys[0,1]], 'o') # start
ax.plot([ys[-1,0]], [ys[-1,1]], 's') # end
plot_trajectory([2.,0.], model_ode)
def onclick(event):
plot_trajectory([event.xdata,event.ydata], model_ode)
plt.draw()
def onclick(event):
plot_trajectory([event.xdata,event.ydata], model_ode)
plt.draw()
cid = fig.canvas.mpl_connect('button_press_event', onclick)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.xlabel('$y_1$')
plt.ylabel('$y_2$')
plt.xlim([-2, 2])
plt.ylim([-2, 2])
plt.show()
plt.xlabel('$y_1$')
plt.ylabel('$y_2$')
plt.xlim([-2, 2])
plt.ylim([-2, 2])
plt.show()
class ${name}:
def __init__(self,\
%for c,val in const.items():
${c}=${val}${'' if loop.last else ', '}\
%endfor
):
self.limit = ${limit}
% for c in const.keys():
self.${c} = ${c}
% endfor
def dfun(self,state_variables):
<%
sv_csl = ", ".join(sv)
%>
${sv_csl} = state_variables
% for c in const.keys():
${c} = self.${c}
% endfor
% for cvar in input:
${cvar} = 0.0
% endfor
% for i, var in enumerate(sv):
d${var} = ${drift[i]}
% endfor
return [${sv_csl}]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment