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

Contents of /trunk/pygtk/solverreporter.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 890 - (show annotations) (download) (as text)
Thu Oct 12 14:06:37 2006 UTC (14 years, 3 months ago) by johnpye
File MIME type: text/x-python
File size: 6144 byte(s)
Eliminated T1, T2 from dsg.a4c.
Added moody diagram example model (with python script)
Removed some debug output from solverreporter.py and simulation.cpp
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 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,browser,numvars):
41 PythonSolverReporter.__init__(self,browser)
42
43 _xml = gtk.glade.XML(browser.glade_file,"solverstatusdialog")
44 _xml.signal_autoconnect(self)
45
46 self.window = _xml.get_widget("solverstatusdialog")
47 if self.browser.icon:
48 self.window.set_icon(self.browser.icon)
49 self.window.set_transient_for(self.browser.window)
50
51 self.numvars = _xml.get_widget("numvarsentry")
52 self.numblocks = _xml.get_widget("numblocksentry")
53 self.elapsedtime = _xml.get_widget("elapsedtimeentry")
54 self.numiterations = _xml.get_widget("numiterationsentry")
55 self.blockvars = _xml.get_widget("blockvarsentry")
56 self.blockiterations = _xml.get_widget("blockiterationsentry")
57 self.blockresidual = _xml.get_widget("blockresidualentry")
58 self.blockelapsedtime = _xml.get_widget("blockelapsedtimeentry")
59
60 self.progressbar = _xml.get_widget("progressbar")
61 self.closebutton = _xml.get_widget("closebutton")
62 self.stopbutton = _xml.get_widget("stopbutton")
63
64 #print "SOLVER REPORTER ---- PYTHON"
65
66 self.solvedvars = 0;
67
68 self.lasttime = 0;
69 self.blockstart = self.starttime;
70 self.blocktime = 0;
71 self.elapsed = 0;
72 self.blocknum = 0;
73 self.guiinterrupt = False;
74 self.guitime = 0;
75
76 self.nv = numvars
77
78 while gtk.events_pending():
79 gtk.main_iteration()
80
81 def run(self):
82 self.window.run()
83
84 def on_stopbutton_clicked(self,*args):
85 print "STOPPING..."
86 self.guiinterrupt = True;
87
88 def on_solverstatusdialog_close(self,*args):
89 self.window.response(gtk.RESPONSE_CLOSE)
90
91 def on_solverstatusdialog_response(self,response,*args):
92 self.window.hide()
93 del(self.window)
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 _time = time.clock()
136
137 _p = self.browser.prefs;
138 _close_on_converged = _p.getBoolPref("SolverReporter","close_on_converged",True);
139 _close_on_nonconverged = _p.getBoolPref("SolverReporter","close_on_nonconverged",False);
140
141
142 if status.isConverged() and _close_on_converged:
143 self.report_to_browser(status)
144 self.window.response(gtk.RESPONSE_CLOSE)
145 return
146
147 if not status.isConverged() and _close_on_nonconverged:
148 self.report_to_browser(status)
149 self.window.response(gtk.RESPONSE_CLOSE)
150 return
151
152 self.fill_values(status)
153
154 if status.isConverged():
155 self.progressbar.set_fraction(1.0)
156 self.progressbar.set_text("Converged")
157 elif status.hasExceededTimeLimit():
158 self.progressbar.set_text("Exceeded time limit")
159 elif status.hasExceededIterationLimit():
160 self.progressbar.set_text("Exceeded iteration limit")
161 elif status.isDiverged():
162 self.progressbar.set_text("Diverged")
163
164 self.closebutton.set_sensitive(True)
165 self.stopbutton.set_sensitive(False)
166
167 self.report_to_browser(status)
168
169 self.guitime = self.guitime + (time.clock() - _time)
170 print "TIME SPENT UPDATING SOLVER: %0.2f s" % self.guitime
171
172
173 class SimpleSolverReporter(PythonSolverReporter):
174 def __init__(self,browser):
175 #print "CREATING SIMPLESOLVERREPORTER..."
176 PythonSolverReporter.__init__(self,browser)
177 self.lasttime = self.starttime
178
179 def report(self,status):
180 _time = time.clock()
181 if _time - self.lasttime > self.updateinterval:
182 self.lasttime = _time
183 _msg = "Solved %d vars in %d iterations" % (status.getNumConverged(),status.getIterationNum())
184 self.browser.statusbar.push(self.statusbarcontext, _msg )
185
186 while gtk.events_pending():
187 gtk.main_iteration()
188 return 0
189
190 def finalise(self,status):
191 self.report_to_browser(status)
192

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