Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2IO support for the qb@ll sys format.
4The positions and cell dimensions are in Bohrs.
6Contributed by Rafi Ullah <rraffiu@gmail.com>
7"""
9from ase.atoms import Atoms
10from ase.units import Bohr
12from re import compile
14__all__ = ['read_sys', 'write_sys']
17def read_sys(fileobj):
18 """
19 Function to read a qb@ll sys file.
21 fileobj: file (object) to read from.
22 """
23 a1, a2, a3, b1, b2, b3, c1, c2, c3 = fileobj.readline().split()[2:11]
24 cell = []
25 cell.append([float(a1) * Bohr, float(a2) * Bohr, float(a3) * Bohr])
26 cell.append([float(b1) * Bohr, float(b2) * Bohr, float(b3) * Bohr])
27 cell.append([float(c1) * Bohr, float(c2) * Bohr, float(c3) * Bohr])
28 positions = []
29 symbols = []
30 reg = compile(r'(\d+|\s+)')
31 line = fileobj.readline()
32 while 'species' in line:
33 line = fileobj.readline()
34 while line:
35 # The units column is ignored.
36 a, symlabel, spec, x, y, z = line.split()[0:6]
37 positions.append([float(x) * Bohr, float(y) * Bohr,
38 float(z) * Bohr])
39 sym = reg.split(str(symlabel))
40 symbols.append(sym[0])
41 line = fileobj.readline()
42 atoms = Atoms(symbols=symbols, cell=cell, positions=positions)
43 return atoms
46def write_sys(fileobj, atoms):
47 """
48 Function to write a sys file.
50 fileobj: file object
51 File to which output is written.
52 atoms: Atoms object
53 Atoms object specifying the atomic configuration.
54 """
55 fileobj.write('set cell')
56 for i in range(3):
57 d = atoms.cell[i] / Bohr
58 fileobj.write((' {:6f} {:6f} {:6f}').format(*d))
59 fileobj.write(' bohr\n')
61 ch_sym = atoms.get_chemical_symbols()
62 atm_nm = atoms.numbers
63 a_pos = atoms.positions
64 an = list(set(atm_nm))
66 for i, s in enumerate(set(ch_sym)):
67 fileobj.write(('species {}{} {}.xml \n').format(s, an[i], s))
68 for i, (S, Z, (x, y, z)) in enumerate(zip(ch_sym, atm_nm, a_pos)):
69 fileobj.write(('atom {0:5} {1:5} {2:12.6f} {3:12.6f} {4:12.6f}\
70 bohr\n').format(S + str(i + 1), S + str(Z), x / Bohr, y / Bohr,
71 z / Bohr))