通用仿真数据文件格式。
gsd的Python项目详细描述
GSD
gsd(general simulation data)是一个文件格式规范和读写它的库。包裹也 包含读取和写入hoomd模式gsd的python模块 文件夹。
请参阅readthedocs.io中的full GSD documentation。
概述
GSD文件:
- 有效地存储来自模拟运行的多帧数据。
- 高性能文件读写。
- 在每个帧中支持任意数据块(位置、方向、类型等) 将附加帧添加到现有文件中,并以单调递增的帧数。
- 对工作杀手有弹性。
- 每个帧中命名块的变量数。
- 每个帧中块的大小可变。
- 每个块标识数据类型。
- 常见用例:double、float、int、char类型的NxM数组
- 通用用例:n字节的二进制blob。
- 可以通过python或C API(1k行)集成到其他工具中
- 快速随机访问帧。
hoomd示例
创建一个hoomd gsd文件
>>>s=gsd.hoomd.Snapshot()>>>s.particles.N=4>>>s.particles.types=['A','B']>>>s.particles.typeid=[0,0,1,1]>>>s.particles.position=[[0,0,0],[1,1,1],[-1,-1,-1],[1,-1,-1]]>>>s.configuration.box=[3,3,3,0,0,0]>>>traj=gsd.hoomd.open(name='test.gsd',mode='wb')>>>traj.append(s)
将帧附加到gsd文件:
>>>defcreate_frame(i):...s=gsd.hoomd.Snapshot();...s.configuration.step=i;...s.particles.N=4+i;...s.particles.position=numpy.random.random(size=(4+i,3))...returns;>>>withgsd.hoomd.open('test.gsd','ab')ast:...t.extend((create_frame(i)foriinrange(10)))...print(len(t))11
随机索引帧:
>>>withgsd.hoomd.open('test.gsd','rb')ast:...snap=t[5]...print(snap.configuration.step)4...print(snap.particles.N)8...print(snap.particles.position)[[0.569932820.422434810.5502916][0.368924860.381670360.27310368][0.047390230.136034860.196539][0.1202320.915911440.99463677][0.798063160.169914360.15228257][0.137243080.142535270.02505][0.392874390.825190540.01613089][0.231503230.951674340.7715748]]
切片帧:
>>>withgsd.hoomd.open('test.gsd','rb')ast:...forsint[5:-2]:...print(s.configuration.step,end=' ')4567
文件层示例
withgsd.fl.open(name='file.gsd',mode='wb')asf:f.write_chunk(name='position',data=numpy.array([[1,2,3],[4,5,6]],dtype=numpy.float32));f.write_chunk(name='angle',data=numpy.array([0,1],dtype=numpy.float32));f.write_chunk(name='box',data=numpy.array([10,10,10],dtype=numpy.float32));f.end_frame()
withgsd.fl.open(name='file.gsd',mode='rb')asf:foriinrange(1,f.nframes):position=f.read_chunk(frame=i,name='position');do_something(position);