Statistiques
| Révision :

root / ase / old.py

Historique | Voir | Annoter | Télécharger (6,56 ko)

1
import numpy as np
2
try:
3
    import Numeric as num
4
except ImportError:
5
    pass
6
else:
7
    def npy2num(a, typecode=num.Float):
8
        return num.array(a, typecode)
9
    if num.__version__ <= '23.8':
10
        #def npy2num(a, typecode=num.Float):
11
        #    return num.array(a.tolist(), typecode)
12
        def npy2num(a, typecode=num.Float):
13
            b = num.fromstring(a.tostring(), typecode)
14
            b.shape = a.shape
15
            return b
16
    
17
from ase.data import chemical_symbols
18

    
19

    
20
class OldASEListOfAtomsWrapper:
21
    def __init__(self, atoms):
22
        self.atoms = atoms
23
        self.constraints = []
24

    
25
    def get_positions(self):
26
        return np.array(self.atoms.GetCartesianPositions())
27

    
28
    def get_calculator(self):
29
        calc = self.atoms.GetCalculator()
30
        if calc is not None:
31
            return OldASECalculatorWrapper(calc)
32

    
33
    def get_potential_energy(self):
34
        return self.atoms.GetPotentialEnergy()
35

    
36
    def get_forces(self):
37
        return np.array(self.atoms.GetCartesianForces())
38

    
39
    def get_stress(self):
40
        return np.array(self.atoms.GetStress())
41

    
42
    def get_atomic_numbers(self):
43
        return np.array(self.atoms.GetAtomicNumbers())
44

    
45
    def get_tags(self):
46
        return np.array(self.atoms.GetTags())
47
    
48
    def get_momenta(self):
49
        return np.array(self.atoms.GetCartesianMomenta())
50
    
51
    def get_masses(self):
52
        return np.array(self.atoms.GetMasses())
53
    
54
    def get_initial_magnetic_moments(self):
55
        return np.array(self.atoms.GetMagneticMoments())
56
    
57
    def get_magnetic_moments(self):
58
        return None
59
    
60
    def get_charges(self):
61
        return np.zeros(len(self))
62

    
63
    def has(self, name):
64
        return True
65
    
66
    def get_cell(self):
67
        return np.array(self.atoms.GetUnitCell())
68

    
69
    def get_pbc(self):
70
        return np.array(self.atoms.GetBoundaryConditions(), bool)
71

    
72
    def __len__(self):
73
        return len(self.atoms)
74

    
75
    def copy(self):
76
        from ase.atoms import Atoms
77
        return Atoms(positions=self.get_positions(),
78
                     numbers=self.get_atomic_numbers(),
79
                     tags=self.get_tags(),
80
                     momenta=self.get_momenta(),
81
                     masses=self.get_masses(),
82
                     magmoms=self.get_initial_magnetic_moments(),
83
                     charges=self.get_charges(),
84
                     cell=self.get_cell(),
85
                     pbc=self.get_pbc(),
86
                     constraint=None,
87
                     calculator=None) # Don't copy the calculator
88

    
89

    
90
class OldASECalculatorWrapper:
91
    def __init__(self, calc, atoms=None):
92
        self.calc = calc
93
            
94
        if atoms is None:
95
            try:
96
                self.atoms = calc.GetListOfAtoms()
97
            except AttributeError:
98
                self.atoms = None
99
        else:
100
            from ASE import Atom, ListOfAtoms
101
            
102
            numbers = atoms.get_atomic_numbers()
103
            positions = atoms.get_positions()
104
            magmoms = atoms.get_initial_magnetic_moments()
105
            self.atoms = ListOfAtoms(
106
                [Atom(Z=numbers[a], position=positions[a], magmom=magmoms[a])
107
                 for a in range(len(atoms))],
108
                cell=npy2num(atoms.get_cell()),
109
                periodic=tuple(atoms.get_pbc()))
110
            self.atoms.SetCalculator(calc)
111

    
112
    def get_atoms(self):
113
        return OldASEListOfAtomsWrapper(self.atoms)
114
            
115
    def get_potential_energy(self, atoms):
116
        self.atoms.SetCartesianPositions(npy2num(atoms.get_positions()))
117
        self.atoms.SetUnitCell(npy2num(atoms.get_cell()), fix=True)
118
        return self.calc.GetPotentialEnergy()
119

    
120
    def get_forces(self, atoms):
121
        self.atoms.SetCartesianPositions(npy2num(atoms.get_positions()))
122
        self.atoms.SetUnitCell(npy2num(atoms.get_cell()), fix=True)
123
        return np.array(self.calc.GetCartesianForces())
124

    
125
    def get_stress(self, atoms):
126
        self.atoms.SetCartesianPositions(npy2num(atoms.get_positions()))
127
        self.atoms.SetUnitCell(npy2num(atoms.get_cell()), fix=True)
128
        return np.array(self.calc.GetStress())
129

    
130
    def get_number_of_bands(self):
131
        return self.calc.GetNumberOfBands()
132

    
133
    def get_kpoint_weights(self):
134
        return np.array(self.calc.GetIBZKPointWeights())
135

    
136
    def get_number_of_spins(self):
137
        return 1 + int(self.calc.GetSpinPolarized())
138

    
139
    def get_eigenvalues(self, kpt=0, spin=0):
140
        return np.array(self.calc.GetEigenvalues(kpt, spin))
141

    
142
    def get_fermi_level(self):
143
        return self.calc.GetFermiLevel()
144

    
145
    def get_number_of_grid_points(self):
146
        return np.array(self.get_pseudo_wave_function(0, 0, 0).shape)
147

    
148
    def get_pseudo_wave_function(self, n=0, k=0, s=0, pad=True):
149
        kpt = self.get_bz_k_points()[k]
150
        state = self.calc.GetElectronicStates().GetState(band=n, spin=s,
151
                                                         kptindex=k)
152

    
153
        # Get wf, without bolch phase (Phase = True doesn't do anything!)
154
        wave = state.GetWavefunctionOnGrid(phase=False)
155

    
156
        # Add bloch phase if this is not the Gamma point
157
        if np.all(kpt == 0):
158
            return wave
159
        coord = state.GetCoordinates()
160
        phase = coord[0] * kpt[0] + coord[1] * kpt[1] + coord[2] * kpt[2]
161
        return np.array(wave) * np.exp(-2.j * np.pi * phase) # sign! XXX
162

    
163
        #return np.array(self.calc.GetWaveFunctionArray(n, k, s)) # No phase!
164

    
165
    def get_bz_k_points(self):
166
        return np.array(self.calc.GetBZKPoints())
167

    
168
    def get_ibz_k_points(self):
169
        return np.array(self.calc.GetIBZKPoints())
170

    
171
    def get_wannier_localization_matrix(self, nbands, dirG, kpoint,
172
                                        nextkpoint, G_I, spin):
173
        return np.array(self.calc.GetWannierLocalizationMatrix(
174
            G_I=G_I.tolist(), nbands=nbands, dirG=dirG.tolist(),
175
            kpoint=kpoint, nextkpoint=nextkpoint, spin=spin))
176
    
177
    def initial_wannier(self, initialwannier, kpointgrid, fixedstates,
178
                        edf, spin):
179
        # Use initial guess to determine U and C
180
        init = self.calc.InitialWannier(initialwannier, self.atoms,
181
                                        npy2num(kpointgrid, num.Int))
182

    
183
        states = self.calc.GetElectronicStates()
184
        waves = [[state.GetWaveFunction()
185
                  for state in states.GetStatesKPoint(k, spin)]
186
                 for k in self.calc.GetIBZKPoints()] 
187

    
188
        init.SetupMMatrix(waves, self.calc.GetBZKPoints())
189
        c, U = init.GetListOfCoefficientsAndRotationMatrices(
190
            (self.calc.GetNumberOfBands(), fixedstates, edf))
191
        U = np.array(U)
192
        for k in range(len(c)):
193
            c[k] = np.array(c[k])
194
        return c, U