1 | #!@PYTHON@
|
---|
2 |
|
---|
3 | # Boxmaker 1.0
|
---|
4 | # Creates tremolo-datafiles with arbitrary size and specific density from a single input molecule,
|
---|
5 | # supporting numerous pre- and postprocessing features such as unit conversion.
|
---|
6 | # Gregor Bollerhey - bollerhe@ins.uni-bonn.de
|
---|
7 |
|
---|
8 |
|
---|
9 | import re, os, os.path, sys, operator
|
---|
10 | import pyMoleCuilder as mol
|
---|
11 |
|
---|
12 | avogadro = 6.022143e23
|
---|
13 |
|
---|
14 | class c_opt():
|
---|
15 | basename = None
|
---|
16 | tremofiledir = './'
|
---|
17 | potentialsfiledir = './'
|
---|
18 | outfilename = 'out'
|
---|
19 |
|
---|
20 | source = None
|
---|
21 | molarmass = None
|
---|
22 | density = None
|
---|
23 | temp = None
|
---|
24 |
|
---|
25 | number = '1000'
|
---|
26 |
|
---|
27 | cubicdomain = 'on'
|
---|
28 | cubiccell = 'off'
|
---|
29 | autorotate = 'off'
|
---|
30 | autodim = 'on'
|
---|
31 | postprocess = 'on'
|
---|
32 | automass = 'on'
|
---|
33 |
|
---|
34 | def update(self, name, value):
|
---|
35 | shortcuts = {'tf': 'temofiledir', 'pf': 'potentialsfiledir', 'o': 'outfilename',
|
---|
36 | 'i': 'source', 'm': 'molarmass', 'rho': 'density',
|
---|
37 | 't': 'temp', 'n': 'number', 'cd': 'cubicdomain',
|
---|
38 | 'cc': 'cubiccell', 'ar': 'autorotate', 'ad': 'autodim',
|
---|
39 | 'pp': 'postprocess', 'am': 'automass'}
|
---|
40 |
|
---|
41 | if name in shortcuts:
|
---|
42 | name = shortcuts[name]
|
---|
43 |
|
---|
44 | if name in dir(self):
|
---|
45 | exec('self.%s = "%s"' % (name, value))
|
---|
46 | else:
|
---|
47 | print 'Warning: Unknown option:', name
|
---|
48 |
|
---|
49 |
|
---|
50 | def ReadSettings(opt):
|
---|
51 | # Obtain basename
|
---|
52 | if len(sys.argv) >= 2:
|
---|
53 | opt.basename = sys.argv[1]
|
---|
54 | else:
|
---|
55 | print 'Usage: boxmaker.py <basename> [options]'
|
---|
56 | exit()
|
---|
57 |
|
---|
58 | # Read settings file
|
---|
59 | try:
|
---|
60 | with open('boxmaker.' + opt.basename) as f:
|
---|
61 | for line in f:
|
---|
62 | if len(line) > 0 and line[0] != '#':
|
---|
63 | L, S, R = line.partition('=')
|
---|
64 | opt.update(L.strip(), R.strip())
|
---|
65 | except IOError:
|
---|
66 | print 'Warning: Configuration file not readable, CLI only'
|
---|
67 |
|
---|
68 | # Parse parameters
|
---|
69 | i = 2
|
---|
70 | while i < len(sys.argv):
|
---|
71 | L = sys.argv[i]
|
---|
72 |
|
---|
73 | if L[0] in '+-':
|
---|
74 | LN = L[1:]
|
---|
75 |
|
---|
76 | if L[0] == '+':
|
---|
77 | R = 'on'
|
---|
78 | else:
|
---|
79 | R = 'off'
|
---|
80 | else:
|
---|
81 | LN = L
|
---|
82 | i += 1
|
---|
83 | R = sys.argv[i]
|
---|
84 |
|
---|
85 | opt.update(LN, R)
|
---|
86 | i += 1
|
---|
87 |
|
---|
88 |
|
---|
89 | def ReadUnits(opt):
|
---|
90 | lines = [] # The file needs to be processed twice, so we save the lines in the first run
|
---|
91 |
|
---|
92 | with open(opt.tremofiledir + opt.basename + '.tremolo') as f:
|
---|
93 | for line in f:
|
---|
94 | if len(line) > 0 and line[0] != '#':
|
---|
95 | line = line.strip()
|
---|
96 | lines.append(line)
|
---|
97 |
|
---|
98 | if 'systemofunits' in line:
|
---|
99 | L, S, SOU = line.partition('=')
|
---|
100 | SOU = SOU.strip()[:-1] # Remove semicolon
|
---|
101 |
|
---|
102 | if SOU == 'custom':
|
---|
103 | units = {}
|
---|
104 | quantities = ['length', 'mass', 'temperature']
|
---|
105 |
|
---|
106 | for quantity in quantities:
|
---|
107 | units[quantity] = [None, None] # Init with scaling factor and unit 'None'.
|
---|
108 |
|
---|
109 | for line in lines:
|
---|
110 | for quantity in quantities:
|
---|
111 | if quantity in line:
|
---|
112 | L, S, R = line.partition('=')
|
---|
113 | R = R.strip()[:-1] # Remove semicolon
|
---|
114 |
|
---|
115 | if 'scalingfactor' in line:
|
---|
116 | units[quantity][0] = float(R)
|
---|
117 | else:
|
---|
118 | units[quantity][1] = R
|
---|
119 |
|
---|
120 | elif SOU == 'kcalpermole':
|
---|
121 | units = {'length': [1.0, 'angstrom'], 'mass': [1.0, 'u'], 'temperature': [503.556, 'K']}
|
---|
122 |
|
---|
123 | elif SOU == 'evolt':
|
---|
124 | units = {'length': [1.0, 'angstrom'], 'mass': [1.0, 'u'], 'temperature': [11604.0, 'K']}
|
---|
125 |
|
---|
126 | else: # SI
|
---|
127 | units = {'length': [1.0, 'm'], 'mass': [1.0, 'kg'], 'temperature': [1.0, 'K']}
|
---|
128 |
|
---|
129 | return units
|
---|
130 |
|
---|
131 |
|
---|
132 | def ConvertUnits(have, want):
|
---|
133 | if have[0] == '!':
|
---|
134 | return float(have[1:])
|
---|
135 |
|
---|
136 | # Redo with pipes?
|
---|
137 | ret = os.system("units '%s' '%s' > temp_units_output" % (have, want))
|
---|
138 |
|
---|
139 | if ret == 0:
|
---|
140 | with open('temp_units_output') as f:
|
---|
141 | line = f.readline()
|
---|
142 |
|
---|
143 | os.system('rm temp_units_output')
|
---|
144 |
|
---|
145 | return float(line[3:-1])
|
---|
146 | else:
|
---|
147 | raise NameError('UnitError')
|
---|
148 |
|
---|
149 |
|
---|
150 | def GetSourceMolareMass(opt):
|
---|
151 | with open(opt.potentialsfiledir+opt.basename+'.potentials') as f:
|
---|
152 | potfile = f.read()
|
---|
153 |
|
---|
154 | elementmasses = dict(re.findall(r'element_name=(\w{1,2}).*?mass=([0-9.]*)', potfile))
|
---|
155 |
|
---|
156 | for key in elementmasses:
|
---|
157 | elementmasses[key] = float(elementmasses[key])
|
---|
158 |
|
---|
159 | mass_sum = 0.0
|
---|
160 |
|
---|
161 | with open('temp_source.xyz') as f:
|
---|
162 | N = int(f.readline())
|
---|
163 | comment = f.readline()
|
---|
164 |
|
---|
165 | for i in range(N):
|
---|
166 | elem = f.readline().split(None, 1)[0].strip()
|
---|
167 | mass_sum += elementmasses[elem]
|
---|
168 |
|
---|
169 | return mass_sum*avogadro
|
---|
170 |
|
---|
171 |
|
---|
172 | def UpdateSettingsAndSource(opt):
|
---|
173 | # Map boolean values
|
---|
174 | boolmap = {'on': True, 'off': False}
|
---|
175 |
|
---|
176 | for name in ['cubicdomain', 'cubiccell', 'autorotate', 'autodim', 'postprocess', 'automass']:
|
---|
177 | value = eval('opt.' + name)
|
---|
178 |
|
---|
179 | if value in boolmap:
|
---|
180 | value = boolmap[value]
|
---|
181 | else:
|
---|
182 | print 'Not a boolean value:', value
|
---|
183 | exit()
|
---|
184 |
|
---|
185 | exec('opt.' + name + '= value')
|
---|
186 |
|
---|
187 | # Convert dimensions
|
---|
188 | if opt.autodim:
|
---|
189 | units = ReadUnits(opt)
|
---|
190 |
|
---|
191 | if not opt.automass:
|
---|
192 | have = opt.molarmass
|
---|
193 | want = '%f*%s / mol' % tuple(units['mass'])
|
---|
194 | opt.molarmass = ConvertUnits(have, want)
|
---|
195 |
|
---|
196 | have = opt.density
|
---|
197 | want = '(%f*%s) ' % tuple(units['mass']) + '/ (%f*%s)**3' % tuple(units['length'])
|
---|
198 | opt.density = ConvertUnits(have, want)
|
---|
199 |
|
---|
200 | if opt.temp:
|
---|
201 | have = opt.temp
|
---|
202 | want = '%f*%s' % tuple(units['temperature'])
|
---|
203 | opt.temp = ConvertUnits(have, want)
|
---|
204 | else:
|
---|
205 | if not opt.automass:
|
---|
206 | opt.molarmass = float(opt.molarmass)
|
---|
207 |
|
---|
208 | opt.density = float(opt.density)
|
---|
209 |
|
---|
210 | if opt.temp:
|
---|
211 | opt.temp = float(opt.temp)
|
---|
212 |
|
---|
213 | # Number might be an integer or a 3-vector
|
---|
214 | nvec = opt.number.split()
|
---|
215 | if len(nvec) == 3:
|
---|
216 | opt.number = [0]*3
|
---|
217 |
|
---|
218 | for i in range(3):
|
---|
219 | opt.number[i] = int(nvec[i])
|
---|
220 | else:
|
---|
221 | opt.number = int(opt.number)
|
---|
222 |
|
---|
223 | UpdateSource(opt)
|
---|
224 |
|
---|
225 | # Automatic source mass
|
---|
226 | if opt.automass:
|
---|
227 | opt.molarmass = GetSourceMolareMass(opt)
|
---|
228 | print '======== MOLAR MASS:', opt.molarmass
|
---|
229 |
|
---|
230 |
|
---|
231 | def FindBestCube(opt):
|
---|
232 | newroot = int( round(opt.number**(1./3)) )
|
---|
233 | newnumber = newroot**3
|
---|
234 |
|
---|
235 | if newnumber != opt.number:
|
---|
236 | print 'Warning: Number changed to %d.' % newnumber
|
---|
237 |
|
---|
238 | return [newroot] * 3
|
---|
239 |
|
---|
240 |
|
---|
241 | def FindBestCuboid(opt):
|
---|
242 | n = opt.number
|
---|
243 |
|
---|
244 | # Prime factors of n
|
---|
245 | factors = []
|
---|
246 |
|
---|
247 | for i in [2, 3]:
|
---|
248 | while n % i == 0:
|
---|
249 | factors.append(i)
|
---|
250 | n /= 2
|
---|
251 |
|
---|
252 | t = 5
|
---|
253 | diff = 2
|
---|
254 |
|
---|
255 | while t*t <= n:
|
---|
256 | while n % t == 0:
|
---|
257 | factors.append(t)
|
---|
258 | n /= t
|
---|
259 |
|
---|
260 | t = t + diff
|
---|
261 | diff = 6 - diff
|
---|
262 |
|
---|
263 | if n > 1:
|
---|
264 | factors.append(n)
|
---|
265 |
|
---|
266 | # Even distribution of current biggest prime to each vector -> similar sizes
|
---|
267 | if len(factors) < 3:
|
---|
268 | print 'Warning: Not enough prime factors - falling back to cubic placement'
|
---|
269 | return FindBestCube(opt)
|
---|
270 |
|
---|
271 | factors.sort()
|
---|
272 | distri = [[],[],[]]
|
---|
273 | current = 0
|
---|
274 |
|
---|
275 | for factor in factors:
|
---|
276 | distri[current].append(factor)
|
---|
277 | current += 1
|
---|
278 | if current == 3:
|
---|
279 | current = 0
|
---|
280 |
|
---|
281 | result = [0]*3
|
---|
282 |
|
---|
283 | print '======== CUBOID USED:',
|
---|
284 |
|
---|
285 | for i in range(3):
|
---|
286 | result[i] = int( reduce(operator.mul, distri[i]) )
|
---|
287 |
|
---|
288 | print result
|
---|
289 | return result
|
---|
290 |
|
---|
291 |
|
---|
292 | def GetSourceBBabs(opt):
|
---|
293 | bbmax = [0.0]*3
|
---|
294 | bbmin = [float('inf')]*3
|
---|
295 |
|
---|
296 | s_name_ext = os.path.basename(opt.source).rsplit('.', 1)
|
---|
297 | s_namepart = s_name_ext[0]
|
---|
298 |
|
---|
299 | if len(s_name_ext) > 1:
|
---|
300 | s_ext = s_name_ext[1]
|
---|
301 | else:
|
---|
302 | s_ext = ''
|
---|
303 |
|
---|
304 | # Calculate bounding box from xyz-file
|
---|
305 | with open('temp_source.xyz') as f:
|
---|
306 | N = int(f.readline())
|
---|
307 | comment = f.readline()
|
---|
308 |
|
---|
309 | for i in xrange(N):
|
---|
310 | buf = f.readline()
|
---|
311 | xyz = buf.split()[1:]
|
---|
312 |
|
---|
313 | for i in range(3):
|
---|
314 | bbmax[i] = max(bbmax[i], float(xyz[i]))
|
---|
315 | bbmin[i] = min(bbmin[i], float(xyz[i]))
|
---|
316 |
|
---|
317 | bb = [0.0]*3
|
---|
318 |
|
---|
319 | for i in range(3):
|
---|
320 | bb[i] = abs(bbmax[i] - bbmin[i])
|
---|
321 |
|
---|
322 | return bb
|
---|
323 |
|
---|
324 |
|
---|
325 | def UpdateSource(opt):
|
---|
326 | potfilepath = opt.potentialsfiledir + opt.basename + '.potentials'
|
---|
327 | mol.ParserSetOutputFormats("xyz tremolo")
|
---|
328 | mol.ParserParseTremoloPotentials(potfilepath)
|
---|
329 | mol.MoleculeLoad(opt.source)
|
---|
330 |
|
---|
331 | #cmd = 'molecuilder -o xyz tremolo --parse-tremolo-potentials %s -i temp_source.xyz -l %s' % (potfilepath, opt.source)
|
---|
332 |
|
---|
333 | if opt.autorotate:
|
---|
334 | mol.SelectAllAtoms()
|
---|
335 | mol.RotateToPrincipalAxisSystem("0 1 0")
|
---|
336 | #cmd += ' --select-all-atoms --rotate-to-principal-axis-system "0, 1, 0"'
|
---|
337 | mol.WorldOutput('temp_source' + '.data')
|
---|
338 | mol.WorldOutput('temp_source' + '.xyz')
|
---|
339 |
|
---|
340 | #os.system(cmd)
|
---|
341 | mol.reinit()
|
---|
342 |
|
---|
343 | opt.source = 'temp_source.data'
|
---|
344 |
|
---|
345 |
|
---|
346 | # Global options with sensible default parameters
|
---|
347 | opt = c_opt()
|
---|
348 |
|
---|
349 | ReadSettings(opt)
|
---|
350 | UpdateSettingsAndSource(opt)
|
---|
351 |
|
---|
352 | if type(opt.number) == type([]):
|
---|
353 | # Number is a vector - use it without any modification
|
---|
354 | nbox = opt.number
|
---|
355 | else:
|
---|
356 | if opt.cubicdomain:
|
---|
357 | nbox = FindBestCube(opt)
|
---|
358 | else:
|
---|
359 | nbox = FindBestCuboid(opt)
|
---|
360 |
|
---|
361 | VolumePerMolecule = opt.molarmass / (avogadro * opt.density)
|
---|
362 | cell = [VolumePerMolecule**(1./3)] * 3
|
---|
363 |
|
---|
364 | if not opt.cubiccell:
|
---|
365 | try:
|
---|
366 | bb = GetSourceBBabs(opt)
|
---|
367 | print '======== BBOX:', bb
|
---|
368 | # Scaling factor - the molecules bounding box is scaled to fit the volume suiting the density
|
---|
369 | s = (VolumePerMolecule / (bb[0]*bb[1]*bb[2])) ** (1./3)
|
---|
370 |
|
---|
371 | if s < 1:
|
---|
372 | print 'Warning: Molecular cells will overlap.'
|
---|
373 |
|
---|
374 | for i in range(3):
|
---|
375 | cell[i] = bb[i]*s
|
---|
376 | except ZeroDivisionError:
|
---|
377 | print 'Warning: Singularity in bounding box, falling back to cubic cell.'
|
---|
378 |
|
---|
379 |
|
---|
380 | print '======== CELL: ', cell
|
---|
381 |
|
---|
382 | mol.CommandVerbose('0')
|
---|
383 | mol.ParserParseTremoloPotentials(opt.potentialsfiledir + opt.basename + '.potentials')
|
---|
384 | mol.WorldInput(opt.source)
|
---|
385 | mol.WorldCenterInBox('%f 0 0 %f 0 %f' % tuple(cell))
|
---|
386 | mol.WorldRepeatBox('%d %d %d' % tuple(nbox))
|
---|
387 | mol.WorldOutput(opt.outfilename + '.data')
|
---|
388 | mol.WorldOutput(opt.outfilename + '.xyz')
|
---|
389 |
|
---|
390 | domain = [0.0]*3
|
---|
391 |
|
---|
392 | for i in range(3):
|
---|
393 | domain[i] = cell[i]*nbox[i]
|
---|
394 |
|
---|
395 | print '======== DOMAIN: ', domain
|
---|
396 |
|
---|
397 | # Postprocessing
|
---|
398 |
|
---|
399 | if opt.postprocess:
|
---|
400 | with open(opt.outfilename + '.data') as f:
|
---|
401 | ofile = f.read()
|
---|
402 |
|
---|
403 | with open(opt.outfilename + '.data', 'w') as f:
|
---|
404 | f.write('# INPUTCONV shift center\n')
|
---|
405 |
|
---|
406 | if opt.temp:
|
---|
407 | f.write('# INPUTCONV temp %.4f\n' % opt.temp)
|
---|
408 |
|
---|
409 | f.write(ofile)
|
---|
410 |
|
---|
411 | os.system('rm temp_source.data temp_source.xyz')
|
---|