[英]Get graph from a structure with pymatgen
給定一個 cif 文件,我想獲得某種材料的圖形表示(作為數據結構)。 我正在嘗試使用代表 CrN 晶胞的cif 文件。
我正在嘗試使用 pymatgen 的 StructureGraph 類,但我遇到了一些問題。 在此鏈接中,他們建議使用with_local_env_strategy()
方法,但是當我嘗試使用它時出現錯誤。 這是我的代碼:
from pymatgen.analysis.graphs import StructureGraph
from pymatgen.analysis.local_env import NearNeighbors
from pymatgen.core import Structure
filename = 'CrN.cif'
structure = Structure.from_file(filename)
nn = NearNeighbors()
strategy = nn.get_all_nn_info(structure)
strucGraph = StructureGraph.with_local_env_strategy(supercell, strategy, weights=False, edge_properties=False)
錯誤:
由於我不是材料學科的專家(我只是一名系統工程師和一名數學家),我提出了這兩種可能的解決方案:
使用get_neighbors()
方法,給定一個給定半徑的球形鄰域,獲得最近的鄰居:
from pymatgen.core import Structure
import networkx as nx
import numpy as np
filename = 'CrN.cif'
structure = Structure.from_file(filename)
supercell = structure.copy()
supercell.make_supercell([2,2,2])
G = nx.Graph()
for i, site in enumerate(supercell):
G.add_node(i)
G.nodes[i]["Species"] = label_set[site.species]
G.nodes[i]["position"] = (site.x, site.y, site.z)
for i, site in enumerate(supercell):
neighbors = [(n.index, n.nn_distance) for n in supercell.get_neighbors(site, r=3)]
for n in neighbors:
G.add_edge(i, n[0], weight=n[1])
第二種方法更可定制,我放在這里的代碼考慮了歐氏距離,但是,對於 2 個原子連接的標准,可以使用其他標准,例如能量。
def space_euclidean_distance(p1, p2):
dist = np.sqrt(np.sum((p1-p2)**2, axis=0))
return dist
lattice = supercell.lattice
fractional_coords = supercell.frac_coords
# Convert the fractional coordinates to Cartesian coordinates using the lattice vectors
cartesian_coords = lattice.get_cartesian_coords(fractional_coords)
distances = []
N = len(cartesian_coords)
for i in range(N):
p1 = cartesian_coords[i]
dist_i = {}
for j in range(N):
p2 = cartesian_coords[j]
if j != i:
dist_i[j] = space_euclidean_distance(p1, p2)
distances.append(dist_i)
G2 = nx.Graph()
for i, site in enumerate(supercell):
G2.add_node(i)
G2.nodes[i]["Species"] = label_set[site.species]
for i in range(N):
for key, value in distances[i].items():
if value <= 2.5: #metric for connection of 2 atoms
G2.add_edge(i, key, weight=value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.