Python實現標準.xyz文件導出

P實現內容

指定步數下的xyz文件 xyz文件格式要求

我是爲了求RDF和鍵角分佈用到的這個xyz文件,當然應該也會有其他的用處,代碼新手所寫,不夠精煉,各位大佬見諒。

所需文件

軌跡文件,Python

# -*- coding: utf-8 -*-
"""
Created on Thu Apr  2 11:04:29 2020

@author: YuanbaoQiang
"""

initial_step = input('請輸入文件的初始步數:')
thermo_step = input('請輸入運算步長:')
step_you_need = input('請輸入指定步數:')

number=int(step_you_need)/int(thermo_step)

#選取thermo_step*number步數範圍的數據
count = []
with open('./optimization.lammpstrj') as f_initial:
    lines = f_initial.readlines()
    number_of_atoms = int(lines[3])
    num = len(lines)
    for i, line in enumerate(lines):
        if line.startswith('ITEM: TIMESTEP'):            
           start = i
           count.append(start)
           
#存儲指定步數的header信息()
with open('./data_after_deal_header.txt','w') as f_delete_01:
    for i in range(count[int(number)],count[int(number)]+8):
         f_delete_01.write(lines[i])


with open('./data_after_deal_rest.txt','w') as f_delete_02:
    for i in range(count[int(number)]+9,count[int(number)]+9+number_of_atoms):
         f_delete_02.write(lines[i])         
        
#提取type, x, y, z
f_old = open('./data_after_deal_rest.txt')
line = f_old.readline()
result = []
while line:
    a = line.split()
    b = a[1:2] 
#原子1爲C,原子2爲H    
    b_after1 = ['C' if x == '1' else x for x in b]
    b_after2 = ['H' if x == '2' else x for x in b_after1]
    for item in b_after2:
        result.append(item)
    for item in a[2:5]:
        result.append(item)        
    line = f_old.readline()
f_old.close()        

with open('./result.xyz','w') as f_new:
    f_new.write(str(int(len(result)/4))+'\n'+'UAPE'+'\n')
    for i in range(len(result)):
        f_new.write(result[i]+' ')
        if (i+1) % 4 == 0:
            f_new.write('\n')
f_new.close()

print('OK,你可以拿去後處理了')

header_file:
在這裏插入圖片描述
.xyz:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章