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

Contents of /trunk/pygtk/solverreporter.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1109 - (show annotations) (download) (as text)
Thu Jan 11 04:07:02 2007 UTC (15 years, 5 months ago) by johnpye
File MIME type: text/x-python
File size: 6424 byte(s)
Added some more user error messages from the solver.
Added detection for the MFGRAPH library, which will be used to output GraphViz files based on the incidence matrix.
1 import ascpy
2 import time
3 import gtk
4 import gtk.glade
5
6 class PythonSolverReporter(ascpy.SolverReporter):
7 def __init__(self,browser):
8 self.browser=browser
9 self.updateinterval = self.browser.prefs.getBoolPref("SolverReporter","update_interval", 0.5)
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 ascpy.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 elif status.isInterrupted():
31 _msg = "Solver interrupted"
32 elif status.hasResidualCalculationErrors():
33 _msg = "Solve had residual calculation errors"
34 else:
35 _msg = "Solve failed"
36
37 _msg = _msg + " while solving block %d/%d (%d vars in block)" % (status.getCurrentBlockNum(),
38 status.getNumBlocks(),status.getCurrentBlockSize() )
39 self.reporter.reportError(_msg)
40
41
42
43 class PopupSolverReporter(PythonSolverReporter):
44 def __init__(self,browser,numvars):
45 PythonSolverReporter.__init__(self,browser)
46
47 _xml = gtk.glade.XML(browser.glade_file,"solverstatusdialog")
48 _xml.signal_autoconnect(self)
49
50 self.window = _xml.get_widget("solverstatusdialog")
51 if self.browser.icon:
52 self.window.set_icon(self.browser.icon)
53 self.window.set_transient_for(self.browser.window)
54
55 self.numvars = _xml.get_widget("numvarsentry")
56 self.numblocks = _xml.get_widget("numblocksentry")
57 self.elapsedtime = _xml.get_widget("elapsedtimeentry")
58 self.numiterations = _xml.get_widget("numiterationsentry")
59 self.blockvars = _xml.get_widget("blockvarsentry")
60 self.blockiterations = _xml.get_widget("blockiterationsentry")
61 self.blockresidual = _xml.get_widget("blockresidualentry")
62 self.blockelapsedtime = _xml.get_widget("blockelapsedtimeentry")
63
64 self.progressbar = _xml.get_widget("progressbar")
65 self.closebutton = _xml.get_widget("closebutton")
66 self.stopbutton = _xml.get_widget("stopbutton")
67
68 #print "SOLVER REPORTER ---- PYTHON"
69
70 self.solvedvars = 0;
71
72 self.lasttime = 0;
73 self.blockstart = self.starttime;
74 self.blocktime = 0;
75 self.elapsed = 0;
76 self.blocknum = 0;
77 self.guiinterrupt = False;
78 self.guitime = 0;
79
80 self.nv = numvars
81
82 while gtk.events_pending():
83 gtk.main_iteration()
84
85 def run(self):
86 self.window.run()
87
88 def on_stopbutton_clicked(self,*args):
89 print "STOPPING..."
90 self.guiinterrupt = True;
91
92 def on_solverstatusdialog_close(self,*args):
93 self.window.response(gtk.RESPONSE_CLOSE)
94
95 def on_solverstatusdialog_response(self,response,*args):
96 self.window.hide()
97 del(self.window)
98
99 def fill_values(self,status):
100 self.numblocks.set_text("%d of %d" % (status.getCurrentBlockNum(),status.getNumBlocks()))
101 self.numvars.set_text("%d of %d" % (status.getNumConverged(), self.nv))
102 self.elapsedtime.set_text("%0.1f s" % self.elapsed)
103 self.numiterations.set_text(str(status.getIterationNum()))
104 self.blockvars.set_text(str(status.getCurrentBlockSize()))
105 self.blockiterations.set_text(str(status.getCurrentBlockIteration()))
106 self.blockresidual.set_text("%8.5e" % status.getBlockResidualRMS())
107 self.blockelapsedtime.set_text("%0.1f s" % self.blocktime)
108
109 _frac = float(status.getNumConverged()) / self.nv
110 self.progressbar.set_text("%d vars converged..." % status.getNumConverged());
111 self.progressbar.set_fraction(_frac)
112
113 def report(self,status):
114 _time = time.clock();
115 _sincelast = _time - self.lasttime
116 if status.getCurrentBlockNum() > self.blocknum:
117 self.blocknum = status.getCurrentBlockNum()
118 self.blockstart = _time
119
120 if self.lasttime==0 or _sincelast > self.updateinterval or status.isConverged():
121 self.lasttime = _time;
122 self.elapsed = _time - self.starttime
123 self.blocktime = _time - self.blockstart
124 #print "UPDATING!"
125 self.fill_values(status)
126
127 while gtk.events_pending():
128 gtk.main_iteration()
129
130 self.guitime = self.guitime + (time.clock() - _time)
131
132 if status.isConverged() or status.isDiverged() or status.isInterrupted():
133 return 1
134 if self.guiinterrupt:
135 return 2
136 return 0
137
138 def finalise(self,status):
139 _time = time.clock()
140
141 _p = self.browser.prefs;
142 _close_on_converged = _p.getBoolPref("SolverReporter","close_on_converged",True);
143 _close_on_nonconverged = _p.getBoolPref("SolverReporter","close_on_nonconverged",False);
144
145
146 if status.isConverged() and _close_on_converged:
147 self.report_to_browser(status)
148 self.window.response(gtk.RESPONSE_CLOSE)
149 return
150
151 if not status.isConverged() and _close_on_nonconverged:
152 self.report_to_browser(status)
153 self.window.response(gtk.RESPONSE_CLOSE)
154 return
155
156 self.fill_values(status)
157
158 if status.isConverged():
159 self.progressbar.set_fraction(1.0)
160 self.progressbar.set_text("Converged")
161 elif status.hasExceededTimeLimit():
162 self.progressbar.set_text("Exceeded time limit")
163 elif status.hasExceededIterationLimit():
164 self.progressbar.set_text("Exceeded iteration limit")
165 elif status.isDiverged():
166 self.progressbar.set_text("Diverged")
167 elif status.isOverDefined():
168 self.progressbar.set_text("Over-defined")
169 elif status.isUnderDefined():
170 self.progressbar.set_text("Under-defined")
171
172 self.closebutton.set_sensitive(True)
173 self.stopbutton.set_sensitive(False)
174
175 self.report_to_browser(status)
176
177 self.guitime = self.guitime + (time.clock() - _time)
178 print "TIME SPENT UPDATING SOLVER: %0.2f s" % self.guitime
179
180
181 class SimpleSolverReporter(PythonSolverReporter):
182 def __init__(self,browser):
183 #print "CREATING SIMPLESOLVERREPORTER..."
184 PythonSolverReporter.__init__(self,browser)
185 self.lasttime = self.starttime
186
187 def report(self,status):
188 _time = time.clock()
189 if _time - self.lasttime > self.updateinterval:
190 self.lasttime = _time
191 _msg = "Solved %d vars in %d iterations" % (status.getNumConverged(),status.getIterationNum())
192 self.browser.statusbar.push(self.statusbarcontext, _msg )
193
194 while gtk.events_pending():
195 gtk.main_iteration()
196 return 0
197
198 def finalise(self,status):
199 self.report_to_browser(status)
200

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