-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmod_dymat.py
468 lines (353 loc) · 13.3 KB
/
mod_dymat.py
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
#!/usr/bin/env python
#
# mod_dymat.py
#
# module for generating dynamical matrix to calculate phonon
# transmission function by using NEGF method.
#
# Copyright (c) 2018 Yuto Tanaka
#
import math
import cmath
import numpy as np
import numpy.linalg as LA
def reciprocal(lavec):
revec = np.zeros([3, 3])
vol = np.dot(lavec[0], (np.cross(lavec[1], lavec[2])))
revec[0] = np.cross(lavec[1], lavec[2])
revec[1] = np.cross(lavec[2], lavec[0])
revec[2] = np.cross(lavec[0], lavec[1])
revec *= 2 * math.pi / vol
return revec
def store_vec(factor, vec, vec_count, ss):
if factor == 0:
factor = float(ss[0])
else:
for i in range(3):
vec[vec_count][i] = factor * float(ss[i])
vec_count += 1
return factor, vec, vec_count
def validation(tran_direct, kpoint):
val = True
var_idx = [i for i, x in enumerate(tran_direct) if x == 0]
fix_idx = [i for i, x in enumerate(tran_direct) if x == 1]
if len(var_idx) != 2 or len(fix_idx) != 1:
print("Transport direction is not selected properly.")
print("Please check direction field in negf input file.")
val = False
kp_wrong = [i for i, x in enumerate(kpoint) if x < 0]
n = len(kp_wrong)
if n > 0:
print("k point must be positive integer.")
print("Please check kpoint field in negf input file.")
val = False
if not val:
exit(1)
def read_negf(negf_file):
target = ['nat', 'nkd', 'mass', '&cell', '&unit_cell',
'&direction', '&kpoint', '&position', '&cutoff']
negf_target = ['imag_delta', 'freq_max', 'criterion', 'freq_div']
lavec_frag = 0
univec_frag = 0
pos_frag = 0
direct_frag = 0
kpoint_frag = 0
cutoff_frag = 0
lavec = np.zeros([3, 3]) # supercell lattice vector (initialize)
univec = np.zeros([3, 3]) # unit vector (initialize)
# default NEGF parameters
imag_delta = 1e-6
criterion = 1e-6
freq_max = 1000
step = 100
f_negf = open(negf_file, 'r') # open output file
for line in f_negf:
ss = line.strip().split()
len_ss = len(ss)
if len_ss == 0: # empty line
continue
else:
# supercell lattice vector
if lavec_frag == 1:
factor, lavec, vec_count = store_vec(
factor, lavec, vec_count, ss)
if vec_count == 3:
lavec_frag = 0
conv = LA.inv(LA.inv(lavec).T) # unit converter
# unit cell vector and reciprocal vector
elif univec_frag == 1:
factor, univec, vec_count = store_vec(
factor, univec, vec_count, ss)
if vec_count == 3:
univec_frag = 0
revec = reciprocal(univec) # reciprocal vector
# transport direction
elif direct_frag == 1:
tran_direct = [int(ss[0]), int(ss[1]), int(ss[2])]
direct_frag = 0
# kpoint
elif kpoint_frag == 1:
if kp_mode < 0:
kp_mode = int(ss[0])
if kp_mode != 3:
print(
'If you calculate dynamical matrix, KPMODE should be 3.')
exit(1)
else:
kpoint = [int(ss[0]), int(ss[1]), int(ss[2])]
kpoint_frag = 0
# coordinates (bohr unit)
elif pos_frag == 1:
k_atom[atom_count] = int(ss[0])
for i in range(3):
x_bohr[atom_count][i] = float(ss[i+1])
atom_count += 1
if atom_count == nat:
pos_frag = 0
# cutoff radius
elif cutoff_frag == 1:
cutoff = ss[1]
cutoff_frag = 0
# search target and build frag
if ss[0].lower() == target[0]:
nat = int(ss[2]) # number of atom
atom_count = 0
# kind of atom (initialize)
k_atom = np.zeros([nat], dtype=np.int64)
x_bohr = np.zeros([nat, 3]) # atomic coordinate (initialize)
elif ss[0].lower() == target[1]:
nkd = int(ss[2]) # number of kind of atom
mass = np.zeros([nkd]) # atomic mass (initialize)
elif ss[0].lower() == target[2]:
for i in range(nkd):
mass[i] = ss[i+2] # atomic mass
elif ss[0].lower() == target[3]:
factor = 0
vec_count = 0
lavec_frag = 1
elif ss[0].lower() == target[4]:
factor = 0
vec_count = 0
univec_frag = 1
elif ss[0].lower() == target[5]:
direct_frag = 1
tran_direct = []
elif ss[0].lower() == target[6]:
kpoint_frag = 1
kpoint = []
kp_mode = -1
elif ss[0].lower() == target[7]:
pos_frag = 1
elif ss[0].lower() == target[8]:
cutoff_frag = 1
# NEGF options
elif ss[0].lower() == negf_target[0]:
imag_delta = float(ss[2])
elif ss[0].lower() == negf_target[1]:
freq_max = float(ss[2])
elif ss[0].lower() == negf_target[2]:
criterion = float(ss[2])
elif ss[0].lower() == negf_target[3]:
step = int(ss[2])
f_negf.close()
# convert coordinate unit frac to bohr
for i in range(nat):
x_bohr[i] = np.dot(conv, x_bohr[i])
# set cutoff radius
if cutoff.lower() == 'none':
cutoff = univec[0][0]
else:
cutoff = float(cutoff)
validation(tran_direct, kpoint)
return x_bohr, k_atom, nat, mass, lavec, univec, revec, \
tran_direct, kpoint, cutoff, imag_delta, freq_max, \
criterion, step
def supercell(lavec, univec): # [l, m, n] supercell
lmn = [int(lavec[i][i] / univec[i][i]) for i in range(3)]
return lmn
def make_shift_list(lmn):
shift_max = [1, 1, 1] # shift_max = [max_x, max_y, max_z]
for i in range(3):
if lmn[i] > 3:
shift_max[i] = int(lmn[i] * 0.5)
global xrng_u, yrng_u, zrng_u
global lavec_shift, univec_shift
def make_rng(m):
rng_l = [i for i in range(-m, 1)]
rng_u = [i for i in range(-m, m+1)]
return rng_l, rng_u
xrng_l, xrng_u = make_rng(shift_max[0])
yrng_l, yrng_u = make_rng(shift_max[1])
zrng_l, zrng_u = make_rng(shift_max[2])
lavec_shift = [[i, j, k] for i in xrng_l for j in yrng_l for k in zrng_l]
univec_shift = [[i, j, k] for i in xrng_u for j in yrng_u for k in zrng_u]
def in_unitcell(x, vec):
d = 1e-7
return 0 <= x[0] < vec[0]-d and 0 <= x[1] < vec[1]-d and 0 <= x[2] < vec[2]-d
def calc_shift(shift, vec):
return shift[0] * vec[0] + shift[1] * vec[1] + shift[2] * vec[2]
def atom_in_unitcell(x_bohr, univec, nat):
unit_len = np.sum(univec, axis=0)
atom_uc = [i + 1 for i in range(nat) if in_unitcell(x_bohr[i], unit_len)]
return atom_uc
def calc_distance(x_bohr, lavec, p, q, num_shift):
distance = []
for i in range(num_shift):
shift = calc_shift(lavec_shift[i], lavec)
dist = LA.norm(x_bohr[p-1] - (x_bohr[q-1] + shift))
distance.append(dist)
return distance
def check_symmetry(lavec, univec, x_bohr, p, q, cutoff):
symmetry = []
unit_len = np.sum(univec, axis=0)
num_shift = len(lavec_shift)
distance = calc_distance(x_bohr, lavec, p, q, num_shift)
if min(distance) <= cutoff:
index = [i for i, x in enumerate(
distance) if abs(x - min(distance)) < 1e-5]
num_shift = len(univec_shift)
for i in index:
x_la = x_bohr[q-1] + calc_shift(lavec_shift[i], lavec)
for j in range(num_shift):
x_uni = x_la - calc_shift(univec_shift[j], univec)
if in_unitcell(x_uni, unit_len):
symmetry.append(j)
break
return symmetry
def generate_pairs(atom_uc, x_bohr, lavec, univec, nat, cutoff):
pairs = {}
for p in atom_uc:
pairs[p] = []
for q in range(p, nat + 1):
sym = check_symmetry(lavec, univec, x_bohr, p, q, cutoff)
pairs[p].append([q, sym])
f_log = open("pairs.log", "w")
f_log.write("atom number in the unit cell\n")
f_log.write(str(atom_uc) + "\n\n")
f_log.write("univec_shift R = l*a + m*b + n*c\n")
num_shift = len(univec_shift)
for i in range(num_shift):
f_log.write(str(i+1) + " " + str(univec_shift[i]) + "\n")
f_log.write("\n")
for p in atom_uc:
f_log.write("symmetry pair of atom %d\n" % (p))
for q in range(len(pairs[p])):
f_log.write(str(pairs[p][q]) + "\n")
f_log.write("\n")
f_log.close()
return pairs
def mass_in_unitcell(mass, k_atom, atom_uc):
mass_uc = [mass[k_atom[i-1] - 1] for i in atom_uc]
return mass_uc
def mapping(x_bohr, univec, atom_uc, nat, lmn):
map_uc = []
unit_len = np.sum(univec, axis=0)
for x in range(nat):
break_frag = False
for i in range(lmn[0]):
for j in range(lmn[1]):
for k in range(lmn[2]):
x_uni = x_bohr[x] - calc_shift([i, j, k], univec)
if in_unitcell(x_uni, unit_len):
break_frag = True
break # break for loop k
# break for loop j
if break_frag:
break
# break for loop i
if break_frag:
break
for p in atom_uc:
r = LA.norm(x_uni - x_bohr[p-1])
if r < 1e-3:
map_uc.append(p)
break
if len(map_uc) != x + 1:
print("mapping atom error.")
print("Please check NAT, &cell, and &unit_cell fields in negf file.")
exit(1)
return map_uc
def store_all_fcs(hessian_file, atom_uc, nat_uc, pairs, map_uc, mass_uc):
fcs = np.zeros([len(xrng_u), len(yrng_u), len(zrng_u),
3*nat_uc, 3*nat_uc], dtype=np.complex128)
num_uniq_pair = {i: len(pairs[i]) for i in pairs.keys()}
f_in = open(hessian_file, 'r')
label = f_in.readline() # Do not comment out
for line in f_in:
ss = line.strip().split()
atom1 = int(ss[0])
xyz1 = int(ss[1])
atom2 = int(ss[2])
xyz2 = int(ss[3])
fc2 = float(ss[4])
if fc2 == 0.0 or atom1 not in atom_uc or atom1 > atom2:
continue
else:
idx1_uc = atom_uc.index(map_uc[atom1 - 1])
idx2_uc = atom_uc.index(map_uc[atom2 - 1])
idx1 = idx1_uc + (xyz1 - 1) * nat_uc
idx2 = idx2_uc + (xyz2 - 1) * nat_uc
for i in range(num_uniq_pair[atom1]):
if pairs[atom1][i][0] == atom2:
p_idx = i
break
if pairs[atom1][p_idx][1] != []:
dev = len(pairs[atom1][p_idx][1]) * \
math.sqrt(mass_uc[idx1_uc] * mass_uc[idx2_uc])
fc2 /= dev
else:
continue
for i in pairs[atom1][p_idx][1]:
idx_x = univec_shift[i][0]
idx_y = univec_shift[i][1]
idx_z = univec_shift[i][2]
fcs[+idx_x][+idx_y][+idx_z][idx1][idx2] = fc2
fcs[-idx_x][-idx_y][-idx_z][idx2][idx1] = fc2
f_in.close()
return fcs
def generate_dynamical_matrix(fcs, q, nat, univec, tran_direct):
N = 3 * nat
dymat = np.zeros([3, N, N], dtype=np.complex128)
# initial values
D_c = np.zeros([3*N, 3*N], dtype=np.complex128)
D_s = np.zeros([2*N, 2*N], dtype=np.complex128)
D_cl = np.zeros([3*N, N], dtype=np.complex128)
D_cr = np.zeros([3*N, N], dtype=np.complex128)
# transport along x direction
if tran_direct[0] == 1:
for k in xrng_u:
for l in yrng_u:
for m in zrng_u:
r_vec = l * univec[1] + m * univec[2]
theta = np.dot(q, r_vec)
dymat[k] += fcs[k][l][m] * cmath.exp(theta * 1j)
# transport along y direction
elif tran_direct[1] == 1:
for l in yrng_u:
for m in zrng_u:
for k in xrng_u:
r_vec = m * univec[2] + k * univec[0]
theta = np.dot(q, r_vec)
dymat[l] += fcs[k][l][m] * cmath.exp(theta * 1j)
# transport along z direction
elif tran_direct[2] == 1:
for m in zrng_u:
for k in xrng_u:
for l in yrng_u:
r_vec = k * univec[0] + l * univec[1]
theta = np.dot(q, r_vec)
dymat[m] += fcs[k][l][m] * cmath.exp(theta * 1j)
else:
print('Transport direction is not selected.')
exit(1)
for i in range(3):
index = i * N
D_c[index:index + N, index:index + N] = dymat[0]
D_c[:N, N:2*N] = np.conjugate(dymat[-1].T)
D_c[N:2*N, :N] = dymat[-1]
D_c[N:2*N, 2*N:] = dymat[+1]
D_c[2*N:, N:2*N] = np.conjugate(dymat[+1].T)
D_s = D_c[:2*N, :2*N]
D_cl[:N, :] = dymat[-1]
D_cr[2*N:, :] = dymat[1]
return D_c, D_s, D_cl, D_cr