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

Contents of /trunk/pygtk/solverreporter.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2239 - (show annotations) (download) (as text)
Sat Jul 31 03:24:35 2010 UTC (13 years, 10 months ago) by jpye
File MIME type: text/x-python
File size: 6615 byte(s)
fixes bug 475.
1 import ascpy
2 import time
3 import gtk
4 import gtk.glade
5
6 class PythonSolverReporter(ascpy.SolverReporter):
7 def __init__(self,browser,message=None):
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 if message:
16 self.browser.statusbar.push(self.statusbarcontext,"Solving (%s)..." % message)
17 else:
18 self.browser.statusbar.push(self.statusbarcontext,"Solving..." )
19 ascpy.SolverReporter.__init__(self)
20
21 def report_to_browser(self,status):
22 self.browser.statusbar.pop(self.statusbarcontext)
23
24 if status.isConverged():
25 self.reporter.reportSuccess("Converged")
26 return
27 elif status.hasExceededTimeLimit():
28 _msg = "Solver exceeded time limit"
29 elif status.hasExceededIterationLimit():
30 _msg = "Solver exceeded iteration limit"
31 elif status.isDiverged():
32 _msg = "Solver diverged"
33 elif status.isInterrupted():
34 _msg = "Solver interrupted"
35 elif status.hasResidualCalculationErrors():
36 _msg = "Solve had residual calculation errors"
37 else:
38 _msg = "Solve failed"
39
40 _msg = _msg + " while solving block %d/%d (%d vars in block)" % (status.getCurrentBlockNum(),
41 status.getNumBlocks(),status.getCurrentBlockSize() )
42 self.reporter.reportError(_msg)
43
44
45
46 class PopupSolverReporter(PythonSolverReporter):
47 def __init__(self,browser,numvars):
48 PythonSolverReporter.__init__(self,browser)
49
50 _xml = gtk.glade.XML(browser.glade_file,"solverstatusdialog")
51 _xml.signal_autoconnect(self)
52
53 self.window = _xml.get_widget("solverstatusdialog")
54 if self.browser.icon:
55 self.window.set_icon(self.browser.icon)
56 self.window.set_transient_for(self.browser.window)
57
58 self.numvars = _xml.get_widget("numvarsentry")
59 self.numblocks = _xml.get_widget("numblocksentry")
60 self.elapsedtime = _xml.get_widget("elapsedtimeentry")
61 self.numiterations = _xml.get_widget("numiterationsentry")
62 self.blockvars = _xml.get_widget("blockvarsentry")
63 self.blockiterations = _xml.get_widget("blockiterationsentry")
64 self.blockresidual = _xml.get_widget("blockresidualentry")
65 self.blockelapsedtime = _xml.get_widget("blockelapsedtimeentry")
66
67 self.progressbar = _xml.get_widget("progressbar")
68 self.closebutton = _xml.get_widget("closebutton1")
69 self.stopbutton = _xml.get_widget("stopbutton")
70
71 #print "SOLVER REPORTER ---- PYTHON"
72
73 self.solvedvars = 0;
74
75 self.lasttime = 0;
76 self.blockstart = self.starttime;
77 self.blocktime = 0;
78 self.elapsed = 0;
79 self.blocknum = 0;
80 self.guiinterrupt = False;
81 self.guitime = 0;
82
83 self.nv = numvars
84
85 while gtk.events_pending():
86 gtk.main_iteration()
87
88 def on_stopbutton_activate(self,*args):
89 self.guiinterrupt = True
90
91 def on_solverstatusdialog_response(self,widget,response):
92 self.guiinterrupt = True
93 self.window.destroy()
94
95 def fill_values(self,status):
96 self.numblocks.set_text("%d of %d" % (status.getCurrentBlockNum(),status.getNumBlocks()))
97 self.numvars.set_text("%d of %d" % (status.getNumConverged(), self.nv))
98 self.elapsedtime.set_text("%0.1f s" % self.elapsed)
99 self.numiterations.set_text(str(status.getIterationNum()))
100 self.blockvars.set_text(str(status.getCurrentBlockSize()))
101 self.blockiterations.set_text(str(status.getCurrentBlockIteration()))
102 self.blockresidual.set_text("%8.5e" % status.getBlockResidualRMS())
103 self.blockelapsedtime.set_text("%0.1f s" % self.blocktime)
104
105 _frac = float(status.getNumConverged()) / self.nv
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
116 if self.lasttime==0 or _sincelast > self.updateinterval or status.isConverged():
117 self.lasttime = _time;
118 self.elapsed = _time - self.starttime
119 self.blocktime = _time - self.blockstart
120 #print "UPDATING!"
121 self.fill_values(status)
122
123 while gtk.events_pending():
124 gtk.main_iteration()
125
126 self.guitime = self.guitime + (time.clock() - _time)
127
128 if status.isConverged() or status.isDiverged() or status.isInterrupted():
129 return 1
130 if self.guiinterrupt:
131 return 2
132 return 0
133
134 def finalise(self,status):
135 try:
136 _time = time.clock()
137
138 _p = self.browser.prefs;
139 _close_on_converged = _p.getBoolPref("SolverReporter","close_on_converged",True);
140 _close_on_nonconverged = _p.getBoolPref("SolverReporter","close_on_nonconverged",False);
141
142
143 if status.isConverged() and _close_on_converged:
144 self.report_to_browser(status)
145 print "CLOSING ON CONVERGED"
146 self.window.response(gtk.RESPONSE_CLOSE)
147 return
148
149 if not status.isConverged() and _close_on_nonconverged:
150 print "CLOSING, NOT CONVERGED"
151 self.report_to_browser(status)
152 if self.window:
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 except Exception,e:
180 print "SOME PROBLEM: %s" % str(e)
181
182 class SimpleSolverReporter(PythonSolverReporter):
183 def __init__(self,browser,message=None):
184 #print "CREATING SIMPLESOLVERREPORTER..."
185 PythonSolverReporter.__init__(self,browser,message)
186 self.lasttime = self.starttime
187
188 def report(self,status):
189 _time = time.clock()
190 if _time - self.lasttime > self.updateinterval:
191 self.lasttime = _time
192 _msg = "Solved %d vars in %d iterations" % (status.getNumConverged(),status.getIterationNum())
193 self.browser.statusbar.push(self.statusbarcontext, _msg )
194
195 while gtk.events_pending():
196 gtk.main_iteration()
197 return 0
198
199 def finalise(self,status):
200 self.report_to_browser(status)
201

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