将模拟数据保存在 python

save simulation data in python

我正在 Python 中使用大量粒子进行分子动力学模拟。在这里,我必须跟踪 x 位置、y 位置、z 位置、x 速度、y 速度、z 速度、x 加速度、y 加速度、z 加速度、x 力、y 力、z 力、所有 500 或 1000 个粒子的势能以及系统在每个时间间隔的动能、势能和总能量。 为了保存这些数据,我目前正在通过以下方式将它们写入文件:

from numpy import*
...
f=open('myMD.dat','w')
while t<=tmax:
    s='%3.3f\t\t'%(t)  # writing the time to the file
    f.write(s)
    for i in range(TotalNumberofParticles):
        UpdateParameters()
        s='%8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e\t\t'%(xpos1[i],ypos1[i],zpos1[i],xvel1[i],yvel1[i],zvel1[i],xacc1[i],yacc1[i],zacc1[i],xforc[i],yforc[i],zforc[i],potn[i])
        f.write(s)
        ...
    s='%8.3e  %8.3e  %8.3e\n'%(KE,PE,TE)    
    f.write(s)
    t=t+delt
f.close()

所以如果有 500 个粒子,数据文件中的每一行将有 7000 列。不知何故,这似乎是一种糟糕的数据保存方式。此外,模拟持续了数千个时间步长。随着程序的进行,它也会变慢。有人可以告诉我如何以更好的方式保存数据吗?我也想知道如何检索这些数据以供进一步分析。

您可以使用 pickle 写入和读取任意数据:

import pickle

a= [4,6,3,7,]

f=open( "testfile", "wb")
pickle.dump(a,f)
f.close()

f=open( "testfile", "rb")
b=pickle.load(f)
f.close()

print b

与其在循环中单独写入每个 xpos[i],不如将整个数组 一次性写入 numpy.savez;

import numpy as np

np.savez('myMD.dat', 
         xpos1=xpos1, ypos1=ypos1, zpos1=zpos1,
         xvel1=xvel1, yvel1=yvel1, zvel1=zvel1,
         xacc1=xacc1, yacc1=yacc1, zacc1=zacc1,
         xforc=xforc, yforc=yforc, zforc=zforc,
         potn=potn)

像这样使用关键字参数可确保数组以其正确的名称保存。

由于您想为每个粒子 每次迭代写入日期,我建议您向数组添加一个额外的轴以包含所有迭代。

所以不用

xpos1 = np.array(TotalNumberofParticles)

xpos1 = np.array((number_of_iterations, TotalNumberofParticles))

您可以使用numpy.load再次读取数据:

import numpy as np

data = numpy.load('myMD.dat')
xpos1 = data['xpos1']
# et cetera

顺便说一句,来自 PEP 8(Python 风格指南):

Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.