OpenMPCD
VTFSnapshotFile.py
1 from .ParticleCollection import ParticleCollection
2 from .Vector3DReal import Vector3DReal
3 
4 import itertools
5 import os.path
6 
8  def __init__(self, path, assertReadMode = False):
9 
10  self.path = path
11 
12  self.structureBlock = None
13 
14  self._lineNumber = 0
15 
16  if os.path.exists(path):
17  self.file = open(path, "r")
18  self.mode = "r"
19 
20  self.readStructureBlock()
21  elif os.path.exists(path + ".xz"):
22  import lzma
23  self.file = lzma.LZMAFile(path + ".xz", "r")
24  self.mode = "r"
25 
26  self.readStructureBlock()
27  else:
28  if assertReadMode:
29  raise RuntimeError("VTFSnapshotFile does not exist: " + path)
30 
31  self.file = open(path, "w")
32  self.mode = "w"
33 
34  def isInReadMode(self):
35  return self.mode == "r"
36 
37  def isInWriteMode(self):
38  return self.mode == "w"
39 
40  def getStructureBlock(self):
41  return self.structureBlock
42 
43  def setStructureBlock(self, structureBlock):
44  self.structureBlock = structureBlock
45 
46  def readStructureBlock(self):
47  if not self.isInReadMode() or self.structureBlock is not None:
48  raise ValueError("")
49 
50  self.structureBlock = ""
51 
52  for line in self.file:
53  self._lineNumber += 1
54 
55  if line.startswith("timestep"):
56  return
57 
58  self.structureBlock += line
59 
60  def writeStructureBlock(self):
61  if not self.isInWriteMode() or self.structureBlock is None:
62  raise ValueError("")
63 
64  self.file.write(self.structureBlock)
65 
66  def readTimestep(self):
67  positions = []
68  velocities = []
69  for line in self.file:
70  self._lineNumber += 1
71 
72  if line.startswith("timestep"):
73  break
74 
75  parts = line.split()
76 
77  if len(parts) not in [3, 6]:
78  message = "Unexpected line, number " + str(self._lineNumber)
79  message += ":\n"
80  message += line
81  raise ValueError(message)
82 
83  x = float(parts[0])
84  y = float(parts[1])
85  z = float(parts[2])
86  positions.append(Vector3DReal(x, y, z))
87 
88  if len(parts) == 6:
89  vx = float(parts[3])
90  vy = float(parts[4])
91  vz = float(parts[5])
92  velocities.append(Vector3DReal(vx, vy, vz))
93 
94  collection = ParticleCollection()
95  collection.setPositionsAndVelocities(positions, velocities)
96 
97  return collection
98 
99  def writeTimestep(self, particleCollection):
100  if not self.isInWriteMode():
101  raise ValueError("")
102 
103  positions = particleCollection.getPositions()
104  velocities = particleCollection.getVelocities()
105 
106  self.file.write("timestep\n")
107  for position, velocity in itertools.izip_longest(positions, velocities):
108  self.file.write(str(position.getX().real) + " ")
109  self.file.write(str(position.getY().real) + " ")
110  self.file.write(str(position.getZ().real))
111 
112  if velocity is not None:
113  self.file.write(" " + str(velocity.getX().real))
114  self.file.write(" " + str(velocity.getY().real))
115  self.file.write(" " + str(velocity.getZ().real))
116 
117  self.file.write("\n")
MPCDAnalysis.VTFSnapshotFile.VTFSnapshotFile.file
file
Definition: VTFSnapshotFile.py:17
MPCDAnalysis.VTFSnapshotFile.VTFSnapshotFile.structureBlock
structureBlock
Definition: VTFSnapshotFile.py:12
MPCDAnalysis.Vector3DReal.Vector3DReal
Definition: Vector3DReal.py:7
MPCDAnalysis.VTFSnapshotFile.VTFSnapshotFile.readStructureBlock
def readStructureBlock(self)
Definition: VTFSnapshotFile.py:46
MPCDAnalysis.VTFSnapshotFile.VTFSnapshotFile.mode
mode
Definition: VTFSnapshotFile.py:18
MPCDAnalysis.VTFSnapshotFile.VTFSnapshotFile._lineNumber
_lineNumber
Definition: VTFSnapshotFile.py:14
MPCDAnalysis.VTFSnapshotFile.VTFSnapshotFile.isInReadMode
def isInReadMode(self)
Definition: VTFSnapshotFile.py:34
MPCDAnalysis.VTFSnapshotFile.VTFSnapshotFile.path
path
Definition: VTFSnapshotFile.py:10
MPCDAnalysis.VTFSnapshotFile.VTFSnapshotFile.isInWriteMode
def isInWriteMode(self)
Definition: VTFSnapshotFile.py:37
MPCDAnalysis.ParticleCollection.ParticleCollection
Definition: ParticleCollection.py:11
MPCDAnalysis.VTFSnapshotFile.VTFSnapshotFile
Definition: VTFSnapshotFile.py:7