C2DB數(shù)據(jù)庫是根據(jù)已知二維材料替換元素得到擴(kuò)充數(shù)據(jù)路镇辉,其包含PBE、HSE苞七、結(jié)構(gòu)穩(wěn)定性器予、convexhull以及真空能級等性質(zhì),可以作為研究二維材料的數(shù)據(jù)庫之一浙芙,這里給出其數(shù)據(jù)庫中的結(jié)構(gòu)提取腳本登刺,方便大家前處理遇到的困苦,加快自己的ideal的推進(jìn)嗡呼。方法比較簡單粗暴莽夫哲學(xué)(亞歷山大砍戈耳狄俄斯之結(jié))(https://zh.wikipedia.org/wiki/%E4%BA%9A%E5%8E%86%E5%B1%B1%E5%A4%A7%E5%A4%A7%E5%B8%9D)
)纸俭。
最近C2DB做了更新,目前我基于的是其2018年的文章中涉及的數(shù)據(jù)庫,之前還能下南窗,現(xiàn)在也找不到下載的地方了揍很。
'''
Created on 14:35, Mar. 25, 2021
@author: yilinZhang
@address: Jilin University
These files are needed: c2db.db
'''
import numpy as np
import pandas as pd
import os
def prehandle_c2db():
import json
import sqlite3
# Step1: connect database and get the features
con = sqlite3.connect("c2db.db")
df = pd.read_sql_query("SELECT * FROM systems", con)
# Step2: convert database to csv
key_value_pairs = []
for line in df.key_value_pairs:
key_value_pairs.append(json.loads(line))
key_value_pairs = pd.DataFrame(key_value_pairs)
# Step3: rename formula
formula_values = list(key_value_pairs['folder'].str.split('/'))
flag = []
for line in formula_values:
flag.append(line[-2])
key_value_pairs['formula'] = flag
# Step4: sorted the vbm
key_value_pairs['vbm_to_evac'] = key_value_pairs['vbm'] - key_value_pairs['evac']
# key_value_pairs['cbm_to_evac'] = key_value_pairs['cbm'] - key_value_pairs['evac']
hasBandGap = key_value_pairs[key_value_pairs['vbm_to_evac'] <= 0]#肯定要滿足價(jià)帶不能超過靜電勢能吧
# hasBandGap = hasBandGap[hasBandGap['gap'] > 0.3]
hasBandGap = hasBandGap[hasBandGap['ehull'] <= 0]# convexhull也要滿足一下
# hasBandGap = hasBandGap[hasBandGap['dynamic_stability_level'] == 3]
hasBandGap = hasBandGap.sort_values(by=['vbm_to_evac'], axis=0)
hasBandGap.to_csv('sorted_by_vbm_to_evac.csv')
def writePOSCAR(atom):
with open(path + os.sep + filename + '_POSCAR', 'w') as f:
f.write(self.parameters['SystemName'] + '\n')
f.write(str(self.parameters['ScalingFactor']) + '\n')
LatticeMatrix = self.parameters['LatticeMatrix']
for i in range(len(LatticeMatrix)):
f.write(float2line(LatticeMatrix[i]))
AtomsInfo = self.parameters['AtomsInfo']
string = ['', '']
for key in AtomsInfo.keys():
string[0] += key + ' '
string[1] += str(AtomsInfo[key]) + ' '
string[0] += '\n'
string[1] += '\n'
f.write(string[0])
f.write(string[1])
f.write(self.parameters['CoordinateType'] + '\n')
ElementsPositionMatrix = self.parameters['ElementsPositionMatrix'].copy(
)
for key in ElementsPositionMatrix.keys():
arr = ElementsPositionMatrix[key]
for i in range(len(arr)):
f.write(float2line(arr[i]))
def output_structure():
import ase.db
from ase.io import write
dbpath = os.path.join(os.getcwd(), 'c2db.db')
db = ase.db.connect(dbpath)
def float2line(mart):
'''
將矩陣轉(zhuǎn)換成可輸出的字符串形式
'''
string = ''
for line in mart:
for s in line:
string += '%21.10f' % float(s)
string += '\n'
return string
def count_element(mart):
string = ['', '']
elements = sorted(set(mart), key=mart.index)
for key in elements:
string[0] += key + ' '
string[1] += str(mart.count(key)) + ' '
string[0] += '\n'
string[1] += '\n'
return string
# with open('bandAlignment_CSV_names', 'r') as f:
# lines = f.readlines()
# for i in range(len(lines)):
# os.chdir(lines[i].strip()[:-4])
csv = pd.read_csv('sorted_by_vbm_to_evac.csv')
for i in range(len(csv)):
atom = db.get_atoms(uid=csv.iloc[i]['uid'], add_additional_information=True)
write('{}.cif'.format(csv.iloc[i]['formula']), atom, format='cif')
with open(csv.iloc[i]['formula'] + '.vasp', 'w') as f:
f.write(csv.iloc[i]['uid'] + '\n')
f.write('1.0\n')
f.write(float2line(atom.get_cell()))
atomInfo = count_element(atom.get_chemical_symbols())
f.write(atomInfo[0])
f.write(atomInfo[1])
f.write('Cartesian\n')
f.write(float2line(atom.get_positions()))
if __name__ == "__main__":
prehandle_c2db()
# after_handle()
output_structure()
os.system('rm *.cif')