-
Notifications
You must be signed in to change notification settings - Fork 1
/
topologyer.py
569 lines (416 loc) · 13.5 KB
/
topologyer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import os
import math
import random
from collections import OrderedDict
class Vector3(object):
def __init__(self,x,y,z):
self.x = float(x)
self.y = float(y)
self.z = float(z)
def Clone(self):
return Vector3(self.x, self.y, self.z)
def Normalise(self):
norm = self.x*self.x + self.y*self.y + self.z*self.z
norm = math.sqrt(norm)
self.x /= norm
self.y /= norm
self.z /= norm
def __add__(self, other):
return Vector3(self.x+other.x, self.y+other.y, self.z+other.z)
def __sub__(self, other):
return Vector3(self.x-other.x, self.y-other.y, self.z-other.z)
def __str__(self):
return str(self.x)+' '+str(self.y)+' '+str(self.z)
@staticmethod
def Rotation(v,q):
p = Vector3(0,0,0)
p.x = (q.w*q.w + q.x*q.x - q.y*q.y - q.z*q.z)*v.x
p.x += 2*(q.x*q.y - q.w*q.z)*v.y
p.x += 2*(q.x*q.z + q.w*q.y)*v.z
p.y = 2*(q.x*q.y + q.w*q.z)*v.x
p.y += (q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z)*v.y
p.y += 2*(q.y*q.z - q.w*q.x)*v.z
p.z = 2*(q.x*q.z - q.w*q.y)*v.x
p.z += 2*(q.y*q.y + q.w*q.x)*v.y
p.z += (q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z)*v.z
return p
def Rotate(self,q):
p = Vector3.Rotation(self,q)
self.x=p.x
self.y=p.y
self.z=p.z
@staticmethod
def PBC(v,box):
p = v.Clone()
p.x -= math.floor(p.x/box.x)*box.x
p.y -= math.floor(p.y/box.y)*box.y
p.z -= math.floor(p.z/box.z)*box.z
return p
@staticmethod
def RandomDirection():
theta = random.uniform(-math.pi, math.pi)*0.5
phi = random.uniform(0,math.pi)*2
v = Vector3(math.sin(theta)*math.cos(phi), math.sin(theta)*math.sin(phi), math.cos(theta))
v.Normalise()
return v
class Quaternion(object):
@staticmethod
def Identity():
return Quaternion(1,0,0,0)
@staticmethod
def FromAngleAxis(angle, axis):
q = Quaternion.Identity()
axis.Normalise()
q.w = math.cos(0.5*angle)
q.x = axis.x * math.sin(0.5*angle)
q.y = axis.y * math.sin(0.5*angle)
q.z = axis.z * math.sin(0.5*angle)
return q
def __init__(self,w,x,y,z):
self.w = w
self.x = x
self.y = y
self.z = z
@staticmethod
def Random():
q = Quaternion.FromAngleAxis(random.uniform(0,2*math.pi), Vector3.RandomDirection())
return q
class AtomType:
def __init__(self, name, mass, charge, epsilon=0, sigma=1):
self.name = name
self.mass = mass
self.charge = charge
self.epsilon = epsilon
self.sigma = sigma
class MoleculeType:
def __init__(self, ID):
self.ID = ID
self.atoms = OrderedDict()
self.bonds = []
self.angles = []
self.dihedrals = []
self.impropers = []
def AddAtom(self, ID, typeID, position):
if ID in self.atoms.keys():
raise NameError("ERROR: atom["+ID+"] is already in the molecule.")
##TODO: check that the typeID exists? naaaa
atom = Atom(ID,typeID,position)
self.atoms[ID] = atom
return atom
def Print(self):
print 'MoleculeType ['+self.ID+']:'
print 'Atoms:'
for aID in self.atoms.keys():
a = self.atoms[aID]
a.Print()
print 'Bonds:'
print str(self.bonds)
class Atom:
pass
class Molecule:
pass
class System:
pass
## class for an atom in a molecule template
class Atom:
def __init__(self, ID, typeID, position=Vector3(0,0,0)):
self.ID = ID
self.typeID = typeID
self.position = position
self.index = -1
def Print(self):
print 'Atom ['+self.ID+'] type '+self.typeID+'\t-> '+str(self.position)
def Clone(self):
atom = Atom(self.ID, self.typeID, self.position)
return atom
## class for a molecule in the topology
class Molecule:
def __init__(self, ID, typeID, position=Vector3(0,0,0), rotation=Quaternion.Identity()):
self.index = -1
self.ID = ID
self.typeID = typeID
self.position = position
self.rotation = rotation
self.atoms = OrderedDict()
def GetAtomPosition(self, atomID, system):
atom = system.moleculeTypes[self.typeID].atoms[atomID]
p = atom.position.Clone()
p = p.Rotation(self.rotation)
p = p+self.position
return p
def Initialise(self, molType, startID):
print 'mol init',molType.ID,molType.atoms.keys()
self.atoms = OrderedDict()
idcnt = startID
for aID in molType.atoms.keys(): #take all atoms from the moltype and replicate them
a = molType.atoms[aID]
atm = a.Clone(); #clone and set the position
atm.position = Vector3.Rotation(a.position,self.rotation) + self.position
atm.index = idcnt
self.atoms[aID] = atm
idcnt+=1
class System(object):
def __init__(self, ID):
self.ID = ID
self.__atomtypes = OrderedDict()
self.topology = OrderedDict()
self.moleculeTypes = OrderedDict()
self.box = Vector3(1,1,1)
self.bondTypes = OrderedDict()
self.angleTypes = OrderedDict()
self.dihedralTypes = OrderedDict()
self.improperTypes = OrderedDict()
print 'System '+ID+' created.'
def AddAtomType(self, **kwargs):
if not ('name' in kwargs.keys()):
raise NameError("ERROR: atomtype must have a name.")
name = kwargs['name']
if name in self.__atomtypes.keys():
raise NameError("ERROR: "+name+" is already in the atom types list.")
if not ('mass' in kwargs.keys()):
print "WARNING: atom type mass is not given: using 1"
mass = 1
else:
mass = float(kwargs['mass'])
if not ('charge' in kwargs.keys()):
print "WARNING: atom type charge is not given: using 0"
charge = 0
else:
charge = float(kwargs['charge'])
if not ('epsilon' in kwargs.keys()):
print "WARNING: atom type LJ epsilon is not given: using 0"
epsilon = 0
else:
epsilon = float(kwargs['epsilon'])
if not ('sigma' in kwargs.keys()):
print "WARNING: atom type LJ sigma is not given: using 1"
sigma = 0
else:
sigma = float(kwargs['sigma'])
self.__atomtypes[name] = AtomType(name,mass,charge,epsilon,sigma)
print 'Atom type '+name+' created.'
def RemoveAtomType(self, name):
if not(name in self.__atomtypes.keys()):
print "WARNING: "+name+" is not in the atom types list."
return
del self.__atomtypes[name]
print 'Atom type '+name+' removed.'
def AddMoleculeType(self,name):
if name in self.moleculeTypes.keys():
raise NameError("ERROR: molcule type["+name+"] was already defined.")
mol = MoleculeType(name)
self.moleculeTypes[name] = mol
print 'Molecule template '+name+' created.'
return mol
def MakeMolecule(self, ID, typeID, position=Vector3(0,0,0), rotation=Quaternion.Identity()):
if not(typeID in self.moleculeTypes.keys()):
raise NameError('ERROR: molecule type '+typeID+' not defined.')
if ID in self.topology.keys():
raise NameError('ERROR: molecule '+ID+' is already in the topology.')
mol = Molecule(ID,typeID,position,rotation)
self.topology[ID] = mol
print 'molecule created and added to topology.'
return mol
def ExportLAMMPS(self, filename):
f = open(filename,'w')
f.write('system '+self.ID+' created by LAMMPSer\n\n')
#compute the number of atoms
counter = 0
for molID in self.topology.keys():
mol = self.topology[molID]
counter += len(self.moleculeTypes[mol.typeID].atoms.keys())
print 'total # atoms: ',counter
f.write(str(counter)+' atoms\n')
#compute the number of bonds
counter = 0
for molID in self.topology.keys():
mol = self.topology[molID]
counter += len(self.moleculeTypes[mol.typeID].bonds)
print 'total # bonds: ',counter
f.write(str(counter)+' bonds\n')
#compute the number of angles
counter = 0
for molID in self.topology.keys():
mol = self.topology[molID]
counter += len(self.moleculeTypes[mol.typeID].angles)
print 'total # angles: ',counter
f.write(str(counter)+' angles\n')
#compute the number of diheds
counter = 0
for molID in self.topology.keys():
mol = self.topology[molID]
counter += len(self.moleculeTypes[mol.typeID].dihedrals)
print 'total # dihedrals: ',counter
f.write(str(counter)+' dihedrals\n')
#compute the number of improps
counter = 0
for molID in self.topology.keys():
mol = self.topology[molID]
counter += len(self.moleculeTypes[mol.typeID].impropers)
print 'total # impropers: ',counter
f.write(str(counter)+' impropers\n')
#compute the number of Types
f.write(str(len(self.__atomtypes))+' atom types\n')
f.write(str(len(self.bondTypes))+' bond types\n')
f.write(str(len(self.angleTypes))+' angle types\n')
f.write(str(len(self.dihedralTypes))+' dihedral types\n')
f.write(str(len(self.improperTypes))+' improper types\n\n')
f.write('0 '+str(self.box.x)+' xlo xhi\n')
f.write('0 '+str(self.box.y)+' ylo yhi\n')
f.write('0 '+str(self.box.z)+' zlo zhi\n\nMasses\n\n')
#determine the type list
atomtypelist = self.__atomtypes.keys()
counter = 1
for atID in atomtypelist:
f.write(str(counter)+' '+str(self.__atomtypes[atID].mass)+'\n')
counter += 1
f.write('\n')
f.write('Pair Coeffs\n\n')
counter = 1
for atID in atomtypelist:
f.write(str(counter)+'\t'+str(self.__atomtypes[atID].epsilon)+'\t'+str(self.__atomtypes[atID].sigma)+'\n')
counter += 1
f.write('\n')
#determine the type list - bonds
bondtypelist = self.bondTypes.keys()
if len(bondtypelist) > 0:
counter = 1
f.write('Bond Coeffs\n\n')
for bt in bondtypelist:
b = self.bondTypes[bt]
f.write(str(counter)+'\t'+str(b[0])+'\t'+str(b[1])+'\n')
counter += 1
f.write('\n')
angletypelist = self.angleTypes.keys()
if len(angletypelist) > 0:
counter = 1
f.write('Angle Coeffs\n\n')
for bt in angletypelist:
b = self.angleTypes[bt]
f.write(str(counter)+'\t'+str(b[0])+'\t'+str(b[1])+'\t'+str(b[2])+'\n')
counter += 1
f.write('\n')
dihedtypelist = self.dihedralTypes.keys()
if len(dihedtypelist) > 0:
counter = 1
f.write('Dihedral Coeffs\n\n')
for bt in dihedtypelist:
b = self.dihedralTypes[bt]
f.write(str(counter)+'\t'+str(b[0])+'\t'+str(b[1])+'\t'+str(b[2])+'\t'+str(b[3])+'\n')
counter += 1
f.write('\n')
impstypelist = self.improperTypes.keys()
if len(impstypelist) > 0:
counter = 1
f.write('Improper Coeffs\n\n')
for bt in impstypelist:
b = self.improperTypes[bt]
f.write(str(counter)+'\t'+str(b[0])+'\t'+str(b[1])+'\t'+str(b[2])+'\n')
counter += 1
f.write('\n')
f.write('Atoms\n\n')
counter = 1; molidx = 1
for molID in self.topology.keys():
mol = self.topology[molID]
mol.index = molidx
mol.Initialise(self.moleculeTypes[mol.typeID],counter)
#for aID in self.moleculeTypes[mol.typeID].atoms.keys():
for aID in mol.atoms.keys():
#for atom in mol.atoms:
atom = mol.atoms[aID]
#atom = self.moleculeTypes[mol.typeID].atoms[aID]
atomTemplate = self.__atomtypes[atom.typeID]
f.write(str(atom.index)+'\t') #atom index
f.write(str(mol.index)+'\t') #molecule index
f.write(str(atomtypelist.index(atom.typeID)+1)+'\t') #atom type
f.write(str(atomTemplate.charge)+'\t') #molecule index
p = Vector3.PBC(atom.position, self.box)
f.write(str(p)+'\n')
counter += 1
molidx += 1
#write the bonds
hasBonds = False
for molID in self.topology.keys():
mol = self.topology[molID]
if len(self.moleculeTypes[mol.typeID].bonds) > 0:
hasBonds = True
break
if hasBonds: #write the bonds if there are any
f.write('\nBonds\n\n')
counter = 1
for molID in self.topology.keys():
mol = self.topology[molID]
molType = self.moleculeTypes[mol.typeID]
for bnd in molType.bonds:
bndIndex = bondtypelist.index(bnd[0])+1
atom1ID = mol.atoms[bnd[1]].index
atom2ID = mol.atoms[bnd[2]].index
f.write(str(counter)+' '+str(bndIndex)+' ')
f.write(str(atom1ID)+" "+str(atom2ID)+"\n")
counter+=1
#write the angles
hasAngles = False
for molID in self.topology.keys():
mol = self.topology[molID]
if len(self.moleculeTypes[mol.typeID].angles) > 0:
hasAngles = True
break
if hasAngles: #write the bonds if there are any
f.write('\nAngles\n\n')
counter = 1
for molID in self.topology.keys():
mol = self.topology[molID]
molType = self.moleculeTypes[mol.typeID]
for bnd in molType.angles:
bndIndex = angletypelist.index(bnd[0])+1
atom1ID = mol.atoms[bnd[1]].index
atom2ID = mol.atoms[bnd[2]].index
atom3ID = mol.atoms[bnd[3]].index
f.write(str(counter)+' '+str(bndIndex)+' ')
f.write(str(atom1ID)+" "+str(atom2ID)+" "+str(atom3ID)+"\n")
counter+=1
#write the dihedrals
hasDiheds = False
for molID in self.topology.keys():
mol = self.topology[molID]
if len(self.moleculeTypes[mol.typeID].dihedrals) > 0:
hasDiheds = True
break
if hasDiheds: #write the bonds if there are any
f.write('\nDihedrals\n\n')
counter = 1
for molID in self.topology.keys():
mol = self.topology[molID]
molType = self.moleculeTypes[mol.typeID]
for bnd in molType.dihedrals:
bndIndex = dihedtypelist.index(bnd[0])+1
atom1ID = mol.atoms[bnd[1]].index
atom2ID = mol.atoms[bnd[2]].index
atom3ID = mol.atoms[bnd[3]].index
atom4ID = mol.atoms[bnd[4]].index
f.write(str(counter)+' '+str(bndIndex)+' ')
f.write(str(atom1ID)+" "+str(atom2ID)+" "+str(atom3ID)+" "+str(atom4ID)+"\n")
counter+=1
#write the dihedrals
hasImps = False
for molID in self.topology.keys():
mol = self.topology[molID]
if len(self.moleculeTypes[mol.typeID].impropers) > 0:
hasImps = True
break
if hasImps: # write the bonds if there are any
f.write('\nImpropers\n\n')
counter = 1
for molID in self.topology.keys():
mol = self.topology[molID]
molType = self.moleculeTypes[mol.typeID]
for bnd in molType.dihedrals:
bndIndex = dihedtypelist.index(bnd[0])+1
atom1ID = mol.atoms[bnd[1]].index
atom2ID = mol.atoms[bnd[2]].index
atom3ID = mol.atoms[bnd[3]].index
atom4ID = mol.atoms[bnd[4]].index
f.write(str(counter)+' '+str(bndIndex)+' ')
f.write(str(atom1ID)+" "+str(atom2ID)+" "+str(atom3ID)+" "+str(atom4ID)+"\n")
counter+=1
f.close()