Coverage for ase/io/sys.py: 100.00%

38 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1""" 

2IO support for the qb@ll sys format. 

3 

4The positions and cell dimensions are in Bohrs. 

5 

6Contributed by Rafi Ullah <rraffiu@gmail.com> 

7""" 

8 

9from re import compile 

10 

11from ase.atoms import Atoms 

12from ase.units import Bohr 

13 

14__all__ = ['read_sys', 'write_sys'] 

15 

16 

17def read_sys(fileobj): 

18 """ 

19 Function to read a qb@ll sys file. 

20 

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, float(z) * Bohr]) 

38 sym = reg.split(str(symlabel)) 

39 symbols.append(sym[0]) 

40 line = fileobj.readline() 

41 atoms = Atoms(symbols=symbols, cell=cell, positions=positions) 

42 return atoms 

43 

44 

45def write_sys(fileobj, atoms): 

46 """ 

47 Function to write a sys file. 

48 

49 fileobj: file object 

50 File to which output is written. 

51 atoms: Atoms object 

52 Atoms object specifying the atomic configuration. 

53 """ 

54 fileobj.write('set cell') 

55 for i in range(3): 

56 d = atoms.cell[i] / Bohr 

57 fileobj.write((' {:6f} {:6f} {:6f}').format(*d)) 

58 fileobj.write(' bohr\n') 

59 

60 ch_sym = atoms.get_chemical_symbols() 

61 atm_nm = atoms.numbers 

62 a_pos = atoms.positions 

63 an = list(set(atm_nm)) 

64 

65 for i, s in enumerate(set(ch_sym)): 

66 fileobj.write(('species {}{} {}.xml \n').format(s, an[i], s)) 

67 for i, (S, Z, (x, y, z)) in enumerate(zip(ch_sym, atm_nm, a_pos)): 

68 fileobj.write( 

69 ( 

70 'atom {:5} {:5} {:12.6f} {:12.6f} {:12.6f}\ 

71 bohr\n' 

72 ).format(S + str(i + 1), S + str(Z), x / Bohr, y / Bohr, z / Bohr) 

73 )