Coverage for /builds/ase/ase/ase/io/sys.py: 100.00%
38 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-08-02 00:12 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-08-02 00:12 +0000
1# fmt: off
3"""
4IO support for the qb@ll sys format.
6The positions and cell dimensions are in Bohrs.
8Contributed by Rafi Ullah <rraffiu@gmail.com>
9"""
11from re import compile
13from ase.atoms import Atoms
14from ase.units import Bohr
16__all__ = ['read_sys', 'write_sys']
19def read_sys(fileobj):
20 """
21 Function to read a qb@ll sys file.
23 fileobj: file (object) to read from.
24 """
25 a1, a2, a3, b1, b2, b3, c1, c2, c3 = fileobj.readline().split()[2:11]
26 cell = []
27 cell.append([float(a1) * Bohr, float(a2) * Bohr, float(a3) * Bohr])
28 cell.append([float(b1) * Bohr, float(b2) * Bohr, float(b3) * Bohr])
29 cell.append([float(c1) * Bohr, float(c2) * Bohr, float(c3) * Bohr])
30 positions = []
31 symbols = []
32 reg = compile(r'(\d+|\s+)')
33 line = fileobj.readline()
34 while 'species' in line:
35 line = fileobj.readline()
36 while line:
37 # The units column is ignored.
38 _a, symlabel, _spec, x, y, z = line.split()[0:6]
39 positions.append([float(x) * Bohr, float(y) * Bohr,
40 float(z) * Bohr])
41 sym = reg.split(str(symlabel))
42 symbols.append(sym[0])
43 line = fileobj.readline()
44 atoms = Atoms(symbols=symbols, cell=cell, positions=positions)
45 return atoms
48def write_sys(fileobj, atoms):
49 """
50 Function to write a sys file.
52 fileobj: file object
53 File to which output is written.
54 atoms: Atoms object
55 Atoms object specifying the atomic configuration.
56 """
57 fileobj.write('set cell')
58 for i in range(3):
59 d = atoms.cell[i] / Bohr
60 fileobj.write((' {:6f} {:6f} {:6f}').format(*d))
61 fileobj.write(' bohr\n')
63 ch_sym = atoms.get_chemical_symbols()
64 atm_nm = atoms.numbers
65 a_pos = atoms.positions
66 an = list(set(atm_nm))
68 for i, s in enumerate(set(ch_sym)):
69 fileobj.write(('species {}{} {}.xml \n').format(s, an[i], s))
70 for i, (S, Z, (x, y, z)) in enumerate(zip(ch_sym, atm_nm, a_pos)):
71 fileobj.write(('atom {:5} {:5} {:12.6f} {:12.6f} {:12.6f}\
72 bohr\n').format(S + str(i + 1), S + str(Z), x / Bohr, y / Bohr,
73 z / Bohr))