/[ascend]/trunk/pygtk/integrator.py
ViewVC logotype

Contents of /trunk/pygtk/integrator.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 961 - (show annotations) (download) (as text)
Mon Dec 11 14:04:13 2006 UTC (13 years, 7 months ago) by johnpye
File MIME type: text/x-python
File size: 6692 byte(s)
Fixed a silly bug with Integrator::setEngine.
Added test case for lotka.a4c to test.py
1 import ascpy
2 import time
3 import gtk
4 import gtk.glade
5 import time
6 from varentry import *
7 from preferences import *
8 from integratorreporter import *
9
10 class IntegratorError(RuntimeError):
11 def __init__(self,msg):
12 self.msg = msg
13 def __str__(self):
14 return "Input Error: %s" % self.msg;
15
16 class IntegratorWindow:
17 def __init__(self,browser,sim):
18 # create a new integrator straight away
19 self.integrator = ascpy.Integrator(sim)
20 self.engines = self.integrator.getEngines()
21
22 self.browser=browser
23 self.prefs = Preferences()
24
25 # locate all the widgets
26 _xml = gtk.glade.XML(browser.glade_file,"integratorwin")
27 _xml.signal_autoconnect(self)
28
29 self.window = _xml.get_widget("integratorwin")
30 self.window.set_transient_for(self.browser.window)
31
32 self.engineselect = _xml.get_widget("engineselect")
33 self.beginentry = _xml.get_widget("beginentry")
34 self.durationentry = _xml.get_widget("durationentry")
35 self.nstepsentry = _xml.get_widget("nstepsentry")
36 self.timedistributionselect = _xml.get_widget("timedistributionselect")
37
38 self.settings = {
39 # input field: [pref name, default value, export-to-integrator function]
40 "initialstep": [1,lambda x:self.integrator.setInitialSubStep(float(x))]
41 ,"minstep":[0.001,lambda x:self.integrator.setMinSubStep(float(x))]
42 ,"maxstep":[1000,lambda x:self.integrator.setMaxSubStep(float(x))]
43 ,"maxsteps":[100,lambda x:self.integrator.setMaxSubSteps(int(x))]
44 }
45
46 self.integratorentries={}
47 for _k in self.settings.keys():
48 _w = _xml.get_widget(_k+"entry")
49 if not _w:
50 raise RuntimeError("Couldn't find entry for"+_k)
51 self.integratorentries[_k]=_w
52
53 # fill values from user preferences, system values, etc
54 self.fill_values()
55
56 def fill_values(self):
57 _enginestore = gtk.ListStore(str)
58 self.engineselect.set_model(_enginestore)
59 _cell = gtk.CellRendererText()
60 self.engineselect.pack_start(_cell, True)
61 self.engineselect.add_attribute(_cell, 'text', 1)
62
63 _engpref = self.prefs.getStringPref("Integrator","engine","LSODE")
64 _engindex = 0
65 _i = 0
66 if len(self.engines):
67 for k in self.engines:
68 _row = _enginestore.append()
69 _enginestore.set(_row,0,self.engines[k])
70 if self.engines[k]==_engpref:
71 _engindex=_i
72 _i += 1
73
74 # set preferred integrator
75 self.engineselect.set_active(_engindex)
76 self.engineselect.set_sensitive(True)
77 else:
78 _row = _enginestore.append()
79 _enginestore.set(_row,0,"No integrators available")
80 self.engineselect.set_active(0)
81 self.engineselect.set_sensitive(False)
82
83
84 # set preferred timesteps etc
85 for _k,_v in self.settings.iteritems():
86 self.integratorentries[_k].set_text(self.prefs.getStringPref("Integrator",_k,str(_v[0])))
87
88 # get the current time value as the beginentry...
89 print "SEARCHING FOR TIME VAR..."
90 if self.integrator.findIndependentVar():
91 print "FOUND time var..."
92 _t = self.integrator.getCurrentTime();
93 print "Found time = %f" % _t
94 self.beginentry.set_text(str(_t))
95 else:
96 self.browser.reporter.reportNote("No indep var found");
97 self.beginentry.set_text("0")
98
99 _dur = self.prefs.getStringPref("Integrator","duration","100")
100 self.durationentry.set_text(_dur)
101 self.nstepsentry.set_text("100")
102 self.timedistributionselect.set_active(0)
103
104 def on_integratorcancel_clicked(self,*args):
105 self.browser.reporter.reportNote("CANCELLING");
106 self.window.response(gtk.RESPONSE_CANCEL)
107
108 def on_entry_key_press_event(self,widget,event):
109 keyname = gtk.gdk.keyval_name(event.keyval)
110 if keyname=="Return":
111 self.window.response(gtk.RESPONSE_OK)
112 return True
113 elif keyname=="Escape":
114 self.window.response(gtk.RESPONSE_CANCEL)
115 return True;
116 return False;
117
118 def run(self):
119 # focus the engine select box when we start...
120 self.engineselect.grab_focus()
121
122 # loop around unitil either cancelled or valid entries are input
123 _ok = False
124 while True:
125 try:
126 _res = self.window.run()
127 if _res == gtk.RESPONSE_OK:
128 self.check_inputs()
129 _ok=True
130 break
131 elif _res == gtk.RESPONSE_CANCEL or _res == gtk.RESPONSE_NONE:
132 self.browser.reporter.reportNote("CANCEL event received");
133 break
134 except IntegratorError,e:
135 self.browser.reporter.reportError(str(e))
136 # continue
137
138 if _ok:
139 _res = self.integrator.analyse()
140 if _res:
141 # if we're all ok, create the reporter window and close this one
142 _integratorreporter = IntegratorReporterPython(self.browser,self.integrator)
143 self.integrator.setReporter(_integratorreporter)
144 self.window.destroy()
145 return _integratorreporter # means proceed to solve
146
147 self.window.destroy()
148 return None # can't solve
149
150 def color_entry(self,entry,color):
151 # colour an input box if it doesn't have acceptable contents
152 # error messages would be reported by the 'errors panel' in the main win
153 entry.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
154 entry.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
155 entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
156 entry.modify_base(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
157
158 def check_inputs(self):
159 # set the timesteps (samples)
160 _val = {}
161 for _k,_v in {
162 self.beginentry:[lambda x:float(x),"begin"]
163 , self.durationentry:[lambda x:float(x),"duration"]
164 , self.nstepsentry:[lambda x:int(x),"num"]
165 }.iteritems():
166 _val[_v[1]]=_v[0](_k.get_text())
167
168 self.integrator.setLinearTimesteps(ascpy.Units("s"), _val["begin"], (_val["begin"]+_val["duration"]), _val["num"]);
169 self.begin=_val["begin"]
170 self.duration=_val["duration"]
171
172 self.prefs.setStringPref("Integrator","duration",str(self.duration))
173
174 # set substep parameters (ie settings common to any integrator engine)
175 _failed=False
176 for _k,_v in self.settings.iteritems():
177 try:
178 _f = self.integratorentries[_k];
179 # pass the substep setting to the integrator
180 _v[1](_f.get_text())
181 self.color_entry(_f,"white")
182 except ValueError,e:
183 self.color_entry(_f,"#FFBBBB")
184 _failed=True
185
186 if _failed:
187 raise IntegratorError("Invalid step parameter(s)");
188
189 for _k,_v in self.settings.iteritems():
190 # store those inputs for next time
191 _f = self.integratorentries[_k];
192 self.prefs.setStringPref("Integrator",_k,str(_f.get_text()))
193
194 # set engine (and check that it's OK with this system)
195 engine=self.engineselect.get_active()
196 if engine==-1:
197 self.color_entry(self.engineselect,"#FFBBBB")
198 raise IntegratorError("No engine selected")
199
200 self.color_entry(self.engineselect,"white")
201
202 self.prefs.setStringPref("Integrator","engine",self.engines.values()[engine])
203
204 try:
205 _res = self.integrator.setEngine(self.engines.keys()[engine])
206 except IndexError,e:
207 raise IntegratorERror("Unable to set engine: %s",e)

Properties

Name Value
svn:executable *

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22