Coverage for ase/io/wien2k.py: 67.83%
143 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
1import numpy as np
3from ase import Atoms
4from ase.units import Bohr, Ry
5from ase.utils import reader, writer
8def read_scf(filename):
9 try:
10 with open(filename + '.scf') as fd:
11 pip = fd.readlines()
12 ene = []
13 for line in pip:
14 if line[0:4] == ':ENE':
15 ene.append(float(line[43:59]) * Ry)
16 return ene
17 except Exception: # XXX Which kind of exception exactly?
18 return None
21@reader
22def read_struct(fd, ase=True):
23 pip = fd.readlines()
24 lattice = pip[1][0:3]
25 nat = int(pip[1][27:30])
26 cell = np.zeros(6)
27 for i in range(6):
28 cell[i] = float(pip[3][0 + i * 10 : 10 + i * 10])
29 cell[0:3] = cell[0:3] * Bohr
30 if lattice == 'P ':
31 lattice = 'P'
32 elif lattice == 'H ':
33 lattice = 'P'
34 cell[3:6] = [90.0, 90.0, 120.0]
35 elif lattice == 'R ':
36 lattice = 'R'
37 elif lattice == 'F ':
38 lattice = 'F'
39 elif lattice == 'B ':
40 lattice = 'I'
41 elif lattice == 'CXY':
42 lattice = 'C'
43 elif lattice == 'CXZ':
44 lattice = 'B'
45 elif lattice == 'CYZ':
46 lattice = 'A'
47 else:
48 raise RuntimeError('TEST needed')
49 pos = np.array([])
50 atomtype = []
51 rmt = []
52 neq = np.zeros(nat)
53 iline = 4
54 indif = 0
55 for iat in range(nat):
56 indifini = indif
57 if len(pos) == 0:
58 pos = np.array(
59 [
60 [
61 float(pip[iline][12:22]),
62 float(pip[iline][25:35]),
63 float(pip[iline][38:48]),
64 ]
65 ]
66 )
67 else:
68 pos = np.append(
69 pos,
70 np.array(
71 [
72 [
73 float(pip[iline][12:22]),
74 float(pip[iline][25:35]),
75 float(pip[iline][38:48]),
76 ]
77 ]
78 ),
79 axis=0,
80 )
81 indif += 1
82 iline += 1
83 neq[iat] = int(pip[iline][15:17])
84 iline += 1
85 for _ in range(1, int(neq[iat])):
86 pos = np.append(
87 pos,
88 np.array(
89 [
90 [
91 float(pip[iline][12:22]),
92 float(pip[iline][25:35]),
93 float(pip[iline][38:48]),
94 ]
95 ]
96 ),
97 axis=0,
98 )
99 indif += 1
100 iline += 1
101 for _ in range(indif - indifini):
102 atomtype.append(pip[iline][0:2].replace(' ', ''))
103 rmt.append(float(pip[iline][43:48]))
104 iline += 4
105 if ase:
106 cell2 = coorsys(cell)
107 atoms = Atoms(atomtype, pos, pbc=True)
108 atoms.set_cell(cell2, scale_atoms=True)
109 cell2 = np.dot(c2p(lattice), cell2)
110 if lattice == 'R':
111 atoms.set_cell(cell2, scale_atoms=True)
112 else:
113 atoms.set_cell(cell2)
114 return atoms
115 else:
116 return cell, lattice, pos, atomtype, rmt
119@writer
120def write_struct(fd, atoms2=None, rmt=None, lattice='P', zza=None):
121 atoms = atoms2.copy()
122 atoms.wrap()
123 fd.write('ASE generated\n')
124 nat = len(atoms)
125 if rmt is None:
126 rmt = [2.0] * nat
127 fd.write(
128 lattice + ' LATTICE,NONEQUIV.ATOMS:%3i\nMODE OF CALC=RELA\n' % nat
129 )
130 cell = atoms.get_cell()
131 metT = np.dot(cell, np.transpose(cell))
132 cell2 = cellconst(metT)
133 cell2[0:3] = cell2[0:3] / Bohr
134 fd.write(('%10.6f' * 6) % tuple(cell2) + '\n')
135 if zza is None:
136 zza = atoms.get_atomic_numbers()
137 for ii in range(nat):
138 fd.write('ATOM %3i: ' % (ii + 1))
139 pos = atoms.get_scaled_positions()[ii]
140 fd.write('X=%10.8f Y=%10.8f Z=%10.8f\n' % tuple(pos))
141 fd.write(' MULT= 1 ISPLIT= 1\n')
142 zz = zza[ii]
143 if zz > 71:
144 ro = 0.000005
145 elif zz > 36:
146 ro = 0.00001
147 elif zz > 18:
148 ro = 0.00005
149 else:
150 ro = 0.0001
151 fd.write(
152 '%-10s NPT=%5i R0=%9.8f RMT=%10.4f Z:%10.5f\n'
153 % (atoms.get_chemical_symbols()[ii], 781, ro, rmt[ii], zz)
154 )
155 fd.write(f'LOCAL ROT MATRIX: {1.0:9.7f} {0.0:9.7f} {0.0:9.7f}\n')
156 fd.write(f' {0.0:9.7f} {1.0:9.7f} {0.0:9.7f}\n')
157 fd.write(f' {0.0:9.7f} {0.0:9.7f} {1.0:9.7f}\n')
158 fd.write(' 0\n')
161def cellconst(metT):
162 """metT=np.dot(cell,cell.T)"""
163 aa = np.sqrt(metT[0, 0])
164 bb = np.sqrt(metT[1, 1])
165 cc = np.sqrt(metT[2, 2])
166 gamma = np.arccos(metT[0, 1] / (aa * bb)) / np.pi * 180.0
167 beta = np.arccos(metT[0, 2] / (aa * cc)) / np.pi * 180.0
168 alpha = np.arccos(metT[1, 2] / (bb * cc)) / np.pi * 180.0
169 return np.array([aa, bb, cc, alpha, beta, gamma])
172def coorsys(latconst):
173 a = latconst[0]
174 b = latconst[1]
175 c = latconst[2]
176 cal = np.cos(latconst[3] * np.pi / 180.0)
177 cbe = np.cos(latconst[4] * np.pi / 180.0)
178 cga = np.cos(latconst[5] * np.pi / 180.0)
179 sga = np.sin(latconst[5] * np.pi / 180.0)
180 return np.array(
181 [
182 [a, b * cga, c * cbe],
183 [0, b * sga, c * (cal - cbe * cga) / sga],
184 [
185 0,
186 0,
187 c
188 * np.sqrt(1 - cal**2 - cbe**2 - cga**2 + 2 * cal * cbe * cga)
189 / sga,
190 ],
191 ]
192 ).transpose()
195def c2p(lattice):
196 """apply as eg. cell2 = np.dot(c2p('F'), cell)"""
197 if lattice == 'P':
198 cell = np.eye(3)
199 elif lattice == 'F':
200 cell = np.array([[0.0, 0.5, 0.5], [0.5, 0.0, 0.5], [0.5, 0.5, 0.0]])
201 elif lattice == 'I':
202 cell = np.array([[-0.5, 0.5, 0.5], [0.5, -0.5, 0.5], [0.5, 0.5, -0.5]])
203 elif lattice == 'C':
204 cell = np.array([[0.5, 0.5, 0.0], [0.5, -0.5, 0.0], [0.0, 0.0, -1.0]])
205 elif lattice == 'B':
206 cell = np.array([[0.5, 0.0, 0.5], [0.0, 1.0, 0.0], [0.5, 0.0, -0.5]])
207 elif lattice == 'A':
208 cell = np.array([[-1.0, 0.0, 0.0], [0.0, -0.5, 0.5], [0.0, 0.5, 0.5]])
209 elif lattice == 'R':
210 cell = np.array(
211 [
212 [2.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0],
213 [-1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0],
214 [-1.0 / 3.0, -2.0 / 3.0, 1.0 / 3.0],
215 ]
216 )
218 else:
219 raise ValueError('lattice is ' + lattice + '!')
220 return cell