Coverage for ase/io/crystal.py: 78.79%
66 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1from ase.atoms import Atoms
2from ase.utils import reader, writer
5@writer
6def write_crystal(fd, atoms):
7 """Method to write atom structure in crystal format
8 (fort.34 format)
9 """
11 ispbc = atoms.get_pbc()
12 box = atoms.get_cell()
14 # here it is assumed that the non-periodic direction are z
15 # in 2D case, z and y in the 1D case.
17 if ispbc[2]:
18 fd.write(
19 '%2s %2s %2s %23s \n' % ('3', '1', '1', 'E -0.0E+0 DE 0.0E+0( 1)')
20 )
21 elif ispbc[1]:
22 fd.write(
23 '%2s %2s %2s %23s \n' % ('2', '1', '1', 'E -0.0E+0 DE 0.0E+0( 1)')
24 )
25 box[2, 2] = 500.0
26 elif ispbc[0]:
27 fd.write(
28 '%2s %2s %2s %23s \n' % ('1', '1', '1', 'E -0.0E+0 DE 0.0E+0( 1)')
29 )
30 box[2, 2] = 500.0
31 box[1, 1] = 500.0
32 else:
33 fd.write(
34 '%2s %2s %2s %23s \n' % ('0', '1', '1', 'E -0.0E+0 DE 0.0E+0( 1)')
35 )
36 box[2, 2] = 500.0
37 box[1, 1] = 500.0
38 box[0, 0] = 500.0
40 # write box
41 # crystal dummy
42 fd.write(' %.17E %.17E %.17E \n' % (box[0][0], box[0][1], box[0][2]))
43 fd.write(' %.17E %.17E %.17E \n' % (box[1][0], box[1][1], box[1][2]))
44 fd.write(' %.17E %.17E %.17E \n' % (box[2][0], box[2][1], box[2][2]))
46 # write symmetry operations (not implemented yet for
47 # higher symmetries than C1)
48 fd.write(' %2s \n' % (1))
49 fd.write(f' {1:.17E} {0:.17E} {0:.17E} \n')
50 fd.write(f' {0:.17E} {1:.17E} {0:.17E} \n')
51 fd.write(f' {0:.17E} {0:.17E} {1:.17E} \n')
52 fd.write(f' {0:.17E} {0:.17E} {0:.17E} \n')
54 # write coordinates
55 fd.write(' %8s \n' % (len(atoms)))
56 coords = atoms.get_positions()
57 tags = atoms.get_tags()
58 atomnum = atoms.get_atomic_numbers()
59 for iatom, coord in enumerate(coords):
60 fd.write(
61 '%5i %19.16f %19.16f %19.16f \n'
62 % (
63 atomnum[iatom] + tags[iatom],
64 coords[iatom][0],
65 coords[iatom][1],
66 coords[iatom][2],
67 )
68 )
71@reader
72def read_crystal(fd):
73 """Method to read coordinates form 'fort.34' files
74 additionally read information about
75 periodic boundary condition
76 """
77 lines = fd.readlines()
79 atoms_pos = []
80 anumber_list = []
81 my_pbc = [False, False, False]
82 mycell = []
84 if float(lines[4]) != 1:
85 raise ValueError('High symmetry geometry is not allowed.')
87 if float(lines[1].split()[0]) < 500.0:
88 cell = [float(c) for c in lines[1].split()]
89 mycell.append(cell)
90 my_pbc[0] = True
91 else:
92 mycell.append([1, 0, 0])
94 if float(lines[2].split()[1]) < 500.0:
95 cell = [float(c) for c in lines[2].split()]
96 mycell.append(cell)
97 my_pbc[1] = True
98 else:
99 mycell.append([0, 1, 0])
101 if float(lines[3].split()[2]) < 500.0:
102 cell = [float(c) for c in lines[3].split()]
103 mycell.append(cell)
104 my_pbc[2] = True
105 else:
106 mycell.append([0, 0, 1])
108 natoms = int(lines[9].split()[0])
109 for i in range(natoms):
110 index = 10 + i
111 anum = int(lines[index].split()[0]) % 100
112 anumber_list.append(anum)
114 position = [float(p) for p in lines[index].split()[1:]]
115 atoms_pos.append(position)
117 atoms = Atoms(
118 positions=atoms_pos, numbers=anumber_list, cell=mycell, pbc=my_pbc
119 )
121 return atoms