Statistiques
| Révision :

root / ase / io / xyz.py @ 15

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

1
from math import pi, cos, sin, sqrt, acos
2

    
3
from ase.atoms import Atoms
4
from ase.parallel import paropen
5

    
6

    
7
def read_xyz(fileobj, index=-1):
8
    if isinstance(fileobj, str):
9
        fileobj = open(fileobj)
10

    
11
    lines = fileobj.readlines()
12
    L1 = lines[0].split()
13
    if len(L1) == 1:
14
        del lines[:2]
15
        natoms = int(L1[0])
16
    else:
17
        natoms = len(lines)
18
    images = []
19
    while len(lines) >= natoms:
20
        positions = []
21
        symbols = []
22
        for line in lines[:natoms]:
23
            symbol, x, y, z = line.split()[:4]
24
            symbols.append(symbol)
25
            positions.append([float(x), float(y), float(z)])
26
        images.append(Atoms(symbols=symbols, positions=positions))
27
        del lines[:natoms + 2]
28
    return images[index]
29

    
30
def write_xyz(fileobj, images):
31
    if isinstance(fileobj, str):
32
        fileobj = paropen(fileobj, 'w')
33

    
34
    if not isinstance(images, (list, tuple)):
35
        images = [images]
36

    
37
    symbols = images[0].get_chemical_symbols()
38
    natoms = len(symbols)
39
    for atoms in images:
40
        fileobj.write('%d\n\n' % natoms)
41
        for s, (x, y, z) in zip(symbols, atoms.get_positions()):
42
            fileobj.write('%-2s %22.15f %22.15f %22.15f\n' % (s, x, y, z))