~ubuntu-branches/ubuntu/karmic/model-builder/karmic

« back to all changes in this revision

Viewing changes to model_builder/Model.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-04-10 17:05:04 UTC
  • Revision ID: james.westby@ubuntu.com-20070410170504-y884ntvt656218me
Tags: upstream-0.4.0
ImportĀ upstreamĀ versionĀ 0.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-----------------------------------------------------------------------------
 
2
# Name:        Model.py
 
3
# Purpose:     concentrate model related functions in a single module
 
4
#
 
5
# Author:      Flavio Codeco Coelho
 
6
#
 
7
# Created:     2006/08/20
 
8
# RCS-ID:      $Id: Model.py $
 
9
# Copyright:   (c) 2004-2006 
 
10
# Licence:     GPL
 
11
# New field:   Whatever
 
12
#-----------------------------------------------------------------------------
 
13
 
 
14
from scipy import integrate
 
15
from numpy import *
 
16
from string import *
 
17
 
 
18
class Model:
 
19
    def __init__(self,equations,pars,inits, trange,):
 
20
        """
 
21
        Equations: a function with the equations
 
22
        inits: sequence of initial conditions
 
23
        trange: time range for the simulation
 
24
        """
 
25
        self.eqs = equations
 
26
        self.pars = pars
 
27
        self.Inits = inits
 
28
        self.Trange = arange(0,trange,0.1)
 
29
        self.compileParEqs()
 
30
        
 
31
    def compileParEqs(self):
 
32
        """
 
33
        compile equation and parameter expressions
 
34
        """
 
35
        #Equations
 
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]
 
40
        
 
41
        try:
 
42
            self.ceqs = [compile(i.split('=')[-1],'<equation>','eval') for i in eql]
 
43
        except SyntaxError:
 
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)
 
46
            try:
 
47
                dlg.ShowModal()
 
48
            finally:
 
49
                dlg.Destroy()
 
50
        #Parameters
 
51
        if self.pars.strip() =="":
 
52
            self.cpars=[]
 
53
            return  
 
54
        if self.pnames:
 
55
            #in this case returns the compete expression, including the '='
 
56
            try:     
 
57
                self.cpars = [compile(i,'<parameter>','exec') for i in pl]
 
58
            except SyntaxError:
 
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)
 
61
                try:
 
62
                    dlg.ShowModal()
 
63
                finally:
 
64
                    dlg.Destroy()
 
65
        else:      
 
66
            try:     
 
67
                self.cpars = [compile(i,'<parameter>','eval') for i in pl]
 
68
            except SyntaxError:
 
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)
 
71
                try:
 
72
                    dlg.ShowModal()
 
73
                finally:
 
74
                    dlg.Destroy()
 
75
    
 
76
    def Run(self):
 
77
        """
 
78
        Do numeric integration
 
79
        """
 
80
        t_courseList = []
 
81
        t_courseList.append(integrate.odeint(self.Equations,self.Inits,self.Trange, 
 
82
        full_output=0, printmessg=0))
 
83
        return (t_courseList,self.Trange)
 
84
    
 
85
    def Equations(self,y,t):
 
86
        """
 
87
        This function defines the system of differential equations, evaluating
 
88
        each line of the equation text box as ydot[i]
 
89
 
 
90
        returns ydot
 
91
        """
 
92
        par = self.pars
 
93
 
 
94
    #---Create Parameter Array----------------------------------------------------------------------------
 
95
        pars = self.cpars#par.strip().split('\n')
 
96
        Npar = len(pars)
 
97
        if self.vnames:
 
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)):
 
102
                if self.pnames:
 
103
                    exec(pars[j])
 
104
                else:
 
105
                    p[j] = eval(pars[j]) #initialize parameter values
 
106
                        
 
107
    #---Create equation array----------------------------------------------------------------------------
 
108
        eqs = self.ceqs#strip(self.eqs).split('\n')
 
109
        Neq=len(eqs)
 
110
        ydot = zeros((Neq),'d') #initialize ydot
 
111
        for k in xrange(Neq):
 
112
            ydot[k] = eval(eqs[k]) #dy(k)/dt
 
113
 
 
114
        return ydot
 
 
b'\\ No newline at end of file'