1
#-----------------------------------------------------------------------------
3
# Purpose: concentrate model related functions in a single module
5
# Author: Flavio Codeco Coelho
8
# RCS-ID: $Id: Model.py $
9
# Copyright: (c) 2004-2006
12
#-----------------------------------------------------------------------------
14
from scipy import integrate
19
def __init__(self,equations,pars,inits, trange,):
21
Equations: a function with the equations
22
inits: sequence of initial conditions
23
trange: time range for the simulation
28
self.Trange = arange(0,trange,0.1)
31
def compileParEqs(self):
33
compile equation and parameter expressions
36
eql = self.eqs.strip().split('\n')
37
pl = self.pars.strip().split('\n')
38
self.vnames = [v.split('=')[0] for v in eql if '=' in v]
39
self.pnames = [p.split('=')[0] for p in pl if '=' in p]
42
self.ceqs = [compile(i.split('=')[-1],'<equation>','eval') for i in eql]
44
dlg = wx.MessageDialog(self, 'There is a syntax error in the equation Box.\nPlease fix it and try again.',
45
'Syntax Error', wx.OK | wx.ICON_INFORMATION)
51
if self.pars.strip() =="":
55
#in this case returns the compete expression, including the '='
57
self.cpars = [compile(i,'<parameter>','exec') for i in pl]
59
dlg = wx.MessageDialog(self, 'There is a syntax error in the parameter Box.\nPlease fix it and try again.',
60
'Syntax Error', wx.OK | wx.ICON_INFORMATION)
67
self.cpars = [compile(i,'<parameter>','eval') for i in pl]
69
dlg = wx.MessageDialog(self, 'There is a syntax error in the parameter Box.\nPlease fix it and try again.',
70
'Syntax Error', wx.OK | wx.ICON_INFORMATION)
78
Do numeric integration
81
t_courseList.append(integrate.odeint(self.Equations,self.Inits,self.Trange,
82
full_output=0, printmessg=0))
83
return (t_courseList,self.Trange)
85
def Equations(self,y,t):
87
This function defines the system of differential equations, evaluating
88
each line of the equation text box as ydot[i]
94
#---Create Parameter Array----------------------------------------------------------------------------
95
pars = self.cpars#par.strip().split('\n')
98
exec('%s=%s'%(','.join(self.vnames),list(y)))
99
p = zeros((Npar),'d') #initialize p
100
if pars: #only if there is at least one parameter
101
for j in xrange(len(pars)):
105
p[j] = eval(pars[j]) #initialize parameter values
107
#---Create equation array----------------------------------------------------------------------------
108
eqs = self.ceqs#strip(self.eqs).split('\n')
110
ydot = zeros((Neq),'d') #initialize ydot
111
for k in xrange(Neq):
112
ydot[k] = eval(eqs[k]) #dy(k)/dt
b'\\ No newline at end of file'