/[ascend]/trunk/pygtk/interface/solverreporter.py
ViewVC logotype

Contents of /trunk/pygtk/interface/solverreporter.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 321 - (show annotations) (download) (as text)
Thu Feb 23 14:14:36 2006 UTC (14 years, 1 month ago) by johnpye
File MIME type: text/x-python
File size: 5791 byte(s)
Added 'close on non-converged' preference
Unified message output for popup/non-popup status reporting
1 import ascend
2 import time
3 import gtk
4 import gtk.glade
5
6 class PythonSolverReporter(ascend.SolverReporter):
7 def __init__(self,browser,interval):
8 self.browser=browser
9 self.updateinterval = interval
10 self.reporter = self.browser.reporter
11 if self.reporter==None:
12 raise RuntimeError("Can't find reporter")
13 self.starttime = time.clock()
14 self.statusbarcontext = self.browser.statusbar.get_context_id("pythonsolverreporter")
15 self.browser.statusbar.push(self.statusbarcontext,"Solving...")
16 ascend.SolverReporter.__init__(self)
17
18 def report_to_browser(self,status):
19 self.browser.statusbar.pop(self.statusbarcontext)
20
21 if status.isConverged():
22 self.reporter.reportSuccess("Converged")
23 return
24 elif status.hasExceededTimeLimit():
25 _msg = "Solver exceeded time limit"
26 elif status.hasExceededIterationLimit():
27 _msg = "Solver exceeded iteration limit"
28 elif status.isDiverged():
29 _msg = "Solver diverged"
30 else:
31 _msg = "Solve failed (unknown reason: check console)"
32
33 _msg = _msg + " while solving block %d/%d (%d vars in block)" % (status.getCurrentBlockNum(),
34 status.getNumBlocks(),status.getCurrentBlockSize() )
35 self.reporter.reportError(_msg)
36
37
38
39 class PopupSolverReporter(PythonSolverReporter):
40 def __init__(self,GLADE_FILE,browser,numvars):
41 _updateinterval=0.1
42 PythonSolverReporter.__init__(self,browser,_updateinterval)
43
44 _xml = gtk.glade.XML(GLADE_FILE,"solverstatusdialog")
45 _xml.signal_autoconnect(self)
46
47 self.window = _xml.get_widget("solverstatusdialog")
48
49 self.numvars = _xml.get_widget("numvarsentry")
50 self.numblocks = _xml.get_widget("numblocksentry")
51 self.elapsedtime = _xml.get_widget("elapsedtimeentry")
52 self.numiterations = _xml.get_widget("numiterationsentry")
53 self.blockvars = _xml.get_widget("blockvarsentry")
54 self.blockiterations = _xml.get_widget("blockiterationsentry")
55 self.blockresidual = _xml.get_widget("blockresidualentry")
56 self.blockelapsedtime = _xml.get_widget("blockelapsedtimeentry")
57
58 self.progressbar = _xml.get_widget("progressbar")
59 self.closebutton = _xml.get_widget("closebutton")
60 self.stopbutton = _xml.get_widget("stopbutton")
61
62 print "SOLVER REPORTER ---- PYTHON"
63
64 self.solvedvars = 0;
65
66 self.lasttime = 0;
67 self.blockstart = self.starttime;
68 self.blocktime = 0;
69 self.elapsed = 0;
70 self.blocknum = 0;
71 self.guiinterrupt = False;
72
73 self.nv = numvars
74 self.numvars.set_text(str(self.nv))
75
76 while gtk.events_pending():
77 gtk.main_iteration()
78
79 def run(self):
80 self.window.run()
81
82 def on_stopbutton_clicked(self,*args):
83 print "STOPPING..."
84 self.guiinterrupt = True;
85
86 def on_solverstatusdialog_close(self,*args):
87 self.window.response(gtk.RESPONSE_CLOSE)
88
89 def on_solverstatusdialog_response(self,response,*args):
90 self.window.hide()
91 del(self.window)
92
93 def fill_values(self,status):
94 print "FILLING VALUES..."
95 self.numblocks.set_text("%d of %d" % (status.getCurrentBlockNum(),status.getNumBlocks()))
96
97 self.elapsedtime.set_text("%0.1f s" % self.elapsed)
98 self.numiterations.set_text(str(status.getIterationNum()))
99 self.blockvars.set_text(str(status.getCurrentBlockSize()))
100 self.blockiterations.set_text(str(status.getCurrentBlockIteration()))
101 self.blockresidual.set_text("%8.5e" % status.getBlockResidualRMS())
102 self.blockelapsedtime.set_text("%0.1f s" % self.blocktime)
103
104 _frac = float(status.getNumConverged()) / self.nv
105 print "FRACTION = ",_frac
106 self.progressbar.set_text("%d vars converged..." % status.getNumConverged());
107 self.progressbar.set_fraction(_frac)
108
109 def report(self,status):
110 _time = time.clock();
111 _sincelast = _time - self.lasttime
112 if status.getCurrentBlockNum() > self.blocknum:
113 self.blocknum = status.getCurrentBlockNum()
114 self.blockstart = _time
115 if self.lasttime==0 or _sincelast > self.updateinterval or status.isConverged():
116 self.lasttime = _time;
117 self.elapsed = _time - self.starttime
118 print "UPDATING!"
119 self.fill_values(status)
120
121 while gtk.events_pending():
122 gtk.main_iteration()
123
124 if status.isConverged() or status.isDiverged() or status.isInterrupted():
125 return 1
126 if self.guiinterrupt:
127 return 2
128 return 0
129
130 def finalise(self,status):
131 _p = self.browser.prefs;
132 _close_on_converged = _p.getBoolPref("SolverReporter","close_on_converged",True);
133 _close_on_nonconverged = _p.getBoolPref("SolverReporter","close_on_nonconverged",False);
134
135 if status.isConverged() and _close_on_converged:
136 self.report_to_browser(status)
137 self.window.response(gtk.RESPONSE_CLOSE)
138 return
139
140 if not status.isConverged() and _close_on_nonconverged:
141 self.report_to_browser(status)
142 self.window.response(gtk.RESPONSE_CLOSE)
143 return
144
145 self.fill_values(status)
146
147 if status.isConverged():
148 self.progressbar.set_fraction(1.0)
149 self.progressbar.set_text("Converged")
150 elif status.hasExceededTimeLimit():
151 self.progressbar.set_text("Exceeded time limit")
152 elif status.hasExceededIterationLimit():
153 self.progressbar.set_text("Exceeded iteration limit")
154 elif status.isDiverged():
155 self.progressbar.set_text("Diverged")
156
157 self.closebutton.set_sensitive(True)
158 self.stopbutton.set_sensitive(False)
159
160 self.report_to_browser(status)
161
162
163 class SimpleSolverReporter(PythonSolverReporter):
164 def __init__(self,browser):
165 print "CREATING SIMPLESOLVERREPORTER..."
166 _update = 0.1
167 PythonSolverReporter.__init__(self,browser,_update)
168 self.lasttime = self.starttime
169
170 def report(self,status):
171 _time = time.clock()
172 if _time - self.lasttime > self.updateinterval:
173 self.lasttime = _time
174 _msg = "Solved %d vars in %d iterations" % (status.getNumConverged(),status.getIterationNum())
175 self.browser.statusbar.push(self.statusbarcontext, _msg )
176
177 while gtk.events_pending():
178 gtk.main_iteration()
179 return 0
180
181 def finalise(self,status):
182 self.report_to_browser(status)
183

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