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

Contents of /trunk/pygtk/integratorreporter.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1735 - (show annotations) (download) (as text)
Wed Feb 6 04:12:44 2008 UTC (11 years, 3 months ago) by jpye
File MIME type: text/x-python
File size: 7422 byte(s)
Fixed bug 359 (units is none)
Fixed bug 358 (progress indicator with DOPRI5)
Added SI prefixes to units-of-measure file (peta, yotta, etc).
Removed debug output from DOPRI5.
1 import ascpy
2 import time
3 import sys
4 import gtk
5 import gtk.glade
6 import time
7 from varentry import *
8 from preferences import *
9 from infodialog import *
10 from observer import *
11 import tempfile
12
13 import gobject
14 try:
15 import pylab
16 except:
17 pass
18
19 # When writing this class, we assume that the integrator class has already had
20 # its "analyse" method called, so we know all that stuff like the number of
21 # observed variables, what our time samples are, what the independent variable
22 # is, etc.
23
24 INTEGRATOR_NUM = 0
25
26 class IntegratorReporterPython(ascpy.IntegratorReporterCxx):
27 def __init__(self,browser,integrator):
28 self.browser=browser
29 ascpy.IntegratorReporterCxx.__init__(self,integrator)
30
31 # GUI elements
32 _xml = gtk.glade.XML(browser.glade_file,"integratorstatusdialog")
33 _xml.signal_autoconnect(self)
34 self.window=_xml.get_widget("integratorstatusdialog")
35 self.window.set_transient_for(self.browser.window)
36 self.label=_xml.get_widget("integratorlabel")
37 self.label.set_text("Solving with "+self.getIntegrator().getName())
38 self.progress=_xml.get_widget("integratorprogress")
39 self.data = None
40
41 self.cancelrequested=False
42
43 def run(self):
44 # run the dialog: start solution, monitor use events
45 try:
46 self.getIntegrator().solve()
47
48 except RuntimeError,e:
49 self.browser.reporter.reportError("Integrator failed: %s" % e)
50
51 if self.browser.prefs.getBoolPref("Integrator","writeendmatrix",True):
52 if platform.system()=="Windows":
53 _deffn = "\\TEMP\\ascintegratormatrix.mtx"
54 else:
55 _deffn = "/tmp/ascintegratormatrix.mtx"
56 _fn = self.browser.prefs.getStringPref("Integrator","matrixfilepath",_deffn)
57 self.browser.reporter.reportNote("Writing matrix to file '%s'" % _fn)
58 _fp = file(_fn,"w")
59 try:
60 try:
61 self.getIntegrator().writeMatrix(_fp)
62 except RuntimeError,e:
63 self.browser.reporter.reportError(str(e))
64 finally:
65 _fp.close()
66 self.window.destroy()
67
68 return
69
70 def on_cancelbutton_clicked(self,*args):
71 self.cancelrequested=True
72
73 def initOutput(self):
74 # empty out the data table
75 self.data=[]
76 self.nsteps = self.getIntegrator().getNumSteps()
77 self.progress.set_text("Starting...")
78 self.progress.set_fraction(0.0)
79 #update the GUI
80 while gtk.events_pending():
81 gtk.main_iteration()
82 return 1
83
84 def closeOutput(self):
85 global INTEGRATOR_NUM
86 integrator = self.getIntegrator()
87 # create an empty observer
88 try:
89 _xml = gtk.glade.XML(self.browser.glade_file,"observervbox")
90 _label = gtk.Label();
91 INTEGRATOR_NUM = INTEGRATOR_NUM + 1
92 _name = "Integrator %d" % INTEGRATOR_NUM
93 _tab = self.browser.maintabs.append_page(_xml.get_widget("observervbox"),_label)
94 _obs = ObserverTab(xml=_xml, name=_name, browser=self.browser, tab=_tab, alive=False)
95 _label.set_text(_obs.name)
96 self.browser.observers.append(_obs)
97 self.browser.tabs[_tab]=_obs
98
99 # add the columns
100 _obs.add_instance(integrator.getIndependentVariable().getInstance())
101 for _v in [integrator.getObservedVariable(_i) for _i in range(0,integrator.getNumObservedVars())]:
102 _obs.add_instance(_v.getInstance())
103
104 for _time,_vals in self.data:
105 _obs.do_add_row([_time]+[_v for _v in _vals])
106 except Exception,e:
107 sys.stderr.write("\n\n\nIntegratorReporter::closeOutput: error: %s\n\n\n" % str(e))
108 return 1
109 return 0
110
111 def closeOutput1(self):
112 # output the results (to the console, for now)
113 for _t,_vals in self.data:
114 print _t,_vals
115
116 self.progress.set_fraction(1.0)
117 self.progress.set_text("Finished.")
118 return 1
119
120 def updateStatus(self):
121 # outdate the GUI
122 try:
123 # TODO: change so it's not updating every step!
124 t = self.getIntegrator().getCurrentTime()
125 _frac = float(self.getIntegrator().getCurrentStep())/self.nsteps
126 self.progress.set_text("t = %f" % (self.getIntegrator().getCurrentTime()))
127 self.progress.set_fraction(_frac)
128 while gtk.events_pending():
129 gtk.main_iteration()
130 if self.cancelrequested:
131 return 0
132 return 1
133 except Exception,e:
134 print "\n\nERROR IN UPDATESTATUS!",str(e)
135 return 0
136
137 def recordObservedValues(self):
138 # just add to our in-memory data structure for now...
139 try:
140 i = self.getIntegrator()
141 print str(i.getCurrentObservations())
142 self.data.append((i.getCurrentTime(),i.getCurrentObservations()))
143 except Exception,e:
144 print "\n\nERROR IN RECORDOBSERVEDVALUES!",str(e)
145 return 0
146 return 1
147
148 class IntegratorReporterFile(ascpy.IntegratorReporterCxx):
149 def __init__(self,integrator,filep):
150 self.filep=filep
151 self.numsteps=0
152 self.indepname="t"
153 ascpy.IntegratorReporterCxx.__init__(self,integrator)
154
155
156 def run(self):
157 self.getIntegrator().solve()
158
159 def initOutput(self):
160 try:
161 sys.stderr.write("Integrating...\n")
162 I = self.getIntegrator()
163 self.numsteps=I.getNumSteps()
164 self.indepname = I.getIndependentVariable().getName()
165 names = [I.getObservedVariable(i).getName() for i \
166 in range(I.getNumObservedVars())
167 ]
168 self.filep.write("#%s\t" % self.indepname)
169 self.filep.write("\t".join(names)+"\n")
170 except Exception,e:
171 print "ERROR %s" % str(e)
172 return 0
173 return 1
174
175 def closeOutput(self):
176 sys.stderr.write(" "*20+chr(8)*20)
177 sys.stderr.write("Finished, %d samples recorded.\n" % self.numsteps)
178 self.filep.write("#end\n")
179 return 0
180
181 def updateStatus(self):
182 try:
183 I = self.getIntegrator()
184 t = I.getCurrentTime()
185 pct = 100.0 * I.getCurrentStep() / self.numsteps;
186 sys.stderr.write("%3.0f%% (%s = %6.3f) \r" % (pct,self.indepname,t))
187 except Exception,e:
188 print "ERROR %s" % str(e)
189 return 0
190 return 1
191
192 def recordObservedValues(self):
193 try:
194 I = self.getIntegrator()
195 obs = I.getCurrentObservations()
196 #print str(obs)
197 self.filep.write("%f\t" % I.getCurrentTime())
198 self.filep.write("\t".join([str(i) for i in obs])+"\n")
199 except Exception,e:
200 print "ERROR %s" % str(e)
201 return 0
202 return 1
203
204 class IntegratorReporterPlot(ascpy.IntegratorReporterCxx):
205 """Plotting integrator reporter"""
206 def __init__(self,integrator):
207 self.numsteps=0
208 self.indepname="t"
209 ascpy.IntegratorReporterCxx.__init__(self,integrator)
210
211 def run(self):
212 import loading
213 loading.load_matplotlib(throw=True)
214 self.getIntegrator().solve()
215
216 def initOutput(self):
217 try:
218 sys.stderr.write("Integrating...\n")
219 self.ax = pylab.subplot(111)
220 self.canvas = self.ax.figure.canvas
221 I = self.getIntegrator()
222 self.numsteps=I.getNumSteps()
223 self.indepname = I.getIndependentVariable().getName()
224 self.x = []
225 self.y = []
226 self.line, = pylab.plot(self.x,self.y,animated=True)
227 self.bg = self.canvas.copy_from_bbox(self.ax.bbox)
228 gobject.idle_add(self.plotupdate)
229 pylab.show()
230 except Exception,e:
231 print "ERROR %s" % str(e)
232 return 0
233 return 1
234
235 def closeOutput(self):
236 sys.stderr.write(" "*20+chr(8)*20)
237 sys.stderr.write("Finished, %d samples recorded.\n" % self.numsteps)
238 return 0
239
240 def updateStatus(self):
241 return 1
242
243 def plotupdate(self):
244 sys.stderr.write("%d...\r " % len(self.x))
245 try:
246 self.canvas.restore_region(self.bg)
247 self.line.set_data(self.x, self.y)
248 self.ax.draw_artist(self.line)
249 self.canvas.blit(self.ax.bbox)
250 except Exception,e:
251 print "ERROR %s" % str(e)
252
253 def recordObservedValues(self):
254 try:
255 I = self.getIntegrator()
256 obs = I.getCurrentObservations()
257 self.x.append(I.getCurrentTime())
258 self.y.append(obs[0])
259 except Exception,e:
260 print "ERROR %s" % str(e)
261 return 0
262 return 1
263
264

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