| 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 |     #sys.stdout.write("units '%s' '%s'\n" % (have, want))
 | 
|---|
| 138 |     ret = os.system("units '%s' '%s' > temp_units_output" % (have, want))
 | 
|---|
| 139 | 
 | 
|---|
| 140 |     if ret == 0:
 | 
|---|
| 141 |         with open('temp_units_output') as f:
 | 
|---|
| 142 |             line = f.readline()
 | 
|---|
| 143 | 
 | 
|---|
| 144 |         os.system('rm temp_units_output')
 | 
|---|
| 145 | 
 | 
|---|
| 146 |         return float(line[3:-1])
 | 
|---|
| 147 |     else:
 | 
|---|
| 148 |         raise NameError('UnitError')
 | 
|---|
| 149 |         
 | 
|---|
| 150 |         
 | 
|---|
| 151 | def GetSourceMolareMass(opt, units):
 | 
|---|
| 152 |     mol.SelectionAllAtoms()
 | 
|---|
| 153 |     mass_sum = mol.AnalysisCalculateMolarMass()
 | 
|---|
| 154 |     have = ("%f atomicmassunit" % mass_sum)
 | 
|---|
| 155 |     want = ("%f*%s" % tuple(units['mass']))
 | 
|---|
| 156 |     return ConvertUnits(have, want)*avogadro
 | 
|---|
| 157 | 
 | 
|---|
| 158 | 
 | 
|---|
| 159 | def UpdateSettingsAndSource(opt):
 | 
|---|
| 160 |     # Map boolean values
 | 
|---|
| 161 |     boolmap = {'on': True, 'off': False}
 | 
|---|
| 162 |     
 | 
|---|
| 163 |     for name in ['cubicdomain', 'cubiccell', 'autorotate', 'autodim', 'postprocess', 'automass']:
 | 
|---|
| 164 |         value = eval('opt.' + name)
 | 
|---|
| 165 |         
 | 
|---|
| 166 |         if value in boolmap:
 | 
|---|
| 167 |             value = boolmap[value]
 | 
|---|
| 168 |         else:
 | 
|---|
| 169 |             print 'Not a boolean value:', value
 | 
|---|
| 170 |             exit()
 | 
|---|
| 171 |             
 | 
|---|
| 172 |         exec('opt.' + name + '= value')
 | 
|---|
| 173 | 
 | 
|---|
| 174 |     # Convert dimensions
 | 
|---|
| 175 |     if opt.autodim:
 | 
|---|
| 176 |         units = ReadUnits(opt)
 | 
|---|
| 177 | 
 | 
|---|
| 178 |         if not opt.automass:
 | 
|---|
| 179 |             have = opt.molarmass
 | 
|---|
| 180 |             want = '%f*%s / mol' % tuple(units['mass'])
 | 
|---|
| 181 |             opt.molarmass = ConvertUnits(have, want)
 | 
|---|
| 182 | 
 | 
|---|
| 183 |         have = opt.density
 | 
|---|
| 184 |         want = '(%f*%s) ' % tuple(units['mass']) + '/ (%f*%s)**3' % tuple(units['length'])
 | 
|---|
| 185 |         opt.density = ConvertUnits(have, want)
 | 
|---|
| 186 |         
 | 
|---|
| 187 |         if opt.temp:
 | 
|---|
| 188 |             have = opt.temp
 | 
|---|
| 189 |             want = '%f*%s' % tuple(units['temperature'])
 | 
|---|
| 190 |             opt.temp = ConvertUnits(have, want)
 | 
|---|
| 191 |     else:
 | 
|---|
| 192 |         if not opt.automass:
 | 
|---|
| 193 |             opt.molarmass = float(opt.molarmass)
 | 
|---|
| 194 |         
 | 
|---|
| 195 |         opt.density = float(opt.density)
 | 
|---|
| 196 |         
 | 
|---|
| 197 |         if opt.temp:
 | 
|---|
| 198 |             opt.temp = float(opt.temp)
 | 
|---|
| 199 | 
 | 
|---|
| 200 |     # Number might be an integer or a 3-vector
 | 
|---|
| 201 |     nvec = opt.number.split()
 | 
|---|
| 202 |     if len(nvec) == 3:
 | 
|---|
| 203 |         opt.number = [0]*3
 | 
|---|
| 204 | 
 | 
|---|
| 205 |         for i in range(3):
 | 
|---|
| 206 |             opt.number[i] = int(nvec[i])
 | 
|---|
| 207 |     else:
 | 
|---|
| 208 |         opt.number = int(opt.number)
 | 
|---|
| 209 |         
 | 
|---|
| 210 |     InitialiseSource(opt)
 | 
|---|
| 211 |         
 | 
|---|
| 212 |     # Automatic source mass
 | 
|---|
| 213 |     if opt.automass:
 | 
|---|
| 214 |         opt.molarmass = GetSourceMolareMass(opt, units)
 | 
|---|
| 215 |         print '======== MOLAR MASS:', opt.molarmass
 | 
|---|
| 216 | 
 | 
|---|
| 217 | 
 | 
|---|
| 218 | def FindBestCube(opt):
 | 
|---|
| 219 |     newroot = int( round(opt.number**(1./3)) )
 | 
|---|
| 220 |     newnumber = newroot**3
 | 
|---|
| 221 | 
 | 
|---|
| 222 |     if newnumber != opt.number:
 | 
|---|
| 223 |         print 'Warning: Number changed to %d.' % newnumber
 | 
|---|
| 224 | 
 | 
|---|
| 225 |     return [newroot] * 3
 | 
|---|
| 226 | 
 | 
|---|
| 227 | 
 | 
|---|
| 228 | def FindBestCuboid(opt):
 | 
|---|
| 229 |     n = opt.number
 | 
|---|
| 230 | 
 | 
|---|
| 231 |     # Prime factors of n
 | 
|---|
| 232 |     factors = []
 | 
|---|
| 233 | 
 | 
|---|
| 234 |     for i in [2, 3]:
 | 
|---|
| 235 |         while n % i == 0:
 | 
|---|
| 236 |             factors.append(i)
 | 
|---|
| 237 |             n /= 2
 | 
|---|
| 238 | 
 | 
|---|
| 239 |     t = 5
 | 
|---|
| 240 |     diff = 2
 | 
|---|
| 241 | 
 | 
|---|
| 242 |     while t*t <= n:
 | 
|---|
| 243 |         while n % t == 0:
 | 
|---|
| 244 |             factors.append(t)
 | 
|---|
| 245 |             n /= t
 | 
|---|
| 246 | 
 | 
|---|
| 247 |         t = t + diff
 | 
|---|
| 248 |         diff = 6 - diff
 | 
|---|
| 249 | 
 | 
|---|
| 250 |     if n > 1:
 | 
|---|
| 251 |         factors.append(n)
 | 
|---|
| 252 | 
 | 
|---|
| 253 |     # Even distribution of current biggest prime to each vector -> similar sizes
 | 
|---|
| 254 |     if len(factors) < 3:
 | 
|---|
| 255 |         print 'Warning: Not enough prime factors - falling back to cubic placement'
 | 
|---|
| 256 |         return FindBestCube(opt)
 | 
|---|
| 257 | 
 | 
|---|
| 258 |     factors.sort()
 | 
|---|
| 259 |     distri = [[],[],[]]
 | 
|---|
| 260 |     current = 0
 | 
|---|
| 261 | 
 | 
|---|
| 262 |     for factor in factors:
 | 
|---|
| 263 |         distri[current].append(factor)
 | 
|---|
| 264 |         current += 1
 | 
|---|
| 265 |         if current == 3:
 | 
|---|
| 266 |             current = 0
 | 
|---|
| 267 | 
 | 
|---|
| 268 |     result = [0]*3
 | 
|---|
| 269 | 
 | 
|---|
| 270 |     print '======== CUBOID USED:',
 | 
|---|
| 271 | 
 | 
|---|
| 272 |     for i in range(3):
 | 
|---|
| 273 |         result[i] = int( reduce(operator.mul, distri[i]) )
 | 
|---|
| 274 | 
 | 
|---|
| 275 |     print result
 | 
|---|
| 276 |     return result
 | 
|---|
| 277 | 
 | 
|---|
| 278 | 
 | 
|---|
| 279 | def GetSourceBBabs(opt):
 | 
|---|
| 280 |     mol.WorldCenterOnEdge()
 | 
|---|
| 281 |     bb = mol.AnalysisCalculateBoundingBox()
 | 
|---|
| 282 |     bbmin = [bb[0], bb[2], bb[4]]
 | 
|---|
| 283 |     bbmax = [bb[1], bb[3], bb[5]]
 | 
|---|
| 284 | 
 | 
|---|
| 285 |     bb = [0.0]*3
 | 
|---|
| 286 | 
 | 
|---|
| 287 |     for i in range(3):
 | 
|---|
| 288 |         bb[i] = abs(bbmax[i] - bbmin[i])
 | 
|---|
| 289 | 
 | 
|---|
| 290 |     return bb
 | 
|---|
| 291 |     
 | 
|---|
| 292 |     
 | 
|---|
| 293 | def InitialiseSource(opt):
 | 
|---|
| 294 |     potfilepath = opt.potentialsfiledir + opt.basename + '.potentials'
 | 
|---|
| 295 |     mol.ParserParseTremoloPotentials(potfilepath)
 | 
|---|
| 296 |     mol.MoleculeLoad(opt.source)
 | 
|---|
| 297 |     
 | 
|---|
| 298 |     if opt.autorotate:
 | 
|---|
| 299 |         mol.SelectAllAtoms()
 | 
|---|
| 300 |         mol.RotateToPrincipalAxisSystem("0 1 0")
 | 
|---|
| 301 | 
 | 
|---|
| 302 | # Global options with sensible default parameters
 | 
|---|
| 303 | opt = c_opt()
 | 
|---|
| 304 | 
 | 
|---|
| 305 | ReadSettings(opt)
 | 
|---|
| 306 | UpdateSettingsAndSource(opt)
 | 
|---|
| 307 | 
 | 
|---|
| 308 | if type(opt.number) == type([]):
 | 
|---|
| 309 |     # Number is a vector - use it without any modification
 | 
|---|
| 310 |     nbox = opt.number
 | 
|---|
| 311 | else:
 | 
|---|
| 312 |     if opt.cubicdomain:
 | 
|---|
| 313 |         nbox = FindBestCube(opt)
 | 
|---|
| 314 |     else:
 | 
|---|
| 315 |         nbox = FindBestCuboid(opt)
 | 
|---|
| 316 | 
 | 
|---|
| 317 | VolumePerMolecule = opt.molarmass / (avogadro * opt.density)
 | 
|---|
| 318 | cell = [VolumePerMolecule**(1./3)] * 3
 | 
|---|
| 319 | 
 | 
|---|
| 320 | if not opt.cubiccell:
 | 
|---|
| 321 |     try:
 | 
|---|
| 322 |         bb = GetSourceBBabs(opt)
 | 
|---|
| 323 |         print '======== BBOX:', bb
 | 
|---|
| 324 |         # Scaling factor - the molecules bounding box is scaled to fit the volume suiting the density
 | 
|---|
| 325 |         s = (VolumePerMolecule / (bb[0]*bb[1]*bb[2])) ** (1./3)
 | 
|---|
| 326 | 
 | 
|---|
| 327 |         if s < 1:
 | 
|---|
| 328 |             print 'Warning: Molecular cells will overlap.'
 | 
|---|
| 329 | 
 | 
|---|
| 330 |         for i in range(3):
 | 
|---|
| 331 |             cell[i] = bb[i]*s
 | 
|---|
| 332 |     except ZeroDivisionError:
 | 
|---|
| 333 |         print 'Warning:  Singularity in bounding box, falling back to cubic cell.'
 | 
|---|
| 334 |         
 | 
|---|
| 335 | 
 | 
|---|
| 336 | print '======== CELL: ', cell
 | 
|---|
| 337 | 
 | 
|---|
| 338 | mol.CommandVerbose('0')
 | 
|---|
| 339 | mol.WorldCenterInBox('%f 0 0 %f 0 %f' % tuple(cell))
 | 
|---|
| 340 | mol.WorldRepeatBox('%d %d %d' % tuple(nbox))
 | 
|---|
| 341 | mol.WorldOutputAs(opt.outfilename + '.data')
 | 
|---|
| 342 | mol.WorldOutputAs(opt.outfilename + '.xyz')
 | 
|---|
| 343 | 
 | 
|---|
| 344 | domain = [0.0]*3
 | 
|---|
| 345 | 
 | 
|---|
| 346 | for i in range(3):
 | 
|---|
| 347 |     domain[i] = cell[i]*nbox[i]
 | 
|---|
| 348 | 
 | 
|---|
| 349 | print  '======== DOMAIN: ', domain
 | 
|---|
| 350 | 
 | 
|---|
| 351 | # Postprocessing
 | 
|---|
| 352 | 
 | 
|---|
| 353 | if opt.postprocess:
 | 
|---|
| 354 |     with open(opt.outfilename + '.data') as f:
 | 
|---|
| 355 |         ofile = f.read()
 | 
|---|
| 356 |     
 | 
|---|
| 357 |     with open(opt.outfilename + '.data', 'w') as f:
 | 
|---|
| 358 |         f.write('# INPUTCONV shift center\n')
 | 
|---|
| 359 |         
 | 
|---|
| 360 |         if opt.temp:
 | 
|---|
| 361 |             f.write('# INPUTCONV temp %.4f\n' % opt.temp)
 | 
|---|
| 362 |             
 | 
|---|
| 363 |         f.write(ofile)
 | 
|---|
| 364 |         
 | 
|---|
| 365 | os.system('rm temp_source.data temp_source.xyz')
 | 
|---|