/[ascend]/branches/adrian/pygtk/canvas/solverreporterforcanvas.py
ViewVC logotype

Contents of /branches/adrian/pygtk/canvas/solverreporterforcanvas.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2910 - (show annotations) (download) (as text)
Wed Apr 15 20:51:37 2015 UTC (4 years ago) by adrian
File MIME type: text/x-python
File size: 6180 byte(s)
Fixed solver status without parent issue
1 import time
2
3 from gi.repository import Gtk
4 import ascpy
5
6
7 class PythonSolverReporter(ascpy.SolverReporter):
8 def __init__(self,browser,message=None):
9 self.browser=browser
10 self.reporter = self.browser.reporter
11 self.starttime = time.clock()
12 ascpy.SolverReporter.__init__(self)
13
14 class PopupSolverReporter(PythonSolverReporter):
15 def __init__(self,browser,numvars):
16 PythonSolverReporter.__init__(self,browser)
17 self.progress_dialog = Gtk.Dialog("Solver Status", browser)
18
19
20 self.numvars = Gtk.Label()
21 self.numblocks = Gtk.Label()
22 self.elapsedtime = Gtk.Label()
23 self.numiterations = Gtk.Label()
24
25 self.progressbar = Gtk.ProgressBar()
26 self.progress_dialog.vbox.pack_start(self.numvars, True, True, 0)
27 self.progress_dialog.vbox.pack_start(self.numblocks, True, True, 0)
28 self.progress_dialog.vbox.pack_start(self.elapsedtime, True, True, 0)
29 self.progress_dialog.vbox.pack_start(self.numiterations, True, True, 0)
30 self.progress_dialog.vbox.pack_start(self.progressbar, True, True, 0)
31
32 self.closebutton = self.progress_dialog.add_button(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE)
33 self.stopbutton = self.progress_dialog.add_button(Gtk.STOCK_QUIT, Gtk.ResponseType.CANCEL)
34 self.closebutton.connect("clicked", self.on_progressdialog_close)
35 self.stopbutton.connect("clicked",self.on_progressdialog_stop)
36 self.closebutton.grab_default()
37
38 self.progress_dialog.show_all()
39 self.solvedvars = 0
40 self.lasttime = 0
41 self.blockstart = self.starttime
42 self.blocktime = 0;
43 self.elapsed = 0
44 self.blocknum = 0
45 self.guiinterrupt = False
46 self.guitime = 0
47
48 self.nv = numvars
49
50 while Gtk.events_pending():
51 Gtk.main_iteration()
52
53 def on_progressdialog_stop(self,*args):
54 self.guiinterrupt = True;
55
56 def on_progressdialog_close(self,widget):
57 self.progress_dialog.destroy()
58
59
60 def fill_values(self,status):
61 self.numblocks.set_text("Total Blocks : %d of %d" % (status.getCurrentBlockNum(),status.getNumBlocks()))
62 self.numvars.set_text("Converged : %d of %d" % (status.getNumConverged(), self.nv))
63 self.elapsedtime.set_text("Time Elapsed : %0.1f s" % self.elapsed)
64 self.numiterations.set_text("Iterations done : "+str(status.getIterationNum()))
65 _frac = float(status.getNumConverged()) / self.nv
66 self.progressbar.set_text("%d vars converged..." % status.getNumConverged());
67 self.progressbar.set_fraction(_frac)
68
69 def report(self,status):
70 _time = time.clock();
71 _sincelast = _time - self.lasttime
72 if status.getCurrentBlockNum() > self.blocknum:
73 self.blocknum = status.getCurrentBlockNum()
74 self.blockstart = _time
75 if self.lasttime==0 or status.isConverged():
76 self.lasttime = _time;
77 self.elapsed = _time - self.starttime
78 self.blocktime = _time - self.blockstart
79 print "UPDATING!"
80 self.fill_values(status)
81 while Gtk.events_pending():
82 Gtk.main_iteration()
83
84 self.guitime = self.guitime + (time.clock() - _time)
85 if status.isConverged() or status.isDiverged() or status.isInterrupted():
86 return 1
87 if self.guiinterrupt:
88 return 2
89 return 0
90
91 def finalise(self,status):
92 try:
93 _time = time.clock()
94
95 self.fill_values(status)
96
97 if status.isConverged():
98 self.progressbar.set_fraction(1.0)
99 self.progressbar.set_text("Converged")
100 self.browser.reporter.reportNote( "Solving successful : Canvasmodel Converged ! " )
101 self.browser.view.canvas.canvasmodelstate = 'Solved'
102 self.browser.status.push(0,"CanvasModel State :: %s" % self.browser.view.canvas.canvasmodelstate)
103 elif status.hasExceededTimeLimit():
104 self.progressbar.set_text("Exceeded time limit")
105 self.browser.reporter.reportError(" Solving failed : Solver exceeded time limit ")
106 self.browser.reporter.reportNote( " Canvasmodel Diverged ! " )
107 self.browser.view.canvas.canvasmodelstate = 'Diverged'
108 self.browser.status.push(0,"CanvasModel State :: %s" % self.browser.view.canvas.canvasmodelstate)
109 elif status.hasExceededIterationLimit():
110 self.progressbar.set_text("Exceeded iteration limit")
111 self.browser.reporter.reportError( " Solving failed : Solver exceeded iteration limit " )
112 self.browser.reporter.reportNote( " Canvasmodel Diverged ! " )
113 self.browser.view.canvas.canvasmodelstate = 'Diverged'
114 self.browser.status.push(0,"CanvasModel State :: %s" % self.browser.view.canvas.canvasmodelstate)
115 elif status.isDiverged():
116 self.progressbar.set_text("Diverged")
117 self.browser.reporter.reportError("Solving failed : Canvasmodel Diverged ! ")
118 self.browser.reporter.reportNote( " Canvasmodel Diverged ! " )
119 self.browser.view.canvas.canvasmodelstate = 'Diverged'
120 self.browser.status.push(0,"CanvasModel State :: %s" % self.browser.view.canvas.canvasmodelstate)
121 elif status.isOverDefined():
122 self.progressbar.set_text("Over-defined")
123 self.browser.reporter.reportError(" Solving failed : Canvasmodel over-defined ! ")
124 self.browser.reporter.reportNote( " Canvasmodel Diverged ! " )
125 self.browser.view.canvas.canvasmodelstate = 'Diverged'
126 self.browser.status.push(0,"CanvasModel State :: %s" % self.browser.view.canvas.canvasmodelstate)
127 elif status.isUnderDefined():
128 self.progressbar.set_text("Under-defined")
129 self.browser.reporter.reportError(" Solving failed : Canvasmodel under-defined ! ")
130 self.browser.reporter.reportNote( " Canvasmodel Diverged ! " )
131 self.browser.view.canvas.canvasmodelstate = 'Diverged'
132 self.browser.status.push(0,"CanvasModel State :: %s" % self.browser.view.canvas.canvasmodelstate)
133 elif status.hasResidualCalculationErrors():
134 self.progressbar.set_text("Residual Calculation Error")
135 self.browser.reporter.reportError(" Solving failed : Solver had residual calculation errors ")
136 self.browser.reporter.reportNote( " Canvasmodel Diverged ! " )
137 self.browser.view.canvas.canvasmodelstate = 'Diverged'
138 self.browser.status.push(0,"CanvasModel State :: %s" % self.browser.view.canvas.canvasmodelstate)
139
140 self.closebutton.set_sensitive(True)
141 self.stopbutton.set_sensitive(False)
142
143 self.guitime = self.guitime + (time.clock() - _time)
144 print "TIME SPENT UPDATING SOLVER: %0.2f s" % self.guitime
145 except Exception,e:
146 print "SOME PROBLEM: %s" % str(e)
147

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