-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-symmetry.py
123 lines (101 loc) · 4.03 KB
/
find-symmetry.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
import spglib #https://pypi.org/project/spglib/
import numpy as np
def read_poscar(filename):
with open(filename, 'r') as file:
lines = file.readlines()
# 读取晶胞参数
cell = np.zeros((3, 3))
for i in range(3):
cell[i] = [float(x) for x in lines[2 + i].split()]
# 读取原子类型和数量
species = lines[5].split()
num_atoms = [int(x) for x in lines[6].split()]
positions = []
start_line = 8 # 假设原子位置从第 9 行开始(索引为 8)
for i, num in enumerate(num_atoms):
for j in range(num):
pos = [float(x) for x in lines[start_line + sum(num_atoms[:i]) + j].split()]
positions.append(pos)
return cell, species, positions, num_atoms # 确保返回四个值
def get_symmetry(filename, tolerance=1e-5):
cell, species, positions, num_atoms = read_poscar(filename)
lattice = cell
positions = np.array(positions)
numbers = []
for i, num in enumerate(num_atoms):
numbers += [i + 1] * num
# 使用 spglib 获取对称性信息
#dataset = spglib.get_symmetry_dataset((lattice, positions, numbers))
dataset = spglib.get_symmetry_dataset((lattice, positions, numbers), symprec=tolerance)
# 提取额外的对称性信息
symmetry_info = {
'space_group_international': dataset['international'],
'space_group_hall': dataset['hall'],
'space_group_number': dataset['number'],
'point_group_type': dataset['pointgroup']
}
return symmetry_info
# 使用 POSCAR 文件
poscar_file = 'POSCAR' # 替换为你的 POSCAR 文件路径
tolerance = 1e-5 # 设置容差值,可以根据需要调整
symmetry_info = get_symmetry(poscar_file, tolerance)
# 打印对称性信息
print(f"Space Group Symbols (International): {symmetry_info['space_group_international']}")
print(f"Space Group Symbols (Hall): {symmetry_info['space_group_hall']}")
print(f"Space Group Number: {symmetry_info['space_group_number']}")
print(f"Point Group Type: {symmetry_info['point_group_type']}")
'''
# 打印 Wyckoff 位置
print("Wyckoff Positions:")
print(dataset['wyckoffs'])
# 打印等价原子
print("\nEquivalent Atoms:")
print(dataset['equivalent_atoms'])
# 打印对称操作的旋转部分
print("\nRotations:")
for i, rotation in enumerate(dataset['rotations']):
print(f"Rotation {i+1}:")
print(rotation)
# 打印对称操作的平移部分
print("\nTranslations:")
for i, translation in enumerate(dataset['translations']):
print(f"Translation {i+1}:")
print(translation)
'''
'''
#输出原胞和单胞的代码
import spglib
import numpy as np
def read_poscar(filename):
# [此处添加读取 POSCAR 文件的代码]
# 返回 lattice, positions, numbers
pass
def write_poscar(filename, lattice, positions, numbers, symbols):
with open(filename, 'w') as f:
f.write("Generated by spglib\n")
f.write("1.0\n")
for vec in lattice:
f.write(" {} {} {}\n".format(*vec))
f.write(" ".join(symbols) + "\n")
f.write(" ".join(map(str, numbers)) + "\n")
f.write("Direct\n")
for pos in positions:
f.write(" {} {} {}\n".format(*pos))
# 读取原始 POSCAR 文件
lattice, positions, numbers = read_poscar("POSCAR")
# 获取对称性信息
cell = (lattice, positions, numbers)
symmetry_dataset = spglib.get_symmetry_dataset(cell)
# 获取原胞
primitive_lattice, primitive_positions, primitive_numbers = spglib.find_primitive(cell)
primitive_symbols = [symbols[i - 1] for i in primitive_numbers]
# 获取单胞
conventional_cell = spglib.get_conventional_standard_structure(cell)
conventional_lattice = conventional_cell[0]
conventional_positions = conventional_cell[1]
conventional_numbers = conventional_cell[2]
conventional_symbols = [symbols[i - 1] for i in conventional_numbers]
# 输出 PPOSCAR 和 BPOSCAR
write_poscar("PPOSCAR", primitive_lattice, primitive_positions, primitive_numbers, primitive_symbols)
write_poscar("BPOSCAR", conventional_lattice, conventional_positions, conventional_numbers, conventional_symbols)
'''