欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python实现标准.xyz文件导出

程序员文章站 2022-03-27 10:25:03
...

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:
Python实现标准.xyz文件导出
.xyz:
Python实现标准.xyz文件导出