修改代码,错误如下:File "structure_analysis4.py", line 33, in <module> indices1, indices2, distances = neighbor_list('ijD', pos1, cutoff, self_interaction=False, bothways=True) TypeError: neighbor_list() got an unexpected keyword argument 'bothways'。。from ase import io from ase.build import sort from ase.visualize import view from ase.neighborlist import neighbor_list import numpy as np from ase import Atoms # 加载两个POSCAR文件 pos1 = io.read('POSCAR1') pos2 = io.read('POSCAR2') # 指定原子种类 atom_type = 'C' # 获得第一个POSCAR中指定原子的位置列表 #indices1 = [i for i, atom in enumerate(pos1) if atom.symbol == atom_type] #positions1 = sort(pos1.get_positions()[indices1]) indices1 = [i for i, atom in enumerate(pos1) if atom.symbol == atom_type] positions1 = pos1.get_positions()[indices1] atoms1 = Atoms(symbols=[atom_type]*len(positions1), positions=positions1) sorted_atoms1 = sort(atoms1) # 获得第二个POSCAR中指定原子的位置列表 #indices2 = [i for i, atom in enumerate(pos2) if atom.symbol == atom_type] #positions2 = sort(pos2.get_positions()[indices2]) indices2 = [i for i, atom in enumerate(pos2) if atom.symbol == atom_type] positions2 = pos2.get_positions()[indices2] atoms2 = Atoms(symbols=[atom_type]*len(positions2), positions=positions2) sorted_atoms2 = sort(atoms2) # 计算两个位置列表之间的距离矩阵 cutoff = 5.0 # 距离截断半径 indices1, indices2, distances = neighbor_list('ijD', pos1, cutoff, self_interaction=False, bothways=True) dist_matrix1 = np.zeros((len(positions1), len(positions1))) for i, j, d in zip(indices1, indices2, distances): if i in indices1 and j in indices1: dist_matrix1[indices1.index(i), indices1.index(j)] = d indices1, indices2, distances = neighbor_list('ijD', pos2, cutoff, self_interaction=False, bothways=True) dist_matrix2 = np.zeros((len(positions2), len(positions2))) for i, j, d in zip(indices1, indices2, distances): if i in indices2 and j in indices2: dist_matrix2[indices2.index(i), indices2.index(j)] = d # 计算两个距离矩阵之间的相似性 similarity = 1.0 - np.abs(dist_matrix1 - dist_matrix2).sum() / dist_matrix1.size print('Structure similarity: ', similarity)
时间: 2023-12-22 21:04:33 浏览: 129
根错误提示,`neighbor_list()` 函数没有 `bothways` 参数。可以尝试把 `bothways=True` 这个参数删除掉。另外,第一个 `neighbor_list()` 函数的输入参数应该是 `positions1`,而不是 `pos1`。修改后的代码如下:
```python
from ase import io
from ase.build import sort
from ase.visualize import view
from ase.neighborlist import neighbor_list
import numpy as np
from ase import Atoms
# 加载两个POSCAR文件
pos1 = io.read('POSCAR1')
pos2 = io.read('POSCAR2')
# 指定原子种类
atom_type = 'C'
# 获得第一个POSCAR中指定原子的位置列表
indices1 = [i for i, atom in enumerate(pos1) if atom.symbol == atom_type]
positions1 = pos1.get_positions()[indices1]
atoms1 = Atoms(symbols=[atom_type]*len(positions1), positions=positions1)
sorted_atoms1 = sort(atoms1)
# 获得第二个POSCAR中指定原子的位置列表
indices2 = [i for i, atom in enumerate(pos2) if atom.symbol == atom_type]
positions2 = pos2.get_positions()[indices2]
atoms2 = Atoms(symbols=[atom_type]*len(positions2), positions=positions2)
sorted_atoms2 = sort(atoms2)
# 计算两个位置列表之间的距离矩阵
cutoff = 5.0 # 距离截断半径
indices1, indices2, distances = neighbor_list('ijD', positions1, cutoff, self_interaction=False)
dist_matrix1 = np.zeros((len(positions1), len(positions1)))
for i, j, d in zip(indices1, indices2, distances):
if i in indices1 and j in indices1:
dist_matrix1[indices1.index(i), indices1.index(j)] = d
indices1, indices2, distances = neighbor_list('ijD', positions2, cutoff, self_interaction=False)
dist_matrix2 = np.zeros((len(positions2), len(positions2)))
for i, j, d in zip(indices1, indices2, distances):
if i in indices2 and j in indices2:
dist_matrix2[indices2.index(i), indices2.index(j)] = d
# 计算两个距离矩阵之间的相似性
similarity = 1.0 - np.abs(dist_matrix1 - dist_matrix2).sum() / dist_matrix1.size
print('Structure similarity: ', similarity)
```
请注意,这里还有一个小问题:在计算相似性时,使用了两个距离矩阵的元素差的绝对值之和来计算距离矩阵的相似性,这种方法并不是很合适。可以考虑使用更为科学的相似性度量方法,比如结构指纹等。
阅读全文