Repository navigation
Expand file tree
/
Copy pathfcsxml.py
More file actions
477 lines (403 loc) · 17.6 KB
/
Copy pathfcsxml.py
File metadata and controls
477 lines (403 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
"""Python module for reading and writing alamode FCSXML."""
import itertools
import warnings
from xml.etree.ElementTree import Element, ElementTree, SubElement
import numpy as np
import spglib
from ._elements import ELEMENT_SYMBOLS as atom_names
# Bohr radius in Angstrom (CODATA 2018); keep equal to Bohr_in_Angstrom in
# include/constants.h (checked by tools/tests/test_constants.py).
_BOHR = 0.529177210903
class Fcsxml:
"""Writer of harmonic and anharmonic interatomic force constants for alamode."""
def __init__(self, lavec, xcoord, numbers, symprec=1.0e-3, unit="angstrom"):
"""Initialize the object with the input crystal structure.
Parameters
----------
lavec : array_like
Basis vectors in units specified by 'unit'
a, b, c are given as column vectors.
shape=(3, 3), dtype='double'
xcoord : array_like
Fractional coordinates of atomic points.
shape=(num_atoms, 3), dtype='double'
numbers : array_like
Atomic numbers.
shape=(num_atoms,), dtype='intc'
symprec: float
The tolerance used for space group detection.
Will be passed to spglib.
unit: str
Length unit of lavec: 'angstrom' (default) or 'bohr'.
"""
if unit == "angstrom":
self._lavec = np.array(lavec).transpose() / _BOHR
elif unit == "bohr":
self._lavec = np.array(lavec).transpose()
else:
raise ValueError(f"unit must be 'angstrom' or 'bohr', got {unit!r}.")
self._xf = np.array(xcoord)
self._atomic_kinds = np.array(numbers)
self._symprec = symprec
self._generated_spacegroup = False
self._xf_image = None
self._xc_image = None
self._map_p2s = None
self._map_s2p = None
self._nat_super = len(xcoord)
self._nat_prim = None
self._set_fcs = [False, False, False]
self._fc2_info = None
self._fc3_info = None
self._fc4_info = None
self._closest_mirror_images = None
self._set_x_images()
self._compute_closest_mirror_images()
self._setup_symmetry()
def set_force_constants(self, fc_values, fc_indices):
"""Set the force constants to write.
Parameters
----------
fc_values: array_like, dtype='float', shape=(num_fc)
Array of force constant values to write in an alamode XML file.
The unit of fc_values should be Rydberg/bohr**fc_order.
fc_indices: array_like, dtype='int', shape=(num_fc, fc_order + 1)
Array of flattened indices 3 * index_atom + index_xyz of the
force constants.
Note
----
The expected input is all force constants in the supercell where
the first column of fc_indices corresponds to the atoms in the
primitive cell at the center. The `index_atom` of the atoms in the
center primitive cell is determined from the output of spglib method,
which can be obtained by the `get_atoms_in_primitive` method.
The order of force constant is inferred from ``fc_indices.shape[1]``:
2 columns = harmonic (FC2), 3 = cubic (FC3), 4 = quartic (FC4).
(This differs from ``ALM.get_fc``, where ``fc_order=1`` is harmonic.)
"""
fc_values = np.asarray(fc_values)
fc_indices = np.asarray(fc_indices)
if fc_indices.ndim != 2:
raise ValueError("fc_indices must be a 2-D array.")
if len(fc_indices) != len(fc_values):
raise ValueError("fc_values and fc_indices must have the same length.")
fc_order = fc_indices.shape[1]
(
fc_values_trim,
fc_indices_trim,
) = self._trim_and_sort_input_force_constants(fc_values, fc_indices)
if fc_order == 2:
self._fc2_info = [fc_values_trim, fc_indices_trim]
elif fc_order == 3:
self._fc3_info = [fc_values_trim, fc_indices_trim]
elif fc_order == 4:
self._fc4_info = [fc_values_trim, fc_indices_trim]
else:
raise ValueError("The anharmonic terms beyond 4th-order cannot be saved.")
def write(self, filename="alamodefc.xml"):
"""Write the input force constants as a given filename.
Parameters
----------
filename : str
The filename where the force constants are saved.
default is "alamodefc.xml"
"""
if self._fc2_info is None:
warnings.warn(
"The harmonic force constants are not set. "
"Please use `set_force_constants` before calling write.",
stacklevel=2,
)
root = Element("Data")
self._add_header_xml(root)
self._add_structure_xml(root)
self._add_symmetry_xml(root)
self._add_force_constants_xml(root)
self._pretty_print(root)
tree = ElementTree(root)
with open(filename, "wb") as file:
tree.write(file, encoding="utf-8")
def get_atoms_in_primitive(self):
"""Return the indices of atoms in the primitive cell.
Returns
-------
atoms_in_primitive: array_like, dtype='int',
shape=(number_of_atoms_in_primitive)
"""
if self._map_p2s is None:
self._setup_symmetry()
return self._map_p2s[:, 0]
def _add_header_xml(self, root_in):
elem = Element("ALM_version")
elem.text = "None"
root_in.append(elem)
def _add_structure_xml(self, root_in):
elem = Element("Structure")
root_in.append(elem)
subelem = SubElement(elem, "NumberOfAtoms")
subelem.text = str(self._nat_super)
atomic_kinds_unique = self._atomic_kinds[
sorted(np.unique(self._atomic_kinds, return_index=True)[1])
]
nkd = len(atomic_kinds_unique)
subelem = SubElement(elem, "NumberOfElements")
subelem.text = str(nkd)
subelem = SubElement(elem, "AtomicElements")
for i in range(nkd):
subsubelem = SubElement(subelem, "element")
subsubelem.set("number", str(i + 1))
subsubelem.text = atom_names[atomic_kinds_unique[i]]
subelem = SubElement(elem, "LatticeVector")
for i in range(3):
subsubelem = SubElement(subelem, "a%d" % (i + 1))
str_avec = ""
for j in range(3):
str_avec += " " + "%.15e" % self._lavec[j][i]
subsubelem.text = str_avec
subelem = SubElement(elem, "Periodicity")
subelem.text = "1 1 1"
subelem = SubElement(elem, "Position")
for i in range(self._nat_super):
subsubelem = SubElement(subelem, "pos")
subsubelem.set("index", str(i + 1))
subsubelem.set("element", atom_names[self._atomic_kinds[i]])
str_pos = ""
for j in range(3):
str_pos += " " + "%.15e" % self._xf[i][j]
subsubelem.text = str_pos
def _add_symmetry_xml(self, root_in):
if self._map_p2s is None:
self._setup_symmetry()
elem = Element("Symmetry")
root_in.append(elem)
subelem = SubElement(elem, "NumberOfTranslations")
ntrans = len(self._map_p2s[0, :])
subelem.text = str(ntrans)
subelem = SubElement(elem, "Translations")
for i in range(ntrans):
for j in range(self._nat_prim):
subsubelem = SubElement(subelem, "map")
subsubelem.set("tran", str(i + 1))
subsubelem.set("atom", str(j + 1))
subsubelem.text = str(self._map_p2s[j, i] + 1)
def _add_force_constants_xml(self, root_in):
elem = Element("ForceConstants")
root_in.append(elem)
if self._fc2_info:
subelem = SubElement(elem, "HARMONIC")
fcvals = self._fc2_info[0]
clusters = self._fc2_info[1]
for fcval, cluster in zip(fcvals, clusters):
list_mir_imgs = self._closest_mirror_images[cluster[0] // 3][
cluster[1] // 3
][0]
num_mir_img = len(list_mir_imgs)
fcval_scale = fcval / float(num_mir_img)
for mir_img in list_mir_imgs:
subsubelem = SubElement(subelem, "FC2")
subsubelem.set(
"pair1",
str(self._map_s2p[cluster[0] // 3]["atom_num"][0] + 1)
+ " "
+ str(cluster[0] % 3 + 1),
)
subsubelem.set(
"pair2",
str(cluster[1] // 3 + 1)
+ " "
+ str(cluster[1] % 3 + 1)
+ " "
+ str(mir_img + 1),
)
subsubelem.text = "%.15e" % fcval_scale
if self._fc3_info:
self._add_anharmonic_fcs_to_treeelement(
elem, self._fc3_info[1], self._fc3_info[0]
)
if self._fc4_info:
self._add_anharmonic_fcs_to_treeelement(
elem, self._fc4_info[1], self._fc4_info[0]
)
def _add_anharmonic_fcs_to_treeelement(self, elem_in, clusters, fcvals):
fc_order = len(clusters[0])
subelem = SubElement(elem_in, "ANHARM%d" % fc_order)
subsubelem_key = "FC%d" % fc_order
for fcval, cluster in zip(fcvals, clusters):
if not self._is_sorted(cluster[1:]):
continue
atoms = cluster // 3
xyz = cluster % 3 + 1
mir_img_combs = self._get_mirror_image_combinations(atoms)
num_mir_img = len(mir_img_combs)
fcval_scale = fcval / float(num_mir_img)
for mir_img in mir_img_combs:
subsubelem = SubElement(subelem, subsubelem_key)
subsubelem.set(
"pair1",
str(self._map_s2p[atoms[0]]["atom_num"][0] + 1) + " " + str(xyz[0]),
)
for i in range(1, fc_order):
subsubelem.set(
"pair%d" % (i + 1),
str(atoms[i] + 1)
+ " "
+ str(xyz[i])
+ " "
+ str(mir_img[i - 1] + 1),
)
subsubelem.text = "%.15e" % fcval_scale
def _get_mirror_image_combinations(self, atoms):
unique_atoms = []
indices_unique_counts = []
iuniq = -1
for atom in atoms[1:]:
if atom not in unique_atoms:
unique_atoms.append(atom)
iuniq += 1
indices_unique_counts.append(iuniq)
mirror_image_combinations = []
if len(unique_atoms) == 1:
all_mir_img_comb = self._closest_mirror_images[atoms[0]][unique_atoms[0]][0]
for icomb in range(len(all_mir_img_comb)):
tmp_list = []
for _i in indices_unique_counts:
tmp_list.append(all_mir_img_comb[icomb])
mirror_image_combinations.append(tmp_list)
elif len(unique_atoms) == 2:
all_mir_img_comb = list(
itertools.product(
self._closest_mirror_images[atoms[0]][unique_atoms[0]][0],
self._closest_mirror_images[atoms[0]][unique_atoms[1]][0],
)
)
for icomb in range(len(all_mir_img_comb)):
tmp_list = []
for i in indices_unique_counts:
tmp_list.append(all_mir_img_comb[icomb][i])
mirror_image_combinations.append(tmp_list)
elif len(unique_atoms) == 3:
all_mir_img_comb = list(
itertools.product(
self._closest_mirror_images[atoms[0]][unique_atoms[0]][0],
self._closest_mirror_images[atoms[0]][unique_atoms[1]][0],
self._closest_mirror_images[atoms[0]][unique_atoms[2]][0],
)
)
for icomb in range(len(all_mir_img_comb)):
tmp_list = []
for i in indices_unique_counts:
tmp_list.append(all_mir_img_comb[icomb][i])
mirror_image_combinations.append(tmp_list)
return mirror_image_combinations
def _pretty_print(self, current, parent=None, index=-1, depth=0):
for i, node in enumerate(current):
self._pretty_print(node, current, i, depth + 1)
if parent is not None:
if index == 0:
parent.text = "\n" + (" " * depth)
else:
parent[index - 1].tail = "\n" + (" " * depth)
if index == len(parent) - 1:
current.tail = "\n" + (" " * (depth - 1))
def _set_x_images(self):
self._xf_image = np.zeros((27, self._nat_super, 3), dtype="float")
self._xf_image[0] = self._xf
ishifts = [-1, 0, 1]
icell = 0
for ix, iy, iz in itertools.product(ishifts, ishifts, ishifts):
if ix == 0 and iy == 0 and iz == 0:
continue
icell += 1
self._xf_image[icell, :, 0] = self._xf[:, 0] + float(ix)
self._xf_image[icell, :, 1] = self._xf[:, 1] + float(iy)
self._xf_image[icell, :, 2] = self._xf[:, 2] + float(iz)
self._xc_image = np.dot(self._xf_image, self._lavec.transpose())
def _compute_closest_mirror_images(self):
if self._xc_image is None:
self._set_x_images()
tolerance = 1.0e-3
self._closest_mirror_images = []
distances = np.zeros(27, dtype="float")
for i in range(self._nat_super):
mirror_image_tmp = []
for j in range(self._nat_super):
xdiff = self._xc_image[:, j, :] - self._xc_image[0, i, :]
for icell in range(27):
distances[icell] = np.sqrt(np.dot(xdiff[icell, :], xdiff[icell, :]))
index_sort = np.argsort(distances)
minimum_dist = distances[index_sort[0]]
mirror_image_tmp.append(
np.where(np.abs(distances[:] - minimum_dist) < tolerance)
)
self._closest_mirror_images.append(mirror_image_tmp)
def _setup_symmetry(self):
cell = (self._lavec.transpose(), self._xf, self._atomic_kinds)
dataset = spglib.get_symmetry_dataset(cell, symprec=self._symprec)
identity_matrix = np.identity(3)
try: # spglib >= 2.5 returns a dataclass; dict-style access is deprecated
rotations = dataset.rotations
translations = dataset.translations
mapping_to_primitive = dataset.mapping_to_primitive
except AttributeError:
rotations = dataset["rotations"]
translations = dataset["translations"]
mapping_to_primitive = dataset["mapping_to_primitive"]
nsym = len(rotations)
# Sort the list of symmetry operations in the lexicographical order
# and create symnum_translation
symmetry_ops = np.zeros((nsym, 12), dtype="float")
symmetry_ops[:, :9] = np.reshape(rotations, (nsym, 9))
symmetry_ops[:, 9:] = translations[:, :]
for isym in range(nsym):
for i in range(3):
if symmetry_ops[isym, 9 + i] < 0.0:
symmetry_ops[isym, 9 + i] += 1.0
index_sort = np.lexsort(np.rot90(symmetry_ops))
symnum_translation = []
for elem in index_sort:
if np.array_equal(rotations[elem], identity_matrix):
symnum_translation.append(elem)
self._nat_prim = self._nat_super // len(symnum_translation)
self._map_p2s = (
np.ones((self._nat_prim, len(symnum_translation)), dtype="int") * -1
)
unique_set, atom_in_primitive_at_origin = np.unique(
mapping_to_primitive, return_index=True
)
for index_p, index_s in enumerate(atom_in_primitive_at_origin):
for itran, isym in enumerate(symnum_translation):
xnew = self._xf[index_s, :] + translations[isym][:]
for iat in np.where(mapping_to_primitive == unique_set[index_p])[0]:
xdiff = xnew - self._xf[iat]
xdiff -= np.array(np.rint(xdiff), dtype="float")
if np.dot(xdiff, xdiff) < 1.0e-5:
self._map_p2s[index_p][itran] = iat
break
mytype = np.dtype(
[
("atom_num", int, (1,)),
("tran_num", int, (1,)),
],
align=True,
)
self._map_s2p = np.zeros(self._nat_super, dtype=mytype)
for iat in range(self._nat_prim):
for itran in range(len(symnum_translation)):
jat = self._map_p2s[iat][itran]
self._map_s2p[jat]["atom_num"] = iat
self._map_s2p[jat]["tran_num"] = itran
def _trim_and_sort_input_force_constants(self, fcvals_in, elems_in):
atom_in_primitive_at_origin = self._map_p2s[:, 0]
indices_to_search = []
for atom in atom_in_primitive_at_origin:
for i in range(3):
indices_to_search.append(3 * atom + i)
indices_to_search = np.array(indices_to_search)
loc_to_use = np.where(np.isin(elems_in[:, 0], indices_to_search))
fcvals_trim = fcvals_in[loc_to_use]
elems_trim = elems_in[loc_to_use]
index_sort = np.lexsort(np.rot90(elems_trim))
return fcvals_trim[index_sort], elems_trim[index_sort]
@staticmethod
def _is_sorted(a):
return np.all(a[:-1] <= a[1:])