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

Contents of /trunk/pygtk/integrator.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1600 - (show annotations) (download) (as text)
Sun Aug 19 06:32:59 2007 UTC (12 years, 5 months ago) by jpye
File MIME type: text/x-python
File size: 8133 byte(s)
Removed some debug output.
Added message for case where no solvers have been loaded.

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 from solverparameters import *
10 from infodialog import *
11 import tempfile
12
13 class IntegratorError(RuntimeError):
14 def __init__(self,msg):
15 self.msg = msg
16 def __str__(self):
17 return "Input Error: %s" % self.msg;
18
19 class IntegratorWindow:
20 def __init__(self,browser,sim):
21 # create a new integrator straight away
22 self.integrator = ascpy.Integrator(sim)
23 self.engines = self.integrator.getEngines()
24
25 self.browser=browser
26 self.prefs = Preferences()
27
28 # locate all the widgets
29 _xml = gtk.glade.XML(browser.glade_file,"integratorwin")
30 _xml.signal_autoconnect(self)
31
32 self.window = _xml.get_widget("integratorwin")
33 self.window.set_transient_for(self.browser.window)
34
35 self.engineselect = _xml.get_widget("engineselect")
36 self.beginentry = _xml.get_widget("beginentry")
37 self.durationentry = _xml.get_widget("durationentry")
38 self.nstepsentry = _xml.get_widget("nstepsentry")
39 self.timedistributionselect = _xml.get_widget("timedistributionselect")
40
41 self.settings = {
42 # input field: [pref name, default value, export-to-integrator function]
43 "initialstep": [1,lambda x:self.integrator.setInitialSubStep(float(x))]
44 ,"minstep":[0.001,lambda x:self.integrator.setMinSubStep(float(x))]
45 ,"maxstep":[1000,lambda x:self.integrator.setMaxSubStep(float(x))]
46 ,"maxsteps":[100,lambda x:self.integrator.setMaxSubSteps(int(x))]
47 }
48
49 self.integratorentries={}
50 for _k in self.settings.keys():
51 _w = _xml.get_widget(_k+"entry")
52 if not _w:
53 raise RuntimeError("Couldn't find entry for"+_k)
54 self.integratorentries[_k]=_w
55
56 # fill values from user preferences, system values, etc
57 self.fill_values()
58
59 # set the engine initially
60 try:
61 self.integrator.setEngine(self.engines[self.engineselect.get_active()])
62 except:
63 # perhaps no integrators are available
64 pass
65
66 def fill_values(self):
67 _enginestore = gtk.ListStore(str)
68 self.engineselect.set_model(_enginestore)
69 _cell = gtk.CellRendererText()
70 self.engineselect.pack_start(_cell, True)
71 self.engineselect.add_attribute(_cell, 'text', 1)
72
73 _engpref = self.prefs.getStringPref("Integrator","engine","LSODE")
74 _engindex = 0
75 _i = 0
76 if len(self.engines):
77 for k in self.engines:
78 _row = _enginestore.append()
79 _enginestore.set(_row,0,k)
80 if k==_engpref:
81 _engindex=_i
82 _i += 1
83
84 # set preferred integrator
85 self.engineselect.set_active(_engindex)
86 self.engineselect.set_sensitive(True)
87 else:
88 _row = _enginestore.append()
89 _enginestore.set(_row,0,"No integrators available")
90 self.engineselect.set_active(0)
91 self.engineselect.set_sensitive(False)
92
93
94 # set preferred timesteps etc
95 for _k,_v in self.settings.iteritems():
96 self.integratorentries[_k].set_text(self.prefs.getStringPref("Integrator",_k,str(_v[0])))
97
98 # get the current time value as the beginentry...
99 print "SEARCHING FOR TIME VAR..."
100 try:
101 self.integrator.findIndependentVar()
102 print "FOUND time var..."
103 _t = self.integrator.getCurrentTime();
104 print "Found time = %f" % _t
105 self.beginentry.set_text(str(_t))
106 except RuntimeError,e:
107 self.browser.reporter.reportNote(str(e));
108 self.beginentry.set_text("0")
109
110 _dur = self.prefs.getStringPref("Integrator","duration","100")
111 self.durationentry.set_text(_dur)
112 self.nstepsentry.set_text("100")
113 self.timedistributionselect.set_active(0)
114
115 def on_integratorcancel_clicked(self,*args):
116 self.browser.reporter.reportNote("CANCELLING");
117 self.window.response(gtk.RESPONSE_CANCEL)
118
119 def on_entry_key_press_event(self,widget,event):
120 keyname = gtk.gdk.keyval_name(event.keyval)
121 if keyname=="Return":
122 self.window.response(gtk.RESPONSE_OK)
123 return True
124 elif keyname=="Escape":
125 self.window.response(gtk.RESPONSE_CANCEL)
126 return True;
127 return False;
128
129 def on_engineselect_changed(self,widget,*args):
130 index = widget.get_active()
131 print "Setting engine to %d" % index
132 print "Engines are",self.engines
133 print "Selection is %s" % self.engines[index]
134 self.integrator.setEngine(self.engines[index])
135
136 def on_moreparametersbutton_clicked(self,*args):
137 print "ZO YOU WANT MORE PAHAMETERS EH!"
138 try:
139 _name = self.integrator.getName()
140 print "NAME = %s" % _name
141 _params = self.integrator.getParameters()
142 except RuntimeError,e:
143 self.browser.reporter.reportError(str(e))
144 return
145 print "CREATING SOLVERPARAMETERSWINDOW"
146 _paramswin = SolverParametersWindow(self.browser,_params,_name)
147 print "RUNNING SOLVERPARAMETERSWINDOW"
148 if _paramswin.run() == gtk.RESPONSE_OK:
149 print "GOT OK RESPONSE"
150 self.integrator.setParameters(_params)
151 print "PARAMETERS UPDATED"
152
153 def run(self):
154 # focus the engine select box when we start...
155 self.engineselect.grab_focus()
156
157 # loop around unitil either cancelled or valid entries are input
158 _ok = False
159 while True:
160 try:
161 _res = self.window.run()
162 if _res == gtk.RESPONSE_OK:
163 self.check_inputs()
164 _ok=True
165 break
166 else:
167 #self.browser.reporter.reportNote("CANCEL event received");
168 break
169 except IntegratorError,e:
170 self.browser.reporter.reportError(str(e))
171 # continue
172
173 if _ok:
174 try:
175 self.integrator.analyse()
176 except RuntimeError,e:
177 self.browser.reporter.reportError(str(e))
178 self.window.destroy()
179 if self.prefs.getBoolPref("Integrator","debuganalyse",True):
180 fp = tempfile.TemporaryFile()
181 self.integrator.writeDebug(fp)
182 fp.seek(0)
183 text = fp.read()
184 fp.close()
185 title = "Integrator Analysis Failed"
186 _dialog = InfoDialog(self.browser,self.browser.window,text,title,tabs=(70,200,300,400,500))
187 _dialog.run()
188
189 return None
190 # if we're all ok, create the reporter window and close this one
191 _integratorreporter = IntegratorReporterPython(self.browser,self.integrator)
192 self.integrator.setReporter(_integratorreporter)
193 self.window.destroy()
194 return _integratorreporter # means proceed to solve
195
196 self.window.destroy()
197 return None # can't solve
198
199 def color_entry(self,entry,color):
200 # colour an input box if it doesn't have acceptable contents
201 # error messages would be reported by the 'errors panel' in the main win
202 entry.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
203 entry.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
204 entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
205 entry.modify_base(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
206
207 def check_inputs(self):
208 # set the timesteps (samples)
209 _val = {}
210 for _k,_v in {
211 self.beginentry:[lambda x:float(x),"begin"]
212 , self.durationentry:[lambda x:float(x),"duration"]
213 , self.nstepsentry:[lambda x:int(x),"num"]
214 }.iteritems():
215 _val[_v[1]]=_v[0](_k.get_text())
216
217 self.integrator.setLinearTimesteps(ascpy.Units("s"), _val["begin"], (_val["begin"]+_val["duration"]), _val["num"]);
218 self.begin=_val["begin"]
219 self.duration=_val["duration"]
220
221 self.prefs.setStringPref("Integrator","duration",str(self.duration))
222
223 # set substep parameters (ie settings common to any integrator engine)
224 _failed=False
225 for _k,_v in self.settings.iteritems():
226 try:
227 _f = self.integratorentries[_k];
228 # pass the substep setting to the integrator
229 _v[1](_f.get_text())
230 self.color_entry(_f,"white")
231 except ValueError,e:
232 self.color_entry(_f,"#FFBBBB")
233 _failed=True
234
235 if _failed:
236 raise IntegratorError("Invalid step parameter(s)");
237
238 for _k,_v in self.settings.iteritems():
239 # store those inputs for next time
240 _f = self.integratorentries[_k];
241 self.prefs.setStringPref("Integrator",_k,str(_f.get_text()))
242
243 # set engine (and check that it's OK with this system)
244 engine=self.engineselect.get_active()
245 if engine==-1:
246 self.color_entry(self.engineselect,"#FFBBBB")
247 raise IntegratorError("No engine selected")
248
249 self.color_entry(self.engineselect,"white")
250
251 try:
252 self.prefs.setStringPref("Integrator","engine",self.engines[engine])
253 _res = self.integrator.setEngine(self.engines[engine])
254 except IndexError,e:
255 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