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

38 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-10 11:04 +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, 

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 

44 

45 

46def write_sys(fileobj, atoms): 

47 """ 

48 Function to write a sys file. 

49 

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') 

60 

61 ch_sym = atoms.get_chemical_symbols() 

62 atm_nm = atoms.numbers 

63 a_pos = atoms.positions 

64 an = list(set(atm_nm)) 

65 

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 {:5} {:5} {:12.6f} {:12.6f} {:12.6f}\ 

70 bohr\n').format(S + str(i + 1), S + str(Z), x / Bohr, y / Bohr, 

71 z / Bohr))