亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何根據通過熊貓從LAMMPS輸出文件導入的鍵合數據將原子細分為三組?

如何根據通過熊貓從LAMMPS輸出文件導入的鍵合數據將原子細分為三組?

慕田峪9158850 2022-09-27 09:55:37
我是編程和分子動力學模擬的新手。我正在使用LAMMPS來模擬物理氣相沉積(PVD)過程,并確定不同時間步長中原子之間的相互作用。在我執行分子動力學模擬后,LAMMPS為我提供了一個輸出鍵文件,其中包含每個單個原子的記錄(作為原子ID),它們的類型(向特定元素的編號),以及與這些特定原子鍵合的其他原子的信息。 典型的債券文件如下所示。我的目標是根據原子的類型(如組1:氧-氫-氫)對原子進行三組分類,方法是考慮它們從鍵輸出文件中的鍵合信息,并計算每個時間步長的組數。我使用熊貓,并為每個時間步長創建了一個數據幀。df = pd.read_table(directory, comment="#", delim_whitespace= True, header=None, usecols=[0,1,2,3,4,5,6] )headers= ["ID","Type","NofB","bondID_1","bondID_2","bondID_3","bondID_4"]df.columns = headersdf.fillna(0,inplace=True)df = df.astype(int)timestep = int(input("Number of Timesteps: ")) #To display desired number of timesteps.total_atom_number = 53924 #Total number of atoms in the simulation.t= 0 #code starts from 0th timestep.firstTime = []while(t <= timestep):    open('file.txt', 'w').close() #In while loop = displays every timestep individually, Out of the while loop = displays results cumulatively.    i = 0    df_tablo =(df[total_atom_number*t:total_atom_number*(t+1)]) #Creates a new dataframe that inlucdes only t'th timestep.    df_tablo.reset_index(inplace=True)    print(df_tablo)請參閱此示例,該示例說明了我對 3 個原子進行分組的算法。鍵合列顯示與其行中的原子鍵合在一起的不同原子(按原子 ID)。例如,通過使用該算法,我們可以對[1,2,5]和[1,2,6]進行分組,但不能對[1,2,1]進行分組,因為原子不能與自身建立鍵。此外,我們可以在分組后將這些原子ID(第一列)轉換為它們的原子類型(第二列),例如[1,3,7]到[1,1,3]。通過遵循上面提到的鍵,1)我可以成功地將原子相對于它們的ID分組,2)將它們轉換為它們的原子類型,3)分別計算每個時間步中存在的組數。第一個 while 循環(上圖)計算每個時間步的組,而第二個 while 循環(下圖)將每行中的原子(等于存在的每個原子 ID)與數據幀中不同行的相應綁定原子進行分組。
查看完整描述

1 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

我不確定我是否理解了邏輯,看看這是否有幫助。


對于100000三重奏,需要41秒。


loc,get_loc是非常廣泛的操作,所以把你的表放在字典里,而不是驗證一切都是唯一的,把它放在一個集合中


import pandas as pd

import random

from collections import defaultdict as dd

from collections import Counter

import time


# create 100000 unique trios of numbers

ids = list(range(50000))

trios_set = set()

while len(trios_set)<100000:

    trio = random.sample(ids,3)

    trios_set.add(frozenset(trio))


ids_dict = dd(list)  # a dictionery where id is the key and value is all the id who are partner with it in a list


for s in trios_set:

    for id in s:

        for other_id in s:

            if id!= other_id:

                ids_dict[id].append(other_id)


ids_dict = dict(ids_dict)

for_df = []

type_list = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n"] 

for id in ids_dict:

    massage = {}

    massage["id"] = id

    other_id_index = 1

    for other_id in ids_dict[id]:

        massage["id_"+str(other_id_index)] =  other_id

        other_id_index+=1

    massage["type"] = random.choice(type_list)

    for_df.append(massage)


df = pd.DataFrame(for_df) # a table with id column and all ids who are with it in trios in id_1 id_2.. and type column with a letter


#------------------------------------------------------------------

#till here we built the input table

start_time = time.time() #till here we build the input table, now check the time for 100000 atoms

type_dict = {}

from_df = dd(set)


for i,r in df.iterrows(): #move the dataframe to a dict of id as key and value as list of ids who connected to it

    for col in df:

        if "id_"in col and str(r[col])!="nan":

            from_df[r["id"]].add(r[col])

    type_dict[r["id"]] = r["type"] #save the type of id in a dictionery


from_df = dict(from_df)

out_trio_set = set() 


for id in from_df:

    for other_id in from_df[id]:

        if other_id!= id and str(other_id)!="nan":

            for third_id in from_df[other_id]:

                current_trio = frozenset([id, other_id,third_id])

                if len(current_trio)==3:

                    out_trio_set.add(current_trio)


type_conter = Counter()


for trio in out_trio_set:

    type_list = []

    for id in trio:

        type_list.append(type_dict[id])

    type_list = sorted(type_list)

    atom_type = "".join(type_list)

    type_conter[atom_type] +=1


out_df = pd.DataFrame(type_conter, index = [1]) # in here put index as timestamp


out_df.to_excel(r"D:\atom.xlsx")

print("--- %s seconds ---" % (time.time() - start_time))


查看完整回答
反對 回復 2022-09-27
  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號