| 1 | #!/usr/bin/env python | 
| 2 | """MetaData file remapper | 
| 3 |  | 
| 4 | Takes a MetaData file and maps all StuntDoubles back to the periodic box. | 
| 5 | Will optionally replicate the system in the three box directions, and | 
| 6 | then writes out a new MetaData file. | 
| 7 |  | 
| 8 | Usage: md2md | 
| 9 |  | 
| 10 | Options: | 
| 11 | -h, --help              show this help | 
| 12 | -m, --meta-data=...     use specified meta-data (.md, .eor) file | 
| 13 | -o, --output-file=...   use specified output file | 
| 14 | -x, --repeatX=...       make the system repeat in the x direction | 
| 15 | -y, --repeatY=...       make the system repeat in the y direction | 
| 16 | -z, --repeatZ=...       make the system repeat in the z direction | 
| 17 |  | 
| 18 |  | 
| 19 | Example: | 
| 20 | md2md -m lipidSystem.md -o bigLipidSystem.md -x 2 -y 2 -z 1 | 
| 21 |  | 
| 22 | """ | 
| 23 |  | 
| 24 | __author__ = "Dan Gezelter (gezelter@nd.edu)" | 
| 25 | __version__ = "$Revision: 1.1 $" | 
| 26 | __date__ = "$Date: 2008-02-13 16:44:14 $" | 
| 27 | __copyright__ = "Copyright (c) 2008 by the University of Notre Dame" | 
| 28 | __license__ = "OOPSE" | 
| 29 |  | 
| 30 | import sys | 
| 31 | import getopt | 
| 32 | import string | 
| 33 | import math | 
| 34 | import random | 
| 35 | from sets import * | 
| 36 |  | 
| 37 | _haveMDFileName = 0 | 
| 38 | _haveOutputFileName = 0 | 
| 39 |  | 
| 40 | repeatX = 1 | 
| 41 | repeatY = 1 | 
| 42 | repeatZ = 1 | 
| 43 |  | 
| 44 | metaData = [] | 
| 45 | frameData = [] | 
| 46 | indices = [] | 
| 47 | pvqj = [] | 
| 48 | p = [] | 
| 49 | v = [] | 
| 50 | q = [] | 
| 51 | j = [] | 
| 52 | Hmat = [] | 
| 53 | BoxInv = [] | 
| 54 |  | 
| 55 | def usage(): | 
| 56 | print __doc__ | 
| 57 |  | 
| 58 | def readFile(mdFileName): | 
| 59 | mdFile = open(mdFileName, 'r') | 
| 60 | # Find OOPSE version info first | 
| 61 | line = mdFile.readline() | 
| 62 | while 1: | 
| 63 | if '<OOPSE version=' in line: | 
| 64 | OOPSEversion = line | 
| 65 | break | 
| 66 | line = mdFile.readline() | 
| 67 |  | 
| 68 | # Rewind file and find start of MetaData block | 
| 69 |  | 
| 70 | mdFile.seek(0) | 
| 71 | line = mdFile.readline() | 
| 72 | print "reading MetaData" | 
| 73 | while 1: | 
| 74 | if '<MetaData>' in line: | 
| 75 | while 2: | 
| 76 | metaData.append(line) | 
| 77 | line = mdFile.readline() | 
| 78 | if '</MetaData>' in line: | 
| 79 | metaData.append(line) | 
| 80 | break | 
| 81 | break | 
| 82 | line = mdFile.readline() | 
| 83 |  | 
| 84 | mdFile.seek(0) | 
| 85 | print "reading Snapshot" | 
| 86 | line = mdFile.readline() | 
| 87 | while 1: | 
| 88 | if '<Snapshot>' in line: | 
| 89 | line = mdFile.readline() | 
| 90 | while 1: | 
| 91 | print "reading FrameData" | 
| 92 | if '<FrameData>' in line: | 
| 93 | while 2: | 
| 94 | frameData.append(line) | 
| 95 | if 'Hmat:' in line: | 
| 96 | L = line.split() | 
| 97 | Hxx = float(L[2].strip(',')) | 
| 98 | Hxy = float(L[3].strip(',')) | 
| 99 | Hxz = float(L[4].strip(',')) | 
| 100 | Hyx = float(L[7].strip(',')) | 
| 101 | Hyy = float(L[8].strip(',')) | 
| 102 | Hyz = float(L[9].strip(',')) | 
| 103 | Hzx = float(L[12].strip(',')) | 
| 104 | Hzy = float(L[13].strip(',')) | 
| 105 | Hzz = float(L[14].strip(',')) | 
| 106 | Hmat.append([Hxx, Hxy, Hxz]) | 
| 107 | Hmat.append([Hyx, Hyy, Hyz]) | 
| 108 | Hmat.append([Hzx, Hzy, Hzz]) | 
| 109 | BoxInv.append(1.0/Hxx) | 
| 110 | BoxInv.append(1.0/Hyy) | 
| 111 | BoxInv.append(1.0/Hzz) | 
| 112 | line = mdFile.readline() | 
| 113 | if '</FrameData>' in line: | 
| 114 | frameData.append(line) | 
| 115 | break | 
| 116 | break | 
| 117 |  | 
| 118 | line = mdFile.readline() | 
| 119 | while 1: | 
| 120 | if '<StuntDoubles>' in line: | 
| 121 | line = mdFile.readline() | 
| 122 | while 2: | 
| 123 | L = line.split() | 
| 124 | myIndex = int(L[0]) | 
| 125 | indices.append(myIndex) | 
| 126 | pvqj.append(L[1]) | 
| 127 | x = float(L[2]) | 
| 128 | y = float(L[3]) | 
| 129 | z = float(L[4]) | 
| 130 | p.append([x, y, z]) | 
| 131 | vx = float(L[5]) | 
| 132 | vy = float(L[6]) | 
| 133 | vz = float(L[7]) | 
| 134 | v.append([vx, vy, vz]) | 
| 135 | if 'pvqj' in L[1]: | 
| 136 | qw = float(L[8]) | 
| 137 | qx = float(L[9]) | 
| 138 | qy = float(L[10]) | 
| 139 | qz = float(L[11]) | 
| 140 | q.append([qw, qx, qy, qz]) | 
| 141 | jx = float(L[12]) | 
| 142 | jy = float(L[13]) | 
| 143 | jz = float(L[14]) | 
| 144 | j.append([jx, jy, jz]) | 
| 145 | else: | 
| 146 | q.append([0.0, 0.0, 0.0, 0.0]) | 
| 147 | j.append([0.0, 0.0, 0.0]) | 
| 148 |  | 
| 149 | line = mdFile.readline() | 
| 150 | if '</StuntDoubles>' in line: | 
| 151 | break | 
| 152 | break | 
| 153 | line = mdFile.readline() | 
| 154 | if not line: break | 
| 155 |  | 
| 156 | mdFile.close() | 
| 157 |  | 
| 158 | def writeFile(outputFileName): | 
| 159 | outputFile = open(outputFileName, 'w') | 
| 160 |  | 
| 161 | outputFile.write("<OOPSE version=4>\n"); | 
| 162 |  | 
| 163 | print "writing MetaData" | 
| 164 | for metaline in metaData: | 
| 165 | if 'nMol' in metaline: | 
| 166 | metasplit = metaline.split() | 
| 167 | nMol = float(metasplit[2].strip(';')) | 
| 168 | newNmol = nMol * repeatX * repeatY * repeatZ | 
| 169 | outputFile.write('  nMol = %10d;\n' % (newNmol)) | 
| 170 | else: | 
| 171 | outputFile.write(metaline) | 
| 172 |  | 
| 173 | print "writing Snapshot" | 
| 174 | outputFile.write("  <Snapshot>\n") | 
| 175 |  | 
| 176 | print "writing FrameData" | 
| 177 | for frameline in frameData: | 
| 178 | if 'Hmat:' in frameline: | 
| 179 | myH = [] | 
| 180 | myH.append([repeatX * Hmat[0][0], repeatX * Hmat[0][1], repeatX * Hmat[0][2]]) | 
| 181 | myH.append([repeatY * Hmat[1][0], repeatY * Hmat[1][1], repeatY * Hmat[1][2]]) | 
| 182 | myH.append([repeatZ * Hmat[2][0], repeatZ * Hmat[2][1], repeatZ * Hmat[2][2]]) | 
| 183 | outputFile.write("        Hmat: {{ %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }}\n" % (myH[0][0], myH[0][1], myH[0][2], myH[1][0], myH[1][1], myH[1][2], myH[2][0], myH[2][1], myH[2][2])) | 
| 184 | else: | 
| 185 | outputFile.write(frameline) | 
| 186 |  | 
| 187 | print "writing StuntDoubles" | 
| 188 | outputFile.write("    <StuntDoubles>\n") | 
| 189 | whichSD = 0 | 
| 190 |  | 
| 191 | for i in range(len(indices)): | 
| 192 | for ii in range(repeatX): | 
| 193 | for jj in range(repeatY): | 
| 194 | for kk in range(repeatZ): | 
| 195 |  | 
| 196 | myP = p[i] + ii*Hmat[0] + jj*Hmat[1] + kk*Hmat[2] | 
| 197 |  | 
| 198 | if (pvqj[i] == 'pv'): | 
| 199 | outputFile.write("%10d %7s %18.10g %18.10g %18.10g %14e %13e %13e\n" % (whichSD, pvqj[i], myP[0], myP[1], myP[2], v[i][0], v[i][1], v[i][2])) | 
| 200 | elif (sdFormat == 'pvqj'): | 
| 201 | outputFile.write("%10d %7s %18.10g %18.10g %18.10g %13e %13e %13e %13e %13e %13e %13e %13e %13e %13e\n" % (whichSD, pvqj[i], myP[0], myP[1], myP[2], v[i][0], v[i][1], v[i][2], q[i][0], q[i][1], q[i][2], q[i][3], j[i][0], j[i][1], j[i][2])) | 
| 202 |  | 
| 203 | whichSD = whichSD + 1 | 
| 204 |  | 
| 205 | outputFile.write("    </StuntDoubles>\n") | 
| 206 | outputFile.write("  </Snapshot>\n") | 
| 207 | outputFile.write("</OOPSE>\n") | 
| 208 | outputFile.close() | 
| 209 |  | 
| 210 | def roundMe(x): | 
| 211 | if (x >= 0.0): | 
| 212 | return math.floor(x + 0.5) | 
| 213 | else: | 
| 214 | return math.ceil(x - 0.5) | 
| 215 |  | 
| 216 | def wrapVector(myVect): | 
| 217 | scaled = [0.0, 0.0, 0.0] | 
| 218 | for i in range(3): | 
| 219 | scaled[i] = myVect[i] * BoxInv[i] | 
| 220 | scaled[i] = scaled[i] - roundMe(scaled[i]) | 
| 221 | myVect[i] = scaled[i] * Hmat[i][i] | 
| 222 | return myVect | 
| 223 |  | 
| 224 | def dot(L1, L2): | 
| 225 | myDot = 0.0 | 
| 226 | for i in range(len(L1)): | 
| 227 | myDot = myDot + L1[i]*L2[i] | 
| 228 | return myDot | 
| 229 |  | 
| 230 | def normalize(L1): | 
| 231 | L2 = [] | 
| 232 | myLength = math.sqrt(dot(L1, L1)) | 
| 233 | for i in range(len(L1)): | 
| 234 | L2.append(L1[i] / myLength) | 
| 235 | return L2 | 
| 236 |  | 
| 237 | def cross(L1, L2): | 
| 238 | # don't call this with anything other than length 3 lists please | 
| 239 | # or you'll be sorry | 
| 240 | L3 = [0.0, 0.0, 0.0] | 
| 241 | L3[0] = L1[1]*L2[2] - L1[2]*L2[1] | 
| 242 | L3[1] = L1[2]*L2[0] - L1[0]*L2[2] | 
| 243 | L3[2] = L1[0]*L2[1] - L1[1]*L2[0] | 
| 244 | return L3 | 
| 245 |  | 
| 246 | def mapToBox(): | 
| 247 | for i in range(len(indices)): | 
| 248 | dpos = p[i] | 
| 249 | p[i] = wrapVector(dpos) | 
| 250 |  | 
| 251 |  | 
| 252 | def main(argv): | 
| 253 | try: | 
| 254 | opts, args = getopt.getopt(argv, "hm:o:x:y:z:", ["help", "meta-data=", "output-file=", "repeatX=", "repeatY=", "repeatZ="]) | 
| 255 | except getopt.GetoptError: | 
| 256 | usage() | 
| 257 | sys.exit(2) | 
| 258 | for opt, arg in opts: | 
| 259 | if opt in ("-h", "--help"): | 
| 260 | usage() | 
| 261 | sys.exit() | 
| 262 | elif opt in ("-m", "--meta-data"): | 
| 263 | mdFileName = arg | 
| 264 | global _haveMDFileName | 
| 265 | _haveMDFileName = 1 | 
| 266 | elif opt in ("-o", "--output-file"): | 
| 267 | outputFileName = arg | 
| 268 | global _haveOutputFileName | 
| 269 | _haveOutputFileName = 1 | 
| 270 | elif opt in ("-x", "--repeatX"): | 
| 271 | repeatX = int(arg) | 
| 272 | elif opt in ("-y", "--repeatY"): | 
| 273 | repeatY = int(arg) | 
| 274 | elif opt in ("-z", "--repeatZ"): | 
| 275 | repeatZ = int(arg) | 
| 276 | if (_haveMDFileName != 1): | 
| 277 | usage() | 
| 278 | print "No meta-data file was specified" | 
| 279 | sys.exit() | 
| 280 | if (_haveOutputFileName != 1): | 
| 281 | usage() | 
| 282 | print "No output file was specified" | 
| 283 | sys.exit() | 
| 284 | readFile(mdFileName) | 
| 285 | mapToBox() | 
| 286 | writeFile(outputFileName) | 
| 287 |  | 
| 288 | if __name__ == "__main__": | 
| 289 | if len(sys.argv) == 1: | 
| 290 | usage() | 
| 291 | sys.exit() | 
| 292 | main(sys.argv[1:]) |