-
Notifications
You must be signed in to change notification settings - Fork 0
/
lattice.py
207 lines (173 loc) · 6.33 KB
/
lattice.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
from numpy.core.multiarray import zeros
from math import exp
class Atom:
def __init__(self, type, x, y, copies=1):
self.type, self.x, self.y, self.copies = type, x, y, copies
def __repr__(self):
return 'Atom(type=%r, x=%r, y=%r)' % (self.type, self.x, self.y)
def setParent(self, molecule):
self.parent = molecule
return self
@classmethod
def fromTuple(cls, t):
if len(t) == 4:
return cls(t[0],t[1],t[2],t[3])
else:
return cls(t[0],t[1],t[2])
def rotateCW(self, x0, y0):
self.x, self.y = x0-y0+self.y, y0+x0-self.x
def rotateCCW(self, x0, y0):
self.x, self.y = x0+y0-self.y, y0-x0+self.x
def translate(self, dx, dy):
self.x += dx
self.y += dy
class Molecule:
def __init__(self, moltype, x, y, template):
self.type = moltype;
self.x = self.y = 0
self.atomList = []
if type(template) is list:
atomList = template
else:
atomList = template['atomList']
for atom in atomList:
self.atomList.append(Atom(atom.type, atom.x, atom.y, atom.copies).setParent(self))
try:
self.dir = template['dir']
except (AttributeError, TypeError):
self.dir = (0,0)
self.translate(x, y)
self.boundMonomer = 0
def __repr__(self):
return 'Molecule(x=%r, y=%r, atomList=%r)' % (self.x, self.y, self.atomList)
def rotateCW(self):
"""Rotate each component atom clockwise"""
for atom in self.atomList: atom.rotateCW(self.x, self.y)
self.dir = (self.dir[1],-self.dir[0])
def rotateCCW(self):
"""Rotate each component atom counterclockwise"""
for atom in self.atomList: atom.rotateCCW(self.x, self.y)
self.dir = (-self.dir[1],self.dir[0])
def translate(self, dx, dy):
"""Translate each component atom by (dx, dy)"""
self.x += dx
self.y += dy
for atom in self.atomList: atom.translate(dx, dy)
class Lattice:
class QList:
def __init__(self):
self.list = []
self.n = 0
def __len__(self):
return self.n
def __getitem__(self,i):
return self.list[i]
def append(self, obj):
self.list.append(obj)
obj.__qid = self.n
self.n += 1
def remove(self, obj):
self.n -= 1
if obj.__qid != self.n:
self.list[obj.__qid] = self.list.pop()
self.list[obj.__qid].__qid = obj.__qid
else:
self.list.pop()
def __init__(self, dimX, dimY):
self.dimX, self.dimY = dimX, dimY
self.atomListMap = tuple([ tuple([ [] for y in range(dimY) ]) for x in range(dimX) ])
self.atomNumListMap = tuple([ tuple([ [] for y in range(dimY) ]) for x in range(dimX) ])
self.moleculeList = self.QList();
#self.moleculeList = [];
self.atomList = [];
self.moleculeNum = 0;
def getDataXY(self, mat, x, y):
return mat[x%self.dimX][y%self.dimY]
def setDataXY(self, mat, x, y, value):
mat[x%self.dimX][y%self.dimY] = value
def incDataXY(self, mat, x, y, inc):
mat[x%self.dimX][y%self.dimY] += inc
def getAtomListAt(self, x, y):
"""Return the list of atoms at (x,y)"""
return self.atomListMap[x%self.dimX][y%self.dimY]
# def getAtomNumListAt(self, x, y):
# """Return the list of atom types at (x,y)"""
# return self.atomNumListMap[x%self.dimX][y%self.dimY]
def getAtomNumAt(self, x, y):
return self.atomNumMap[x%self.dimX][y%self.dimY]
def addMolecule(self, molecule):
self.moleculeList.append(molecule)
self.layMolecule(molecule)
self.moleculeNum += 1;
def removeMolecule(self, molecule):
self.raiseMolecule(molecule)
self.moleculeList.remove(molecule)
self.moleculeNum -= 1;
def raiseMolecule(self, molecule, perm = True):
"""Temporarily remove a molecule from the lattice to facilate operations on that molecule"""
if perm:
for atom in molecule.atomList:
self.getAtomListAt(atom.x, atom.y).remove(atom)
self.incDataXY(self.atomNumMap[atom.type], atom.x, atom.y, -1)
self.atomList[atom.type].remove(atom)
else:
for atom in molecule.atomList:
self.getAtomListAt(atom.x, atom.y).remove(atom)
self.incDataXY(self.atomNumMap[atom.type], atom.x, atom.y, -1)
def layMolecule(self, molecule, perm = True):
"""Put back a raised molecule to the lattice after operations"""
if perm:
for atom in molecule.atomList:
self.getAtomListAt(atom.x, atom.y).append(atom)
self.incDataXY(self.atomNumMap[atom.type], atom.x, atom.y, 1)
self.atomList[atom.type].append(atom)
else:
for atom in molecule.atomList:
self.getAtomListAt(atom.x, atom.y).append(atom)
self.incDataXY(self.atomNumMap[atom.type], atom.x, atom.y, 1)
def translateMolecule(self, molecule, dx, dy, p = 0):
# Put doc
if p > 1: return
self.raiseMolecule(molecule, perm = False)
e1 = self.getEnergy(molecule)
molecule.translate(dx, dy)
e2 = self.getEnergy(molecule)
if p > min(1,exp(e1-e2)): molecule.translate(-dx, -dy)
self.layMolecule(molecule, perm = False)
def translateMoleculeByIndex(self, i, dx, dy, p = 0):
self.translateMolecule(self.moleculeList[i], dx, dy, p)
def rotateMoleculeCW(self, molecule, p = 0):
# Put doc
if p > 1: return
self.raiseMolecule(molecule, perm = False)
e1 = self.getEnergy(molecule)
molecule.rotateCW()
e2 = self.getEnergy(molecule)
if p > min(1,exp(e1-e2)): molecule.rotateCCW()
self.layMolecule(molecule, perm = False)
def rotateMoleculeCWByIndex(self, i, p = 0):
self.rotateMoleculeCW(self.moleculeList[i], p)
def rotateMoleculeCCW(self, molecule, p = 0):
if p > 1: return
self.raiseMolecule(molecule, perm = False)
e1 = self.getEnergy(molecule)
molecule.rotateCCW()
e2 = self.getEnergy(molecule)
if p > min(1,exp(e1-e2)): molecule.rotateCW()
self.layMolecule(molecule, perm = False)
def rotateMoleculeCCWByIndex(self, i, p = 0):
self.rotateMoleculeCCW(self.moleculeList[i], p)
def setAtomTypeNum(self, atomTypeNum):
self.atomTypeNum = atomTypeNum;
#self.atomList = [[] for x in range(atomTypeNum)]
self.atomList = [self.QList() for x in range(atomTypeNum)]
self.atomNumMap = zeros((self.atomTypeNum, self.dimX, self.dimY), int)
def setEnergyMatrix(self, energyMatrix):
self.energyMatrix = energyMatrix;
def getEnergy(self, molecule):
e = 0;
for atom in molecule.atomList:
#ls = self.getAtomNumAt(atom.x, atom.y)
for i in range(self.atomTypeNum):
e += self.getDataXY(self.atomNumMap[i], atom.x, atom.y) * self.energyMatrix[atom.type][i]
return e