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