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

Contents of /trunk/pygtk/studyreporter.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2598 - (show annotations) (download) (as text)
Tue Apr 17 21:00:29 2012 UTC (12 years, 3 months ago) by jpye
File MIME type: text/x-python
File size: 8722 byte(s)
Rename to StudyReporter in studyreporter.py (seems to be duplication from solverreporter.py?).
Fixed some unit conversions in tmy3.c, test file seems correct now (to float precision at least).
Suppressed some debug output to try to speed up Study feature, not there yet.
1 import ascpy
2 import time
3 import gtk
4
5 class PythonSolverReporter(ascpy.SolverReporter):
6 def __init__(self,browser,message=None):
7 self.browser=browser
8 self.updateinterval = self.browser.prefs.getBoolPref("SolverReporter","update_interval", 0.5)
9 self.reporter = self.browser.reporter
10 if self.reporter==None:
11 raise RuntimeError("Can't find reporter")
12 self.starttime = time.clock()
13 self.statusbarcontext = self.browser.statusbar.get_context_id("pythonstudyreporter")
14 if message:
15 self.browser.statusbar.push(self.statusbarcontext,"Solving (%s)..." % message)
16 else:
17 self.browser.statusbar.push(self.statusbarcontext,"Solving..." )
18 ascpy.SolverReporter.__init__(self)
19
20 def report_to_browser(self,status):
21 self.browser.statusbar.pop(self.statusbarcontext)
22
23 if status.isConverged():
24 #self.reporter.reportSuccess("Converged for %s = %0.2f" % (self.browser.sim.getInstanceName(self.instance),
25 # self.instance.getRealValue()))
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 for tabs in self.browser.observers:
43 if tabs.alive:
44 tabs.taint_row(_msg)
45 _msg = _msg + " for %s = %0.2f" % (self.browser.sim.getInstanceName(self.instance), self.instance.getRealValue())
46 self.reporter.reportError(_msg)
47
48 class StudyReporter(PythonSolverReporter):
49 def __init__(self, browser, numvars, instance, nsteps, study):
50 PythonSolverReporter.__init__(self,browser)
51
52 self.browser.builder.add_objects_from_file(self.browser.glade_file, ["studystatusdialog"])
53 self.study = study
54 self.window = self.browser.builder.get_object("studystatusdialog")
55 self.browser.builder.connect_signals(self)
56 if self.browser.icon:
57 self.window.set_icon(self.browser.icon)
58 self.window.set_transient_for(self.browser.window)
59
60 self.studyvar = self.browser.builder.get_object("studyvarentry")
61 self.currentvalue = self.browser.builder.get_object("currentvalueentry")
62 self.points = self.browser.builder.get_object("pointsentry")
63 self.totaltime = self.browser.builder.get_object("totaltimeentry")
64 self.currentrun = self.browser.builder.get_object("currentrunlabel")
65 self.numvars = self.browser.builder.get_object("numvarsentry1")
66 self.numblocks = self.browser.builder.get_object("numblocksentry1")
67 self.elapsedtime = self.browser.builder.get_object("elapsedtimeentry1")
68 self.numiterations = self.browser.builder.get_object("numiterationsentry1")
69 self.blockvars = self.browser.builder.get_object("blockvarsentry1")
70 self.blockiterations = self.browser.builder.get_object("blockiterationsentry1")
71 self.blockresidual = self.browser.builder.get_object("blockresidualentry1")
72 self.blockelapsedtime = self.browser.builder.get_object("blockelapsedtimeentry1")
73
74 self.currentprogressbar = self.browser.builder.get_object("currentprogressbar")
75 self.totalprogressbar = self.browser.builder.get_object("totalprogressbar")
76 self.closebutton = self.browser.builder.get_object("closebutton6")
77 self.stopbutton = self.browser.builder.get_object("stopbutton1")
78
79 #print "SOLVER REPORTER ---- PYTHON"
80 _p = self.browser.prefs
81 self.continue_on_fail = _p.getBoolPref("StudyReporter", "continue_on_fail", True)
82 self.solvedvars = 0;
83
84 self.lasttime = 0;
85 self.blockstart = self.starttime;
86 self.blocktime = 0;
87 self.elapsed = 0;
88 self.blocknum = 0;
89 self.guiinterrupt = False;
90 self.guitime = 0;
91 self.studybegintime = time.clock()
92 self.totalelapsed = 0
93 self.nv = numvars
94 self.instance = instance
95 self.nsteps = nsteps
96 self.pointsdone = 0
97 self.allconverged = True
98 while gtk.events_pending():
99 gtk.main_iteration()
100
101 def on_stopbutton_activate(self,*args):
102 self.guiinterrupt = True
103
104 def on_studystatusdialog_response(self,widget,response):
105 self.guiinterrupt = True
106 self.window.destroy()
107
108 def fill_values(self,status):
109
110 self.studyvar.set_text(self.browser.sim.getInstanceName(self.instance))
111 self.currentvalue.set_text(str(self.instance.getRealValue()))
112 self.points.set_text("%d out of %d solved" % (self.pointsdone, self.nsteps+1))
113 self.currentrun.set_text("%s = %0.2f" % (self.browser.sim.getInstanceName(self.instance), self.instance.getRealValue()))
114 self.totaltime.set_text("%0.1f s" % self.totalelapsed)
115 self.numblocks.set_text("%d of %d" % (status.getCurrentBlockNum(),status.getNumBlocks()))
116 self.numvars.set_text("%d of %d" % (status.getNumConverged(), self.nv))
117 self.elapsedtime.set_text("%0.1f s" % self.elapsed)
118 self.numiterations.set_text(str(status.getIterationNum()))
119 self.blockvars.set_text(str(status.getCurrentBlockSize()))
120 self.blockiterations.set_text(str(status.getCurrentBlockIteration()))
121 self.blockresidual.set_text("%8.5e" % status.getBlockResidualRMS())
122 self.blockelapsedtime.set_text("%0.1f s" % self.blocktime)
123
124 _frac = float(status.getNumConverged()) / self.nv
125 self.currentprogressbar.set_text("%d vars converged..." % status.getNumConverged());
126 self.currentprogressbar.set_fraction(_frac)
127
128 _frac2 = float(self.pointsdone) / (self.nsteps+1)
129 self.totalprogressbar.set_text("%d points solved..." % self.pointsdone);
130 self.totalprogressbar.set_fraction(_frac2)
131
132 def updateVarDetails(self, pointsdone):
133 self.pointsdone = pointsdone
134
135 _frac2 = float(self.pointsdone) / (self.nsteps+1)
136 self.totalprogressbar.set_text("%d points solved..." % self.pointsdone);
137 self.totalprogressbar.set_fraction(_frac2)
138 self.points.set_text("%d out of %d solved" % (self.pointsdone, self.nsteps+1))
139
140 self.starttime = time.clock()
141 self.blocknum = 0
142
143 def report(self,status):
144 _time = time.clock();
145 _sincelast = _time - self.lasttime
146 if status.getCurrentBlockNum() > self.blocknum:
147 self.blocknum = status.getCurrentBlockNum()
148 self.blockstart = _time
149
150 if self.lasttime==0 or _sincelast > self.updateinterval:
151 self.lasttime = _time;
152 self.elapsed = _time - self.starttime
153 self.totalelapsed = _time - self.studybegintime
154 self.blocktime = _time - self.blockstart
155 #print "UPDATING!"
156 self.fill_values(status)
157
158 while gtk.events_pending():
159 gtk.main_iteration()
160
161 self.guitime = self.guitime + (time.clock() - _time)
162
163 if status.isConverged() or status.isDiverged() or status.isInterrupted():
164 return 1
165 if self.guiinterrupt:
166 return 2
167 return 0
168
169 def finalise(self,status):
170 try:
171 _time = time.clock()
172 _sincelast = _time - self.lasttime
173 if _sincelast > self.updateinterval:
174 self.fill_values(status)
175
176 if status.isConverged():
177 self.report_to_browser(status)
178 # print "Converged for %s = %s" % (self.browser.sim.getInstanceName(self.instance), self.instance.getRealValue())
179 #add row in the observer tabs
180 for tabs in self.browser.observers:
181 if tabs.alive:
182 tabs.do_add_row()
183 if self.pointsdone == (self.nsteps):
184 self.window.response(gtk.RESPONSE_CLOSE)
185 return
186
187 if not status.isConverged():
188 print "NOT Converged for %s = %s" % (self.browser.sim.getInstanceName(self.instance),
189 self.instance.getRealValue())
190 self.allconverged = False
191 self.report_to_browser(status)
192 for tabs in self.browser.observers:
193 if tabs.alive:
194 tabs.do_add_row()
195 if self.continue_on_fail is True:
196 if self.pointsdone == self.nsteps:
197 self.closebutton.set_sensitive(True)
198 self.stopbutton.set_sensitive(False)
199 return
200 else:
201 self.guiinterrupt = True
202
203 if status.isConverged():
204 self.currentprogressbar.set_fraction(1.0)
205 self.currentprogressbar.set_text("Converged")
206 elif status.hasExceededTimeLimit():
207 self.currentprogressbar.set_text("Exceeded time limit")
208 elif status.hasExceededIterationLimit():
209 self.currentprogressbar.set_text("Exceeded iteration limit")
210 elif status.isDiverged():
211 self.currentprogressbar.set_text("Diverged")
212 elif status.isOverDefined():
213 self.currentprogressbar.set_text("Over-defined")
214 elif status.isUnderDefined():
215 self.currentprogressbar.set_text("Under-defined")
216
217 self.closebutton.set_sensitive(True)
218 self.stopbutton.set_sensitive(False)
219
220 self.report_to_browser(status)
221
222 self.guitime = self.guitime + (time.clock() - _time)
223 print "TIME SPENT UPDATING SOLVER: %0.2f s" % self.guitime
224 except Exception,e:
225 print "SOME PROBLEM: %s" % str(e)

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