Statistiques
| Révision :

root / ase / io / gpawtext.py @ 18

Historique | Voir | Annoter | Télécharger (2,76 ko)

1
from ase.atoms import Atom, Atoms
2
from ase.calculators.singlepoint import SinglePointCalculator
3

    
4

    
5
def read_gpaw_text(fileobj, index=-1):
6
    if isinstance(fileobj, str):
7
        fileobj = open(fileobj)
8

    
9
    def index_startswith(lines, string):
10
        for i, line in enumerate(lines):
11
            if line.startswith(string):
12
                return i
13
        raise ValueError
14

    
15
    lines = fileobj.readlines()
16
    images = []
17
    while True:
18
        try:
19
            i = lines.index('Unit Cell:\n')
20
        except ValueError:
21
            pass
22
        else:
23
            cell = []
24
            pbc = []
25
            for line in lines[i + 3:i + 6]:
26
                words = line.split()
27
                if len(words) == 5:  # old format
28
                    cell.append(float(words[2]))
29
                    pbc.append(words[1] == 'yes')
30
                else:                # new format with GUC
31
                    cell.append([float(word) for word in words[3:6]])
32
                    pbc.append(words[2] == 'yes')
33
            
34
        try:
35
            i = lines.index('Positions:\n')
36
        except ValueError:
37
            break
38

    
39
        atoms = Atoms(cell=cell, pbc=pbc)
40
        for line in lines[i + 1:]:
41
            words = line.split()
42
            if len(words) != 5:
43
                break
44
            n, symbol, x, y, z = words
45
            symbol = symbol.split('.')[0]
46
            atoms.append(Atom(symbol, [float(x), float(y), float(z)]))
47
        lines = lines[i + 5:]
48
        try:
49
            i = lines.index('-------------------------\n')
50
        except ValueError:
51
            e = None
52
        else:
53
            line = lines[i + 9]
54
            assert line.startswith('Zero Kelvin:')
55
            e = float(line.split()[-1])
56
        try:
57
            ii = index_startswith(lines, 'Total Charge:')
58
        except ValueError:
59
            q = None
60
        else:
61
            q = float(lines[ii].split()[2])
62
        try:
63
            ii = lines.index('Forces in eV/Ang:\n')
64
        except ValueError:
65
            f = None
66
        else:
67
            f = []
68
            for i in range(ii + 1, ii + 1 + len(atoms)):
69
                try:
70
                    x, y, z = lines[i].split()[-3:]
71
                    f.append((float(x), float(y), float(z)))
72
                except (ValueError, IndexError), m:
73
                    raise IOError('Malformed GPAW log file: %s' % m)
74

    
75
        if len(images) > 0 and e is None:
76
            break
77

    
78
        if e is not None or f is not None:
79
            atoms.set_calculator(SinglePointCalculator(e, f, None, None, atoms)) ### Fixme magmoms
80
        if q is not None:
81
            n = len(atoms)
82
            atoms.set_charges([q / n] * n)
83

    
84
        images.append(atoms)
85
        lines = lines[i:]
86

    
87
    if len(images) == 0:
88
        raise IOError('Corrupted GPAW-text file!')
89
    
90
    return images[index]