OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
omdLast
1#!@Python3_EXECUTABLE@
2"""OpenMD File Extractor
3
4Makes omd file from the last good frame of OpenMD dump file.
5
6Usage: omdLast
7
8Options:
9 -h, --help show this help
10 -m, --meta-data=... use specified OpenMD (.dump) file
11
12Example:
13 omdLast -m Ar.dump
14
15"""
16
17__author__ = "Hemanta Bhattarai (hbhattar@nd.edu), Charles Vardeman (cvardema@nd.edu)"
18__copyright__ = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
19__license__ = "OpenMD"
20
21import sys
22import getopt
23import string
24import os
25
26
27_haveMDFileName = 0
28
29
30def usage():
31 print(__doc__)
32
33
34def getLastFrame(mdFileName):
35 mdFile = open(mdFileName, 'r')
36
37 (filepath, filename) = os.path.split(mdFileName)
38
39 (outfilePrefix, ext) = os.path.splitext(filename)
40
41
42
43 metaDataFrame = []
44 haveMetaData = 0
45 haveMetaDataEnd = 0
46# Find OpenMD version info first
47 line = mdFile.readline()
48 while True:
49 if '<OOPSE version=' in line or '<OpenMD version=' in line:
50 OpenMDversion = line
51 break
52 line = mdFile.readline()
53
54# Rewind file and find start of MetaData block
55
56 mdFile.seek(0)
57 breakLoop = False
58 line = mdFile.readline()
59
60 while True:
61 if '<MetaData>' in line:
62 while 2:
63 metaDataFrame.append(line)
64 line = mdFile.readline()
65 if '</MetaData>' in line:
66 metaDataFrame.append(line)
67 breakLoop = True
68 break
69 if breakLoop:
70 break
71 line = mdFile.readline()
72
73
74
75
76 # reading last valid frame from dump file
77 mdFile.seek(0)
78 framePos = 0
79 foundFrames = 0;
80 lineReversed = reversed(mdFile.readlines())
81 SnapshotFrameReversed = []
82 writeSnapshotFrameReversed = False
83 for line in lineReversed:
84 splittedLine = line.split()
85 if '</Snapshot>' in splittedLine or writeSnapshotFrameReversed == True:
86 writeSnapshotFrameReversed = True
87 SnapshotFrameReversed.append(line)
88
89 if '<Snapshot>' in splittedLine and writeSnapshotFrameReversed:
90 break
91
92 mdFile.close()
93
94 # checking if there exist valid frame
95 try:
96 SnapshotFrameReversed[0]
97 except IndexError:
98 print("\t\tError: %s doesn't have any valid frames." %filename)
99 return None
100
101 # writing out the valid frame
102
103 outfilename = outfilePrefix + "_lastframe" + ".omd"
104 outFile = open(outfilename, 'w')
105 outFile.write(OpenMDversion)
106 for metaline in metaDataFrame:
107 outFile.write(metaline)
108
109 for lineNumber in range(1,len(SnapshotFrameReversed)+1):
110 outFile.write(SnapshotFrameReversed[-lineNumber])
111
112 outFile.write("</OpenMD>")
113 outFile.close()
114
115
116
117
118def main(argv):
119 try:
120 opts, args = getopt.getopt(argv, "hm:", ["help", "meta-data=", ""])
121 except getopt.GetoptError:
122 usage()
123 sys.exit(2)
124 for opt, arg in opts:
125 if opt in ("-h", "--help"):
126 usage()
127 sys.exit()
128 elif opt in ("-m", "--meta-data"):
129 mdFileName = arg
130 global _haveMDFileName
131 _haveMDFileName = 1
132
133
134
135 if (_haveMDFileName != 1):
136 usage()
137 print("No OpenMD file was specified")
138 sys.exit()
139
140 getLastFrame(mdFileName)
141
142
143if __name__ == "__main__":
144 if len(sys.argv) == 1:
145 usage()
146 sys.exit()
147 main(sys.argv[1:])