Statistiques
| Révision :

root / ase / gui / status.py @ 15

Historique | Voir | Annoter | Télécharger (3,39 ko)

1
# -*- coding: utf-8 -*-
2

    
3
from math import sqrt, pi, acos
4

    
5
import gtk
6
import numpy as np
7

    
8
from ase.data import chemical_symbols as symbols
9
from ase.data import atomic_names as names
10
from ase.gui.widgets import pack
11
from ase.gui.languages import translate as _
12

    
13
def formula(Z):
14
    hist = {}
15
    for z in Z:
16
        if z in hist:
17
            hist[z] += 1
18
        else:
19
            hist[z] = 1
20
    text = ''
21
    Z = hist.keys()
22
    Z.sort()
23
    for z in Z:
24
        text += symbols[z]
25
        n = hist[z]
26
        if n > 1:
27
            text += '<sub>%d</sub>' % n
28
    return text
29

    
30
class Status:
31
    def __init__(self, vbox):
32
        self.eventbox = gtk.EventBox()
33
        self.label = gtk.Label()
34
        self.eventbox.add(self.label)
35
        self.label.show()
36
        if gtk.pygtk_version < (2, 12):
37
            self.set_tip(self.eventbox, _('Tip for status box ...'))
38
        else:
39
            self.eventbox.set_tooltip_text(_('Tip for status box ...'))
40
        pack(vbox, self.eventbox)
41
        self.ordered_indices = []
42

    
43
    def status(self):
44
        # use where here:  XXX
45
        indices = np.arange(self.images.natoms)[self.images.selected]
46
        ordered_indices = self.images.selected_ordered
47
        n = len(indices)
48
        self.nselected = n
49
        
50
        if n == 0:
51
            self.label.set_text('')
52
            return
53

    
54
        Z = self.images.Z[indices]
55
        R = self.R[indices]
56

    
57
        if n == 1:
58
            tag = self.images.T[indices][0]
59
            mom = self.images.M[self.frame][indices]
60
            text = (u' #%d %s (%s): %.3f Å, %.3f Å, %.3f Å ' %
61
                    ((indices[0], names[Z[0]], symbols[Z[0]]) + tuple(R[0])))
62
        
63
            text+=' tag=%s mom=%1.2f' % (tag, mom)        
64
        elif n == 2:
65
            D = R[0] - R[1]
66
            d = sqrt(np.dot(D, D))
67
            text = u' %s-%s: %.3f Å' % (symbols[Z[0]], symbols[Z[1]], d)
68
        elif n == 3:
69
            d = []
70
            for c in range(3):
71
                D = R[c] - R[(c + 1) % 3]
72
                d.append(np.dot(D, D))
73
            a = []
74
            for c in range(3):
75
                t1 = 0.5 * (d[c] + d[(c + 1) % 3] - d[(c + 2) % 3])
76
                t2 = sqrt(d[c] * d[(c + 1) % 3])
77
                try:
78
                    t3 = acos(t1 / t2)
79
                except ValueError:
80
                    if t1 > 0:
81
                        t3 = 0
82
                    else:
83
                        t3 = pi
84
                a.append(t3 * 180 / pi)
85
            text = (u' %s-%s-%s: %.1f°, %.1f°, %.1f°' %
86
                    tuple([symbols[z] for z in Z] + a))
87
        elif len(ordered_indices) == 4:
88
            R = self.R[ordered_indices]
89
            Z = self.images.Z[ordered_indices]
90
            a    = R[1]-R[0]
91
            b    = R[2]-R[1]
92
            c    = R[3]-R[2]
93
            bxa  = np.cross(b,a)
94
            bxa /= np.sqrt(np.vdot(bxa,bxa))
95
            cxb  = np.cross(c,b)
96
            cxb /= np.sqrt(np.vdot(cxb,cxb))
97
            angle = np.vdot(bxa,cxb)
98
            if angle < -1: angle = -1
99
            if angle >  1: angle =  1
100
            angle = np.arccos(angle)
101
            if (np.vdot(bxa,c)) > 0: angle = 2*np.pi-angle
102
            angle = angle*180.0/np.pi
103
            text = ( u'dihedral %s->%s->%s->%s: %.1f°' % tuple([symbols[z] for z in Z]+[angle]))
104
        else:
105
            text = ' ' + formula(Z)
106
            
107
        self.label.set_markup(text)
108
        
109
if __name__ == '__main__':
110
    import os
111
    os.system('python gui.py')