~jrjohansson/qutip/master

« back to all changes in this revision

Viewing changes to test.py

  • Committer: Paul Nation
  • Date: 2011-04-21 04:46:56 UTC
  • Revision ID: git-v1:dd4c966b490aa468dfbd28cef66694df4bf235c8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#This file is part of QuTIP.
 
2
#
 
3
#    QuTIP is free software: you can redistribute it and/or modify
 
4
#    it under the terms of the GNU General Public License as published by
 
5
#    the Free Software Foundation, either version 3 of the License, or
 
6
#   (at your option) any later version.
 
7
#
 
8
#    QuTIP is distributed in the hope that it will be useful,
 
9
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
#    GNU General Public License for more details.
 
12
#
 
13
#    You should have received a copy of the GNU General Public License
 
14
#    along with QuTIP.  If not, see <http://www.gnu.org/licenses/>.
 
15
###########################################################################
 
16
from qutip import *
 
17
import sys,time
 
18
###-platform dependent imports-###
 
19
if sys.platform=='darwin':
 
20
    from qutip.mac import ProgressBar
 
21
else:
 
22
    from qutip.linux import Tk_ProgressBar,center
 
23
    import Tkinter
 
24
#---------------------------------
 
25
 
 
26
class Counter():
 
27
    def __init__(self,max,step_size=1):
 
28
        self.max=max
 
29
        self.count=0
 
30
        self.step=step_size
 
31
        self.percent=0.0
 
32
        if sys.platform=='darwin':
 
33
            self.bar = ProgressBar(title="Running Monte-Carlo Trajectories:")
 
34
        else:
 
35
            self.root = Tkinter.Tk(className=' Running Monte-Carlo Trajectories:')
 
36
            self.root.withdraw()
 
37
            self.root.focus()
 
38
            self.root.after(0,center,self.root)
 
39
            self.bar=Tk_ProgressBar(self.root, relief='ridge', bd=3)
 
40
            self.bar.pack(fill='x')
 
41
            self.bar.set(self.percent, str(self.percent*100)+' %')
 
42
                
 
43
    def update(self):#update the counter
 
44
        self.count+=self.step
 
45
        self.percent=self.count/(1.0*self.max)
 
46
        if sys.platform=='darwin':
 
47
            self.bar.update(self.percent*100.0, "Trajectories completed: "+str(self.count)+"/"+str(self.max))
 
48
        else:
 
49
            self.bar.set(self.percent,"Trajectories completed: "+str(self.count)+"/"+str(self.max))
 
50
    
 
51
    def finish(self):#pause and close and finish
 
52
        time.sleep(0.5)
 
53
        if sys.platform=='darwin':
 
54
            self.bar.finish()
 
55
        else:
 
56
            self.root.destroy()
 
57
 
 
58
 
 
59
######---Demo---######
 
60
if __name__ == "__main__":
 
61
    x=Counter(100)
 
62
    def func(x):
 
63
        for i in range(100):
 
64
            time.sleep(.1)
 
65
            x.update()
 
66
        time.sleep(.5)
 
67
        x.finish()
 
68
    
 
69
 
 
70
    if sys.platform!='darwin':
 
71
        x.bar.after(0,lambda: func(x))
 
72
        x.root.mainloop()
 
73
    else:
 
74
        func(x)
 
75
    print 'finished'
 
76
 
 
77
 
 
78
 
 
79
 
 
80
 
 
81
 
 
82
 
 
83
 
 
84
 
 
85
           
 
 
b'\\ No newline at end of file'