-
Notifications
You must be signed in to change notification settings - Fork 58
/
lattice
executable file
·155 lines (126 loc) · 4.87 KB
/
lattice
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
#!/usr/bin/env python
# lattice - generate molecular coordinates on a lattice
# Agilio Padua <[email protected]>, version 2013/12/16
# http://perso.ens-lyon.fr/agilio.padua
import sys
class cell:
def __init__(self, ltype = 'fcc', scale = 1.0):
if ltype == 'fcc':
self.basis = [[0.0, 0.0, 0.0],
[0.0, 0.5, 0.5],
[0.5, 0.0, 0.5],
[0.5, 0.5, 0.0]]
offset = 0.25
elif ltype == 'bcc':
self.basis = [[0.0, 0.0, 0.0],
[0.5, 0.5, 0.5]]
offset = 0.25
elif ltype == 'sc':
self.basis = [[0.0, 0.0, 0.0]]
offset = 0.5
else:
print('unknown lattice, choices are: fcc, bcc, sc')
sys.exit(1)
for site in self.basis:
for i in range(3):
site[i] = (site[i] + offset) * scale
class lattice:
def __init__(self, ltype, scale, n):
self.ltype = ltype
self.scale = scale
self.n = n
self.c = cell(ltype, scale)
self.site = []
site = disp = [0.0, 0.0, 0.0]
for i in range(n[0]):
disp[0] = i * scale
for j in range(n[1]):
disp[1] = j * scale
for k in range(n[2]):
disp[2] = k * scale
for a in self.c.basis:
site = [ a[l] + disp[l] for l in range(3) ]
self.site.append(site)
def writexyz(self, filename):
with open(filename, 'w') as f:
f.write(str(len(self.site)) + '\n')
title = "{0} {1:15.6f} {2:15.6f} "\
"{3:15.6f}\n".format(self.ltype, self.scale*self.n[0],
self.scale*self.n[1], self.scale*self.n[2])
f.write(title)
for a in self.site:
f.write("{0:<5s} {1:15.6f} {2:15.6f} "
"{3:15.6f}\n".format('X', a[0], a[1], a[2]))
print("generated {0} sites in file {1}".format(len(self.site),
filename))
class atom:
def __init__(self, name, x, y, z):
self.name = name
self.pos = [x, y, z]
class mol:
def __init__(self, molfile):
self.at = []
try:
with open(molfile, 'r') as f:
self.natoms = int(f.readline().strip())
self.name = f.readline().strip()
for i in range(self.natoms):
tok = f.readline().strip().split()
a = atom(tok[0], \
float(tok[1]), float(tok[2]), float(tok[3]))
self.at.append(a)
except IOError:
print('error: cannot open ' + molfile)
sys.exit(1)
# find geometric center of molecule
gc = [0.0, 0.0, 0.0]
for a in self.at:
for i in range(3):
gc[i] += a.pos[i]
for i in range(3):
gc[i] /= self.natoms
# recenter molecule on gc
for a in self.at:
for i in range(3):
a.pos[i] -= gc[i]
class box:
def __init__(self, lat, m):
self.lat = lat
self.m = m
self.at = []
pos = [0.0, 0.0, 0.0]
for site in self.lat.site:
for a in self.m.at:
for i in range(3):
pos[i] = site[i] + a.pos[i]
at = atom(a.name, pos[0], pos[1], pos[2])
self.at.append(at)
def writexyz(self, filename):
with open(filename, 'w') as f:
f.write(str(len(self.at)) + '\n')
title = "{0} {1} {2:15.6f} {3:15.6f} "\
"{4:15.6f}\n".format(self.m.name, self.lat.ltype,
self.lat.scale*self.lat.n[0],
self.lat.scale*self.lat.n[1],
self.lat.scale*self.lat.n[2])
f.write(title)
for a in self.at:
f.write("{0:<5s} {1:15.6f} {2:15.6f} "\
"{3:15.6f}\n".format(a.name, a.pos[0],
a.pos[1], a.pos[2]))
print("coordinates for {0} molecules in "\
"file {1}".format(len(self.lat.site), filename))
def main():
if len(sys.argv) != 7:
print("Generate molecular coordinates on a lattice")
print("usage: lattice.py {fcc|bcc|sc} L/A nx ny nz molecule.xyz")
sys.exit(1)
ltype = sys.argv[1]
scale = float(sys.argv[2])
n = [ int(sys.argv[3]), int(sys.argv[4]), int(sys.argv[5]) ]
molfile = sys.argv[6]
lat = lattice(ltype, scale, n)
b = box(lat, mol(molfile))
b.writexyz("simbox.xyz")
if __name__ == '__main__':
main()