gromacstopfile.py 64.1 KB
Newer Older
Peter Eastman's avatar
Peter Eastman committed
1
2
3
4
5
6
7
8
"""
gromacstopfile.py: Used for loading Gromacs top files.

This is part of the OpenMM molecular simulation toolkit originating from
Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.

9
Portions copyright (c) 2012-2023 Stanford University and the Authors.
Peter Eastman's avatar
Peter Eastman committed
10
Authors: Peter Eastman
11
Contributors: Jason Swails
Peter Eastman's avatar
Peter Eastman committed
12

Justin MacCallum's avatar
Justin MacCallum committed
13
Permission is hereby granted, free of charge, to any person obtaining a
Peter Eastman's avatar
Peter Eastman committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
31
from __future__ import absolute_import
Peter Eastman's avatar
Peter Eastman committed
32
33
34
__author__ = "Peter Eastman"
__version__ = "1.0"

35
36
from openmm.app import Topology
from openmm.app import PDBFile
37
38
39
from . import forcefield as ff
from . import element as elem
from . import amberprmtopfile as prmtop
40
41
import openmm.unit as unit
import openmm as mm
Peter Eastman's avatar
Peter Eastman committed
42
43
import math
import os
44
import re
Robert McGibbon's avatar
Fix  
Robert McGibbon committed
45
import distutils.spawn
Sunhwan Jo's avatar
Sunhwan Jo committed
46
from collections import OrderedDict, defaultdict
47
from itertools import combinations, combinations_with_replacement
48
from copy import deepcopy
Peter Eastman's avatar
Peter Eastman committed
49
50
51
52
53
54
55

HBonds = ff.HBonds
AllBonds = ff.AllBonds
HAngles = ff.HAngles

OBC2 = prmtop.OBC2

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
novarcharre = re.compile(r'\W')

def _find_all_instances_in_string(string, substr):
    """ Find indices of all instances of substr in string """
    indices = []
    idx = string.find(substr, 0)
    while idx > -1:
        indices.append(idx)
        idx = string.find(substr, idx+1)
    return indices

def _replace_defines(line, defines):
    """ Replaces defined tokens in a given line """
    if not defines: return line
    for define in reversed(defines):
        value = defines[define]
        indices = _find_all_instances_in_string(line, define)
        if not indices: continue
        # Check to see if it's inside of quotes
        inside = ''
        idx = 0
        n_to_skip = 0
        new_line = []
        for i, char in enumerate(line):
            if n_to_skip:
                n_to_skip -= 1
                continue
            if char in ('\'"'):
                if not inside:
                    inside = char
                else:
                    if inside == char:
                        inside = ''
            if idx < len(indices) and i == indices[idx]:
                if inside:
                    new_line.append(char)
                    idx += 1
                    continue
                if i == 0 or novarcharre.match(line[i-1]):
                    endidx = indices[idx] + len(define)
                    if endidx >= len(line) or novarcharre.match(line[endidx]):
                        new_line.extend(list(value))
                        n_to_skip = len(define) - 1
                        idx += 1
                        continue
                idx += 1
            new_line.append(char)
        line = ''.join(new_line)

    return line

Peter Eastman's avatar
Peter Eastman committed
107
108
class GromacsTopFile(object):
    """GromacsTopFile parses a Gromacs top file and constructs a Topology and (optionally) an OpenMM System from it."""
Justin MacCallum's avatar
Justin MacCallum committed
109

Peter Eastman's avatar
Peter Eastman committed
110
111
    class _MoleculeType(object):
        """Inner class to store information about a molecule type."""
112
113
114
        def __init__(self, name, nrexcl):
            self.name = name
            self.nrexcl = nrexcl
Peter Eastman's avatar
Peter Eastman committed
115
116
117
118
119
            self.atoms = []
            self.bonds = []
            self.angles = []
            self.dihedrals = []
            self.exclusions = []
120
            self.pairs = []
121
            self.constraints = []
122
            self.cmaps = []
Sunhwan Jo's avatar
Sunhwan Jo committed
123
            self.vsites2 = []
124
            self.vsites3 = []
125
            self.has_virtual_sites = False
Sunhwan Jo's avatar
Sunhwan Jo committed
126
            self.has_nbfix_terms = False
Justin MacCallum's avatar
Justin MacCallum committed
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
        def findExclusionsFromBonds(self, genpairs):
            """Find exclusions between atoms separated by up to nrexcl bonds if genpairs is false,
               or up to 2 bonds if genpairs is true.
            """
            bondedTo = [set() for i in range(len(self.atoms))]
            for fields in self.bonds:
                i = int(fields[0])-1
                j = int(fields[1])-1
                bondedTo[i].add(j)
                bondedTo[j].add(i)

            # Identify all neighbors of each atom with each separation.

            bondedWithSeparation = [bondedTo]
            maxBonds = self.nrexcl
            if genpairs:
                maxBonds = min(maxBonds, 2)
            for i in range(maxBonds-1):
                lastBonds = bondedWithSeparation[-1]
                newBonds = deepcopy(lastBonds)
                for atom in range(len(self.atoms)):
                    for a1 in lastBonds[atom]:
                        for a2 in bondedTo[a1]:
                            newBonds[atom].add(a2)
                bondedWithSeparation.append(newBonds)

            # Build the list of pairs.

            pairs = []
            for atom in range(len(self.atoms)):
                for otherAtom in bondedWithSeparation[-1][atom]:
                    if otherAtom > atom:
                        pairs.append((atom, otherAtom))
            return pairs

Peter Eastman's avatar
Peter Eastman committed
163
164
165
166
    def _processFile(self, file):
        append = ''
        for line in open(file):
            if line.strip().endswith('\\'):
167
                append = '%s %s' % (append, line[:line.rfind('\\')])
Peter Eastman's avatar
Peter Eastman committed
168
            else:
169
                self._processLine(append+' '+line, file)
Peter Eastman's avatar
Peter Eastman committed
170
                append = ''
Justin MacCallum's avatar
Justin MacCallum committed
171

Peter Eastman's avatar
Peter Eastman committed
172
173
    def _processLine(self, line, file):
        """Process one line from a file."""
174
175
        if ';' in line:
            line = line[:line.index(';')]
Peter Eastman's avatar
Peter Eastman committed
176
177
        stripped = line.strip()
        ignore = not all(self._ifStack)
178
        if stripped.startswith('*') or len(stripped) == 0:
Peter Eastman's avatar
Peter Eastman committed
179
180
            # A comment or empty line.
            return
Justin MacCallum's avatar
Justin MacCallum committed
181

Peter Eastman's avatar
Peter Eastman committed
182
183
184
185
186
        elif stripped.startswith('[') and not ignore:
            # The start of a category.
            if not stripped.endswith(']'):
                raise ValueError('Illegal line in .top file: '+line)
            self._currentCategory = stripped[1:-1].strip()
Justin MacCallum's avatar
Justin MacCallum committed
187

Peter Eastman's avatar
Peter Eastman committed
188
189
190
191
        elif stripped.startswith('#'):
            # A preprocessor command.
            fields = stripped.split()
            command = fields[0]
Robert McGibbon's avatar
Robert McGibbon committed
192
193
194
            if len(self._ifStack) != len(self._elseStack):
                raise RuntimeError('#if/#else stack out of sync')

Peter Eastman's avatar
Peter Eastman committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
            if command == '#include' and not ignore:
                # Locate the file to include
                name = stripped[len(command):].strip(' \t"<>')
                searchDirs = self._includeDirs+(os.path.dirname(file),)
                for dir in searchDirs:
                    file = os.path.join(dir, name)
                    if os.path.isfile(file):
                        # We found the file, so process it.
                        self._processFile(file)
                        break
                else:
                    raise ValueError('Could not locate #include file: '+name)
            elif command == '#define' and not ignore:
                # Add a value to our list of defines.
                if len(fields) < 2:
                    raise ValueError('Illegal line in .top file: '+line)
                name = fields[1]
212
                valueStart = stripped.find(name, len(command))+len(name)+1
Peter Eastman's avatar
Peter Eastman committed
213
                value = line[valueStart:].strip()
214
                value = value or '1' # Default define is 1
Peter Eastman's avatar
Peter Eastman committed
215
216
217
218
219
220
221
                self._defines[name] = value
            elif command == '#ifdef':
                # See whether this block should be ignored.
                if len(fields) < 2:
                    raise ValueError('Illegal line in .top file: '+line)
                name = fields[1]
                self._ifStack.append(name in self._defines)
Robert McGibbon's avatar
Robert McGibbon committed
222
                self._elseStack.append(False)
223
224
225
226
227
228
            elif command == '#undef':
                # Un-define a variable
                if len(fields) < 2:
                    raise ValueError('Illegal line in .top file: '+line)
                if fields[1] in self._defines:
                    self._defines.pop(fields[1])
Peter Eastman's avatar
Peter Eastman committed
229
230
231
232
233
234
            elif command == '#ifndef':
                # See whether this block should be ignored.
                if len(fields) < 2:
                    raise ValueError('Illegal line in .top file: '+line)
                name = fields[1]
                self._ifStack.append(name not in self._defines)
Robert McGibbon's avatar
Robert McGibbon committed
235
                self._elseStack.append(False)
Peter Eastman's avatar
Peter Eastman committed
236
237
238
239
240
            elif command == '#endif':
                # Pop an entry off the if stack.
                if len(self._ifStack) == 0:
                    raise ValueError('Unexpected line in .top file: '+line)
                del(self._ifStack[-1])
Robert McGibbon's avatar
Robert McGibbon committed
241
                del(self._elseStack[-1])
242
243
            elif command == '#else':
                # Reverse the last entry on the if stack
Robert McGibbon's avatar
Robert McGibbon committed
244
245
246
247
248
                if len(self._ifStack) == 0:
                    raise ValueError('Unexpected line in .top file: '+line)
                if self._elseStack[-1]:
                    raise ValueError('Unexpected line in .top file: '
                                     '#else has already been used ' + line)
249
                self._ifStack[-1] = (not self._ifStack[-1])
Robert McGibbon's avatar
Robert McGibbon committed
250
                self._elseStack[-1] = True
Justin MacCallum's avatar
Justin MacCallum committed
251

Peter Eastman's avatar
Peter Eastman committed
252
        elif not ignore:
253
254
255
256
257
            # Gromacs occasionally uses #define's to introduce specific
            # parameters for individual terms (for instance, this is how
            # ff99SB-ILDN is implemented). So make sure we do the appropriate
            # pre-processor replacements necessary
            line = _replace_defines(line, self._defines)
Peter Eastman's avatar
Peter Eastman committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
            # A line of data for the current category
            if self._currentCategory is None:
                raise ValueError('Unexpected line in .top file: '+line)
            if self._currentCategory == 'defaults':
                self._processDefaults(line)
            elif self._currentCategory == 'moleculetype':
                self._processMoleculeType(line)
            elif self._currentCategory == 'molecules':
                self._processMolecule(line)
            elif self._currentCategory == 'atoms':
                self._processAtom(line)
            elif self._currentCategory == 'bonds':
                self._processBond(line)
            elif self._currentCategory == 'angles':
                self._processAngle(line)
            elif self._currentCategory == 'dihedrals':
                self._processDihedral(line)
            elif self._currentCategory == 'exclusions':
                self._processExclusion(line)
277
278
            elif self._currentCategory == 'pairs':
                self._processPair(line)
279
280
            elif self._currentCategory == 'constraints':
                self._processConstraint(line)
281
282
            elif self._currentCategory == 'cmap':
                self._processCmap(line)
Peter Eastman's avatar
Peter Eastman committed
283
284
285
286
287
288
289
290
291
292
            elif self._currentCategory == 'atomtypes':
                self._processAtomType(line)
            elif self._currentCategory == 'bondtypes':
                self._processBondType(line)
            elif self._currentCategory == 'angletypes':
                self._processAngleType(line)
            elif self._currentCategory == 'dihedraltypes':
                self._processDihedralType(line)
            elif self._currentCategory == 'implicit_genborn_params':
                self._processImplicitType(line)
293
294
            elif self._currentCategory == 'pairtypes':
                self._processPairType(line)
295
296
            elif self._currentCategory == 'cmaptypes':
                self._processCmapType(line)
Sunhwan Jo's avatar
Sunhwan Jo committed
297
298
            elif self._currentCategory == 'nonbond_params':
                self._processNonbondType(line)
299
            elif self._currentCategory == 'virtual_sites2' or self._currentCategory == 'dummies2':
Sunhwan Jo's avatar
Sunhwan Jo committed
300
                self._processVirtualSites2(line)
301
302
303
            elif self._currentCategory == 'virtual_sites3' or self._currentCategory == 'dummies3':
                self._processVirtualSites3(line)
            elif self._currentCategory.startswith('virtual_sites') or self._currentCategory.startswith('dummies'):
304
305
306
307
                if self._currentMoleculeType is None:
                    raise ValueError('Found %s before [ moleculetype ]' %
                                     self._currentCategory)
                self._currentMoleculeType.has_virtual_sites = True
Justin MacCallum's avatar
Justin MacCallum committed
308

Peter Eastman's avatar
Peter Eastman committed
309
310
311
    def _processDefaults(self, line):
        """Process the [ defaults ] line."""
        fields = line.split()
Stephen Constable's avatar
Stephen Constable committed
312
313
314
315
316
317
        if len(fields) < 5:
            # fudgeLJ and fudgeQQ not specified, assumed 1.0 by default
            if len(fields) == 3:
                fields.append(1.0)
                fields.append(1.0)
            else:
Stephen Constable's avatar
Stephen Constable committed
318
                raise ValueError('Too few fields in [ defaults ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
319
320
        if fields[0] != '1':
            raise ValueError('Unsupported nonbonded type: '+fields[0])
321
        if not fields[1] in ('1', '2', '3'):
Peter Eastman's avatar
Peter Eastman committed
322
323
            raise ValueError('Unsupported combination rule: '+fields[1])
        if fields[2].lower() == 'no':
Jason Swails's avatar
Jason Swails committed
324
            self._genpairs = False
Peter Eastman's avatar
Peter Eastman committed
325
        self._defaults = fields
Justin MacCallum's avatar
Justin MacCallum committed
326

Peter Eastman's avatar
Peter Eastman committed
327
328
329
330
    def _processMoleculeType(self, line):
        """Process a line in the [ moleculetypes ] category."""
        fields = line.split()
        if len(fields) < 1:
Jason Swails's avatar
Jason Swails committed
331
            raise ValueError('Too few fields in [ moleculetypes ] line: '+line)
332
        type = GromacsTopFile._MoleculeType(fields[0], int(fields[1]))
Peter Eastman's avatar
Peter Eastman committed
333
334
        self._moleculeTypes[fields[0]] = type
        self._currentMoleculeType = type
Justin MacCallum's avatar
Justin MacCallum committed
335

Peter Eastman's avatar
Peter Eastman committed
336
337
338
339
    def _processMolecule(self, line):
        """Process a line in the [ molecules ] category."""
        fields = line.split()
        if len(fields) < 2:
Jason Swails's avatar
Jason Swails committed
340
            raise ValueError('Too few fields in [ molecules ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
341
        self._molecules.append((fields[0], int(fields[1])))
Justin MacCallum's avatar
Justin MacCallum committed
342

Peter Eastman's avatar
Peter Eastman committed
343
344
345
346
347
348
    def _processAtom(self, line):
        """Process a line in the [ atoms ] category."""
        if self._currentMoleculeType is None:
            raise ValueError('Found [ atoms ] section before [ moleculetype ]')
        fields = line.split()
        if len(fields) < 5:
Jason Swails's avatar
Jason Swails committed
349
            raise ValueError('Too few fields in [ atoms ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
350
        self._currentMoleculeType.atoms.append(fields)
Justin MacCallum's avatar
Justin MacCallum committed
351

Peter Eastman's avatar
Peter Eastman committed
352
353
354
355
356
357
    def _processBond(self, line):
        """Process a line in the [ bonds ] category."""
        if self._currentMoleculeType is None:
            raise ValueError('Found [ bonds ] section before [ moleculetype ]')
        fields = line.split()
        if len(fields) < 3:
Jason Swails's avatar
Jason Swails committed
358
            raise ValueError('Too few fields in [ bonds ] line: '+line)
359
360
        if fields[2] not in ('1', '2'):
                raise ValueError('Unsupported function type in [ bonds ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
361
        self._currentMoleculeType.bonds.append(fields)
Justin MacCallum's avatar
Justin MacCallum committed
362

Peter Eastman's avatar
Peter Eastman committed
363
364
365
366
367
368
    def _processAngle(self, line):
        """Process a line in the [ angles ] category."""
        if self._currentMoleculeType is None:
            raise ValueError('Found [ angles ] section before [ moleculetype ]')
        fields = line.split()
        if len(fields) < 4:
Jason Swails's avatar
Jason Swails committed
369
            raise ValueError('Too few fields in [ angles ] line: '+line)
370
        if fields[3] not in ('1', '2', '5'):
Jason Swails's avatar
Jason Swails committed
371
            raise ValueError('Unsupported function type in [ angles ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
372
        self._currentMoleculeType.angles.append(fields)
Justin MacCallum's avatar
Justin MacCallum committed
373

Peter Eastman's avatar
Peter Eastman committed
374
375
376
377
378
379
    def _processDihedral(self, line):
        """Process a line in the [ dihedrals ] category."""
        if self._currentMoleculeType is None:
            raise ValueError('Found [ dihedrals ] section before [ moleculetype ]')
        fields = line.split()
        if len(fields) < 5:
Jason Swails's avatar
Jason Swails committed
380
            raise ValueError('Too few fields in [ dihedrals ] line: '+line)
381
        if fields[4] not in ('1', '2', '3', '4', '5', '9'):
Jason Swails's avatar
Jason Swails committed
382
            raise ValueError('Unsupported function type in [ dihedrals ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
383
        self._currentMoleculeType.dihedrals.append(fields)
Justin MacCallum's avatar
Justin MacCallum committed
384

Peter Eastman's avatar
Peter Eastman committed
385
386
387
388
389
390
    def _processExclusion(self, line):
        """Process a line in the [ exclusions ] category."""
        if self._currentMoleculeType is None:
            raise ValueError('Found [ exclusions ] section before [ moleculetype ]')
        fields = line.split()
        if len(fields) < 2:
Jason Swails's avatar
Jason Swails committed
391
            raise ValueError('Too few fields in [ exclusions ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
392
        self._currentMoleculeType.exclusions.append(fields)
Justin MacCallum's avatar
Justin MacCallum committed
393

394
395
396
397
398
399
    def _processPair(self, line):
        """Process a line in the [ pairs ] category."""
        if self._currentMoleculeType is None:
            raise ValueError('Found [ pairs ] section before [ moleculetype ]')
        fields = line.split()
        if len(fields) < 3:
Jason Swails's avatar
Jason Swails committed
400
            raise ValueError('Too few fields in [ pairs ] line: '+line)
401
        if fields[2] != '1':
Jason Swails's avatar
Jason Swails committed
402
            raise ValueError('Unsupported function type in [ pairs ] line: '+line)
403
        self._currentMoleculeType.pairs.append(fields)
Justin MacCallum's avatar
Justin MacCallum committed
404

405
406
407
408
409
410
411
412
413
    def _processConstraint(self, line):
        """Process a line in the [ constraints ] category."""
        if self._currentMoleculeType is None:
            raise ValueError('Found [ constraints ] section before [ moleculetype ]')
        fields = line.split()
        if len(fields) < 4:
            raise ValueError('Too few fields in [ constraints ] line: '+line)
        self._currentMoleculeType.constraints.append(fields)

414
415
416
417
418
419
    def _processCmap(self, line):
        """Process a line in the [ cmaps ] category."""
        if self._currentMoleculeType is None:
            raise ValueError('Found [ cmap ] section before [ moleculetype ]')
        fields = line.split()
        if len(fields) < 6:
Jason Swails's avatar
Jason Swails committed
420
            raise ValueError('Too few fields in [ cmap ] line: '+line)
421
        self._currentMoleculeType.cmaps.append(fields)
Justin MacCallum's avatar
Justin MacCallum committed
422

Peter Eastman's avatar
Peter Eastman committed
423
424
425
    def _processAtomType(self, line):
        """Process a line in the [ atomtypes ] category."""
        fields = line.split()
426
        if len(fields) < 6:
Jason Swails's avatar
Jason Swails committed
427
            raise ValueError('Too few fields in [ atomtypes ] line: '+line)
428
429
430
431
        if len(fields[3]) == 1:
            # Bonded type and atomic number are both missing.
            fields.insert(1, None)
            fields.insert(1, None)
peastman's avatar
peastman committed
432
        elif len(fields[4]) == 1 and fields[4].isalpha():
433
434
435
436
437
438
            if fields[1][0].isalpha():
                # Atomic number is missing.
                fields.insert(2, None)
            else:
                # Bonded type is missing.
                fields.insert(1, None)
Peter Eastman's avatar
Peter Eastman committed
439
        self._atomTypes[fields[0]] = fields
Justin MacCallum's avatar
Justin MacCallum committed
440

Peter Eastman's avatar
Peter Eastman committed
441
442
443
444
    def _processBondType(self, line):
        """Process a line in the [ bondtypes ] category."""
        fields = line.split()
        if len(fields) < 5:
Jason Swails's avatar
Jason Swails committed
445
            raise ValueError('Too few fields in [ bondtypes ] line: '+line)
446
        if fields[2] not in ('1', '2'):
Jason Swails's avatar
Jason Swails committed
447
            raise ValueError('Unsupported function type in [ bondtypes ] line: '+line)
448
        self._bondTypes[tuple(fields[:3])] = fields
Justin MacCallum's avatar
Justin MacCallum committed
449

Peter Eastman's avatar
Peter Eastman committed
450
451
452
453
    def _processAngleType(self, line):
        """Process a line in the [ angletypes ] category."""
        fields = line.split()
        if len(fields) < 6:
Jason Swails's avatar
Jason Swails committed
454
            raise ValueError('Too few fields in [ angletypes ] line: '+line)
455
        if fields[3] not in ('1', '2', '5'):
Jason Swails's avatar
Jason Swails committed
456
            raise ValueError('Unsupported function type in [ angletypes ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
457
        self._angleTypes[tuple(fields[:3])] = fields
Justin MacCallum's avatar
Justin MacCallum committed
458

Peter Eastman's avatar
Peter Eastman committed
459
460
461
    def _processDihedralType(self, line):
        """Process a line in the [ dihedraltypes ] category."""
        fields = line.split()
462
463
464
465
        if len(fields[2]) == 1 and fields[2].isdigit():
            # The third field contains the function type, meaning only two atom types are specified.
            # Interpret them as the two inner ones.
            fields = ['X', fields[0], fields[1], 'X']+fields[2:]
Peter Eastman's avatar
Peter Eastman committed
466
        if len(fields) < 7:
Jason Swails's avatar
Jason Swails committed
467
            raise ValueError('Too few fields in [ dihedraltypes ] line: '+line)
468
        if fields[4] not in ('1', '2', '3', '4', '5', '9'):
Jason Swails's avatar
Jason Swails committed
469
            raise ValueError('Unsupported function type in [ dihedraltypes ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
470
471
472
473
474
475
        key = tuple(fields[:5])
        if fields[4] == '9' and key in self._dihedralTypes:
            # There are multiple dihedrals defined for these atom types.
            self._dihedralTypes[key].append(fields)
        else:
            self._dihedralTypes[key] = [fields]
Justin MacCallum's avatar
Justin MacCallum committed
476

Peter Eastman's avatar
Peter Eastman committed
477
478
479
480
    def _processImplicitType(self, line):
        """Process a line in the [ implicit_genborn_params ] category."""
        fields = line.split()
        if len(fields) < 6:
Jason Swails's avatar
Jason Swails committed
481
            raise ValueError('Too few fields in [ implicit_genborn_params ] line: '+line)
Peter Eastman's avatar
Peter Eastman committed
482
        self._implicitTypes[fields[0]] = fields
Justin MacCallum's avatar
Justin MacCallum committed
483

484
485
486
487
    def _processPairType(self, line):
        """Process a line in the [ pairtypes ] category."""
        fields = line.split()
        if len(fields) < 5:
Jason Swails's avatar
Jason Swails committed
488
            raise ValueError('Too few fields in [ pairtypes] line: '+line)
489
        if fields[2] != '1':
Jason Swails's avatar
Jason Swails committed
490
            raise ValueError('Unsupported function type in [ pairtypes ] line: '+line)
491
        self._pairTypes[tuple(fields[:2])] = fields
Justin MacCallum's avatar
Justin MacCallum committed
492

493
494
495
496
    def _processCmapType(self, line):
        """Process a line in the [ cmaptypes ] category."""
        fields = line.split()
        if len(fields) < 8 or len(fields) < 8+int(fields[6])*int(fields[7]):
Jason Swails's avatar
Jason Swails committed
497
            raise ValueError('Too few fields in [ cmaptypes ] line: '+line)
498
        if fields[5] != '1':
Jason Swails's avatar
Jason Swails committed
499
            raise ValueError('Unsupported function type in [ cmaptypes ] line: '+line)
500
        self._cmapTypes[tuple(fields[:5])] = fields
Justin MacCallum's avatar
Justin MacCallum committed
501

Sunhwan Jo's avatar
Sunhwan Jo committed
502
503
504
505
506
507
508
509
510
511
512
513
514
515
    def _processNonbondType(self, line):
        """Process a line in the [ nonbond_params ] category."""
        fields = line.split()
        if len(fields) < 5:
            raise ValueError('Too few fields in [ nonbond_params ] line: '+line)
        if fields[2] != '1':
            raise ValueError('Unsupported function type in [ nonbond_params ] line: '+line)
        self._nonbondTypes[tuple(sorted(fields[:2]))] = fields

    def _processVirtualSites2(self, line):
        """Process a line in the [ virtual_sites2 ] category."""
        fields = line.split()
        if len(fields) < 5:
            raise ValueError('Too few fields in [ virtual_sites2 ] line: ' + line)
516
517
        if fields[3] != '1':
            raise ValueError('Unsupported function type in [ virtual_sites2 ] line: '+line)
Sunhwan Jo's avatar
Sunhwan Jo committed
518
519
        self._currentMoleculeType.vsites2.append(fields[:5])

520
521
522
523
524
525
526
527
528
    def _processVirtualSites3(self, line):
        """Process a line in the [ virtual_sites3 ] category."""
        fields = line.split()
        if len(fields) < 7:
            raise ValueError('Too few fields in [ virtual_sites3 ] line: ' + line)
        if fields[4] != '1':
            raise ValueError('Unsupported function type in [ virtual_sites3 ] line: '+line)
        self._currentMoleculeType.vsites3.append(fields)

529
    def __init__(self, file, periodicBoxVectors=None, unitCellDimensions=None, includeDir=None, defines=None):
Peter Eastman's avatar
Peter Eastman committed
530
        """Load a top file.
Justin MacCallum's avatar
Justin MacCallum committed
531

532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
        Parameters
        ----------
        file : str
            the name of the file to load
        periodicBoxVectors : tuple of Vec3=None
            the vectors defining the periodic box
        unitCellDimensions : Vec3=None
            the dimensions of the crystallographic unit cell.  For
            non-rectangular unit cells, specify periodicBoxVectors instead.
        includeDir : string=None
            A directory in which to look for other files included from the
            top file. If not specified, we will attempt to locate a gromacs
            installation on your system. When gromacs is installed in
            /usr/local, this will resolve to /usr/local/gromacs/share/gromacs/top
        defines : dict={}
            preprocessor definitions that should be predefined when parsing the file
Peter Eastman's avatar
Peter Eastman committed
548
         """
549
550
        if includeDir is None:
            includeDir = _defaultGromacsIncludeDir()
Peter Eastman's avatar
Peter Eastman committed
551
        self._includeDirs = (os.path.dirname(file), includeDir)
Robert McGibbon's avatar
Robert McGibbon committed
552
553
554
555
        # Most of the gromacs water itp files for different forcefields,
        # unless the preprocessor #define FLEXIBLE is given, don't define
        # bonds between the water hydrogen and oxygens, but only give the
        # constraint distances and exclusions.
556
557
        self._defines = OrderedDict()
        self._defines['FLEXIBLE'] = True
Jason Swails's avatar
Jason Swails committed
558
        self._genpairs = True
Robert McGibbon's avatar
Robert McGibbon committed
559
        if defines is not None:
560
561
            for define, value in defines.iteritems():
                self._defines[define] = value
Justin MacCallum's avatar
Justin MacCallum committed
562

Peter Eastman's avatar
Peter Eastman committed
563
        # Parse the file.
Justin MacCallum's avatar
Justin MacCallum committed
564

Peter Eastman's avatar
Peter Eastman committed
565
566
        self._currentCategory = None
        self._ifStack = []
Robert McGibbon's avatar
Robert McGibbon committed
567
        self._elseStack = []
Peter Eastman's avatar
Peter Eastman committed
568
569
570
571
572
573
574
575
        self._moleculeTypes = {}
        self._molecules = []
        self._currentMoleculeType = None
        self._atomTypes = {}
        self._bondTypes= {}
        self._angleTypes = {}
        self._dihedralTypes = {}
        self._implicitTypes = {}
576
        self._pairTypes = {}
577
        self._cmapTypes = {}
Sunhwan Jo's avatar
Sunhwan Jo committed
578
        self._nonbondTypes = {}
Peter Eastman's avatar
Peter Eastman committed
579
        self._processFile(file)
Justin MacCallum's avatar
Justin MacCallum committed
580

Peter Eastman's avatar
Peter Eastman committed
581
        # Create the Topology from it.
Justin MacCallum's avatar
Justin MacCallum committed
582

Peter Eastman's avatar
Peter Eastman committed
583
584
585
        top = Topology()
        ## The Topology read from the prmtop file
        self.topology = top
586
587
588
589
590
591
        if periodicBoxVectors is not None:
            if unitCellDimensions is not None:
                raise ValueError("specify either periodicBoxVectors or unitCellDimensions, but not both")
            top.setPeriodicBoxVectors(periodicBoxVectors)
        else:
            top.setUnitCellDimensions(unitCellDimensions)
Peter Eastman's avatar
Peter Eastman committed
592
593
594
595
596
        PDBFile._loadNameReplacementTables()
        for moleculeName, moleculeCount in self._molecules:
            if moleculeName not in self._moleculeTypes:
                raise ValueError("Unknown molecule type: "+moleculeName)
            moleculeType = self._moleculeTypes[moleculeName]
597
598
            if moleculeCount > 0 and moleculeType.has_virtual_sites:
                raise ValueError('Virtual sites not yet supported by Gromacs parsers')
Justin MacCallum's avatar
Justin MacCallum committed
599

Peter Eastman's avatar
Peter Eastman committed
600
            # Create the specified number of molecules of this type.
Justin MacCallum's avatar
Justin MacCallum committed
601

Peter Eastman's avatar
Peter Eastman committed
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
            for i in range(moleculeCount):
                atoms = []
                lastResidue = None
                c = top.addChain()
                for index, fields in enumerate(moleculeType.atoms):
                    resNumber = fields[2]
                    if resNumber != lastResidue:
                        lastResidue = resNumber
                        resName = fields[3]
                        if resName in PDBFile._residueNameReplacements:
                            resName = PDBFile._residueNameReplacements[resName]
                        r = top.addResidue(resName, c)
                        if resName in PDBFile._atomNameReplacements:
                            atomReplacements = PDBFile._atomNameReplacements[resName]
                        else:
                            atomReplacements = {}
                    atomName = fields[4]
                    if atomName in atomReplacements:
                        atomName = atomReplacements[atomName]
Justin MacCallum's avatar
Justin MacCallum committed
621

622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
                    # Try to determine the element.

                    atomicNumber = self._atomTypes[fields[1]][2]
                    if atomicNumber is None:
                        # Try to guess the element from the name.
                        upper = atomName.upper()
                        if upper.startswith('CL'):
                            element = elem.chlorine
                        elif upper.startswith('NA'):
                            element = elem.sodium
                        elif upper.startswith('MG'):
                            element = elem.magnesium
                        else:
                            try:
                                element = elem.get_by_symbol(atomName[0])
                            except KeyError:
                                element = None
                    elif atomicNumber == '0':
                        element = None
Peter Eastman's avatar
Peter Eastman committed
641
                    else:
642
                        element = elem.Element.getByAtomicNumber(int(atomicNumber))
Peter Eastman's avatar
Peter Eastman committed
643
                    atoms.append(top.addAtom(atomName, element, r))
Justin MacCallum's avatar
Justin MacCallum committed
644

Peter Eastman's avatar
Peter Eastman committed
645
                # Add bonds to the topology
Justin MacCallum's avatar
Justin MacCallum committed
646

Peter Eastman's avatar
Peter Eastman committed
647
648
649
650
                for fields in moleculeType.bonds:
                    top.addBond(atoms[int(fields[0])-1], atoms[int(fields[1])-1])

    def createSystem(self, nonbondedMethod=ff.NoCutoff, nonbondedCutoff=1.0*unit.nanometer,
651
652
                     constraints=None, rigidWater=True, implicitSolvent=None, soluteDielectric=1.0, solventDielectric=78.5,
                     ewaldErrorTolerance=0.0005, removeCMMotion=True, hydrogenMass=None, switchDistance=None):
Robert McGibbon's avatar
Robert McGibbon committed
653
        """Construct an OpenMM System representing the topology described by this
654
        top file.
Robert McGibbon's avatar
Robert McGibbon committed
655
656
657
658
659

        Parameters
        ----------
        nonbondedMethod : object=NoCutoff
            The method to use for nonbonded interactions.  Allowed values are
Peter Eastman's avatar
Peter Eastman committed
660
            NoCutoff, CutoffNonPeriodic, CutoffPeriodic, Ewald, PME, or LJPME.
Robert McGibbon's avatar
Robert McGibbon committed
661
662
663
664
665
        nonbondedCutoff : distance=1*nanometer
            The cutoff distance to use for nonbonded interactions
        constraints : object=None
            Specifies which bonds and angles should be implemented with
            constraints. Allowed values are None, HBonds, AllBonds, or HAngles.
666
667
            Regardless of this value, constraints that are explicitly specified
            in the top file will always be included.
Robert McGibbon's avatar
Robert McGibbon committed
668
669
670
671
672
        rigidWater : boolean=True
            If true, water molecules will be fully rigid regardless of the value
            passed for the constraints argument
        implicitSolvent : object=None
            If not None, the implicit solvent model to use.  The only allowed
673
674
675
            value is OBC2.  This option is deprecated, since Gromacs 2019 and later
            no longer support implicit solvent.  It will be removed in a future
            release.
Robert McGibbon's avatar
Robert McGibbon committed
676
677
678
679
680
681
        soluteDielectric : float=1.0
            The solute dielectric constant to use in the implicit solvent model.
        solventDielectric : float=78.5
            The solvent dielectric constant to use in the implicit solvent
            model.
        ewaldErrorTolerance : float=0.0005
Stephen Constable's avatar
Stephen Constable committed
682
            The error tolerance to use if nonbondedMethod is Ewald, PME or LJPME.
Robert McGibbon's avatar
Robert McGibbon committed
683
684
685
686
687
        removeCMMotion : boolean=True
            If true, a CMMotionRemover will be added to the System
        hydrogenMass : mass=None
            The mass to use for hydrogen atoms bound to heavy atoms.  Any mass
            added to a hydrogen is subtracted from the heavy atom to keep their
688
689
            total mass the same.  If rigidWater is used to make water molecules
            rigid, then water hydrogens are not altered.
690
691
692
        switchDistance : float=None
            The distance at which the potential energy switching function is turned on for
            Lennard-Jones interactions. If this is None, no switching function will be used.
Robert McGibbon's avatar
Robert McGibbon committed
693
694
695

        Returns
        -------
696
        System
Robert McGibbon's avatar
Robert McGibbon committed
697
             the newly created System
Peter Eastman's avatar
Peter Eastman committed
698
        """
699
700
701
702
703
704
705
706
707
708
709

        # Build a list of atom types for NBFIX

        atom_types = []
        for moleculeName, moleculeCount in self._molecules:
            moleculeType = self._moleculeTypes[moleculeName]
            for _ in range(moleculeCount):
                for atom in moleculeType.atoms:
                    atom_types.append(atom[1])
        has_nbfix_terms = any([pair in self._nonbondTypes for pair in combinations_with_replacement(sorted(set(atom_types)), 2)])

Peter Eastman's avatar
Peter Eastman committed
710
        # Create the System.
Justin MacCallum's avatar
Justin MacCallum committed
711

Peter Eastman's avatar
Peter Eastman committed
712
        sys = mm.System()
peastman's avatar
peastman committed
713
714
715
        boxVectors = self.topology.getPeriodicBoxVectors()
        if boxVectors is not None:
            sys.setDefaultPeriodicBoxVectors(*boxVectors)
Peter Eastman's avatar
Peter Eastman committed
716
        elif nonbondedMethod in (ff.CutoffPeriodic, ff.Ewald, ff.PME, ff.LJPME):
717
            raise ValueError('Illegal nonbonded method for a non-periodic system')
718
        nb = mm.NonbondedForce()
Peter Eastman's avatar
Peter Eastman committed
719
        sys.addForce(nb)
720
721
722
723
724
725
        lj = None
        if has_nbfix_terms:
            lj = mm.CustomNonbondedForce('(a/r6)^2-b/r6; r6=r^6; a=acoef(type1, type2); b=bcoef(type1, type2)')
            lj.addPerParticleParameter('type')
            sys.addForce(lj)
        elif self._defaults[1] in ('1', '3'):
726
            lj = mm.CustomNonbondedForce('A1*A2/r^12-C1*C2/r^6')
727
728
729
            lj.addPerParticleParameter('C')
            lj.addPerParticleParameter('A')
            sys.addForce(lj)
Peter Eastman's avatar
Peter Eastman committed
730
731
732
733
734
        if implicitSolvent is OBC2:
            gb = mm.GBSAOBCForce()
            gb.setSoluteDielectric(soluteDielectric)
            gb.setSolventDielectric(solventDielectric)
            sys.addForce(gb)
735
            nb.setReactionFieldDielectric(1.0)
Peter Eastman's avatar
Peter Eastman committed
736
737
        elif implicitSolvent is not None:
            raise ValueError('Illegal value for implicitSolvent')
738
739
        bonds = {}
        angles = {}
Peter Eastman's avatar
Peter Eastman committed
740
741
        periodic = None
        rb = None
742
743
744
        harmonicTorsion = None
        cmap = None
        mapIndices = {}
Peter Eastman's avatar
Peter Eastman committed
745
746
        bondIndices = []
        topologyAtoms = list(self.topology.atoms())
747
        exclusions = []
Stephen Constable's avatar
Stephen Constable committed
748
749
750
        pairs = []
        fudgeQQ = float(self._defaults[4])
        fudgeLJ = float(self._defaults[3])
Robert McGibbon's avatar
Robert McGibbon committed
751

752
        # Build a lookup table to let us process dihedrals more quickly.
Robert McGibbon's avatar
Robert McGibbon committed
753

754
755
756
757
758
759
760
761
762
763
764
765
766
        dihedralTypeTable = {}
        for key in self._dihedralTypes:
            if key[1] != 'X' and key[2] != 'X':
                if (key[1], key[2]) not in dihedralTypeTable:
                    dihedralTypeTable[(key[1], key[2])] = []
                dihedralTypeTable[(key[1], key[2])].append(key)
                if (key[2], key[1]) not in dihedralTypeTable:
                    dihedralTypeTable[(key[2], key[1])] = []
                dihedralTypeTable[(key[2], key[1])].append(key)
        wildcardDihedralTypes = []
        for key in self._dihedralTypes:
            if key[1] == 'X' or key[2] == 'X':
                wildcardDihedralTypes.append(key)
767
                for types in dihedralTypeTable.values():
768
                    types.append(key)
Justin MacCallum's avatar
Justin MacCallum committed
769

Sunhwan Jo's avatar
Sunhwan Jo committed
770
771
772
773
774
775
776
777
        if has_nbfix_terms:
            # Build a lookup table and angle/dihedral indices list to
            # let us handle exclusion manually.
            angleIndices = []
            torsionIndices = []
            atom_partners = defaultdict(lambda : defaultdict(set))
            atom_charges = []

Peter Eastman's avatar
Peter Eastman committed
778
        # Loop over molecules and create the specified number of each type.
Justin MacCallum's avatar
Justin MacCallum committed
779

Peter Eastman's avatar
Peter Eastman committed
780
781
        for moleculeName, moleculeCount in self._molecules:
            moleculeType = self._moleculeTypes[moleculeName]
782
            exclusionsFromBonds = moleculeType.findExclusionsFromBonds(self._genpairs)
Peter Eastman's avatar
Peter Eastman committed
783
            for i in range(moleculeCount):
Justin MacCallum's avatar
Justin MacCallum committed
784

Peter Eastman's avatar
Peter Eastman committed
785
                # Record the types of all atoms.
Justin MacCallum's avatar
Justin MacCallum committed
786

Peter Eastman's avatar
Peter Eastman committed
787
788
789
                baseAtomIndex = sys.getNumParticles()
                atomTypes = [atom[1] for atom in moleculeType.atoms]
                try:
790
                    bondedTypes = [self._atomTypes[t][1] for t in atomTypes]
Peter Eastman's avatar
Peter Eastman committed
791
                except KeyError as e:
792
                    raise ValueError('Unknown atom type: ' + e.message)
793
                bondedTypes = [b if b is not None else a for a, b in zip(atomTypes, bondedTypes)]
Justin MacCallum's avatar
Justin MacCallum committed
794

Peter Eastman's avatar
Peter Eastman committed
795
                # Add atoms.
Justin MacCallum's avatar
Justin MacCallum committed
796

Peter Eastman's avatar
Peter Eastman committed
797
798
799
800
                for fields in moleculeType.atoms:
                    if len(fields) >= 8:
                        mass = float(fields[7])
                    else:
801
                        mass = float(self._atomTypes[fields[1]][3])
Peter Eastman's avatar
Peter Eastman committed
802
                    sys.addParticle(mass)
Justin MacCallum's avatar
Justin MacCallum committed
803

Peter Eastman's avatar
Peter Eastman committed
804
                # Add bonds.
Justin MacCallum's avatar
Justin MacCallum committed
805

Peter Eastman's avatar
Peter Eastman committed
806
807
808
                atomBonds = [{} for x in range(len(moleculeType.atoms))]
                for fields in moleculeType.bonds:
                    atoms = [int(x)-1 for x in fields[:2]]
809
                    types = tuple(bondedTypes[i] for i in atoms)
810
811
812
                    bondType = fields[2]
                    reversedTypes = types[::-1]+(bondType,)
                    types = types+(bondType,)
Peter Eastman's avatar
Peter Eastman committed
813
814
815
816
                    if len(fields) >= 5:
                        params = fields[3:5]
                    elif types in self._bondTypes:
                        params = self._bondTypes[types][3:5]
817
818
                    elif reversedTypes in self._bondTypes:
                        params = self._bondTypes[reversedTypes][3:5]
Peter Eastman's avatar
Peter Eastman committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
                    else:
                        raise ValueError('No parameters specified for bond: '+fields[0]+', '+fields[1])
                    # Decide whether to use a constraint or a bond.
                    useConstraint = False
                    if rigidWater and topologyAtoms[baseAtomIndex+atoms[0]].residue.name == 'HOH':
                        useConstraint = True
                    if constraints in (AllBonds, HAngles):
                        useConstraint = True
                    elif constraints is HBonds:
                        elements = [topologyAtoms[baseAtomIndex+i].element for i in atoms]
                        if elem.hydrogen in elements:
                            useConstraint = True
                    # Add the bond or constraint.
                    length = float(params[0])
                    if useConstraint:
                        sys.addConstraint(baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], length)
835
836
837
838
839
840
841
842
843
844
845
846
847
                    elif bondType == '1':
                        if bondType not in bonds:
                            bonds[bondType] = mm.HarmonicBondForce()
                            sys.addForce(bonds[bondType])
                        bonds[bondType].addBond(baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], length, float(params[1]))
                    elif bondType == '2':
                        if bondType not in bonds:
                            bonds[bondType] = mm.CustomBondForce('0.25*k*(r^2-r0^2)^2')
                            bonds[bondType].addPerBondParameter('r0')
                            bonds[bondType].addPerBondParameter('k')
                            bonds[bondType].setName('GROMOSBondForce')
                            sys.addForce(bonds[bondType])
                        bonds[bondType].addBond(baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], (length, float(params[1])))
Peter Eastman's avatar
Peter Eastman committed
848
                    else:
849
                        raise ValueError('Internal error: bondType has unexpected value: '+bondType)
Peter Eastman's avatar
Peter Eastman committed
850
851
852
                    # Record information that will be needed for constraining angles.
                    atomBonds[atoms[0]][atoms[1]] = length
                    atomBonds[atoms[1]][atoms[0]] = length
Justin MacCallum's avatar
Justin MacCallum committed
853

Peter Eastman's avatar
Peter Eastman committed
854
                # Add angles.
Justin MacCallum's avatar
Justin MacCallum committed
855

Peter Eastman's avatar
Peter Eastman committed
856
857
858
                degToRad = math.pi/180
                for fields in moleculeType.angles:
                    atoms = [int(x)-1 for x in fields[:3]]
859
                    types = tuple(bondedTypes[i] for i in atoms)
860
                    angleType = fields[3]
Peter Eastman's avatar
Peter Eastman committed
861
                    if len(fields) >= 6:
862
                        params = fields[4:]
Peter Eastman's avatar
Peter Eastman committed
863
                    elif types in self._angleTypes:
864
                        params = self._angleTypes[types][4:]
Peter Eastman's avatar
Peter Eastman committed
865
                    elif types[::-1] in self._angleTypes:
866
                        params = self._angleTypes[types[::-1]][4:]
Peter Eastman's avatar
Peter Eastman committed
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
                    else:
                        raise ValueError('No parameters specified for angle: '+fields[0]+', '+fields[1]+', '+fields[2])
                    # Decide whether to use a constraint or a bond.
                    useConstraint = False
                    if rigidWater and topologyAtoms[baseAtomIndex+atoms[0]].residue.name == 'HOH':
                        useConstraint = True
                    if constraints is HAngles:
                        elements = [topologyAtoms[baseAtomIndex+i].element for i in atoms]
                        if elements[0] == elem.hydrogen and elements[2] == elem.hydrogen:
                            useConstraint = True
                        elif elements[1] == elem.oxygen and (elements[0] == elem.hydrogen or elements[2] == elem.hydrogen):
                            useConstraint = True
                    # Add the bond or constraint.
                    theta = float(params[0])*degToRad
                    if useConstraint:
                        # Compute the distance between atoms and add a constraint
                        if atoms[0] in atomBonds[atoms[1]] and atoms[2] in atomBonds[atoms[1]]:
                            l1 = atomBonds[atoms[1]][atoms[0]]
                            l2 = atomBonds[atoms[1]][atoms[2]]
                            length = math.sqrt(l1*l1 + l2*l2 - 2*l1*l2*math.cos(theta))
                            sys.addConstraint(baseAtomIndex+atoms[0], baseAtomIndex+atoms[2], length)
                    else:
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
                        if angleType in ('1', '5'):
                            if angleType not in angles:
                                angles[angleType] = mm.HarmonicAngleForce()
                                sys.addForce(angles[angleType])
                            angles[angleType].addAngle(baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], theta, float(params[1]))
                            if angleType == '5':
                                # This is a Urey-Bradley term, so also add the bond.
                                if '1' not in bonds:
                                    bonds['1'] = mm.HarmonicBondForce()
                                    sys.addForce(bonds['1'])
                                k = float(params[3])
                                if k != 0:
                                    bonds['1'].addBond(baseAtomIndex + atoms[0], baseAtomIndex + atoms[2], float(params[2]), k)
                        elif angleType == '2':
                            if angleType not in angles:
                                angles[angleType] = mm.CustomAngleForce('0.5*k*(cos(theta)-cos(theta0))^2')
                                angles[angleType].addPerAngleParameter('theta0')
                                angles[angleType].addPerAngleParameter('k')
                                angles[angleType].setName('GROMOSAngleForce')
                                sys.addForce(angles[angleType])
                            angles[angleType].addAngle(baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], (theta, float(params[1])))
                        else:
                            raise ValueError('Internal error: angleType has unexpected value: '+angleType)
912

Peter Eastman's avatar
Peter Eastman committed
913
                # Add torsions.
Justin MacCallum's avatar
Justin MacCallum committed
914

Peter Eastman's avatar
Peter Eastman committed
915
916
                for fields in moleculeType.dihedrals:
                    atoms = [int(x)-1 for x in fields[:4]]
917
                    types = tuple(bondedTypes[i] for i in atoms)
Peter Eastman's avatar
Peter Eastman committed
918
919
920
                    dihedralType = fields[4]
                    reversedTypes = types[::-1]+(dihedralType,)
                    types = types+(dihedralType,)
921
                    if (dihedralType in ('1', '4', '5', '9') and len(fields) > 7) or (dihedralType == '3' and len(fields) > 10) or (dihedralType == '2' and len(fields) > 6):
922
                        paramsList = [fields]
Peter Eastman's avatar
Peter Eastman committed
923
924
925
                    else:
                        # Look for a matching dihedral type.
                        paramsList = None
926
927
928
929
930
                        if (types[1], types[2]) in dihedralTypeTable:
                            dihedralTypes = dihedralTypeTable[(types[1], types[2])]
                        else:
                            dihedralTypes = wildcardDihedralTypes
                        for key in dihedralTypes:
Peter Eastman's avatar
Peter Eastman committed
931
932
933
934
935
936
                            if all(a == b or a == 'X' for a, b in zip(key, types)) or all(a == b or a == 'X' for a, b in zip(key, reversedTypes)):
                                paramsList = self._dihedralTypes[key]
                                if 'X' not in key:
                                    break
                        if paramsList is None:
                            raise ValueError('No parameters specified for dihedral: '+fields[0]+', '+fields[1]+', '+fields[2]+', '+fields[3])
937
938
939
940
941
942
943
944
                    for params in paramsList:
                        if dihedralType in ('1', '4', '9'):
                            # Periodic torsion
                            k = float(params[6])
                            if k != 0:
                                if periodic is None:
                                    periodic = mm.PeriodicTorsionForce()
                                    sys.addForce(periodic)
945
                                periodic.addTorsion(baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], baseAtomIndex+atoms[3], int(float(params[7])), float(params[5])*degToRad, k)
946
947
948
                        elif dihedralType == '2':
                            # Harmonic torsion
                            k = float(params[6])
Stephen Constable's avatar
Stephen Constable committed
949
                            phi0 = float(params[5])
950
951
                            if k != 0:
                                if harmonicTorsion is None:
952
                                    harmonicTorsion = mm.CustomTorsionForce('0.5*k*(thetap-theta0)^2; thetap = step(-(theta-theta0+pi))*2*pi+theta+step(theta-theta0-pi)*(-2*pi); pi = %.15g' % math.pi)
953
954
                                    harmonicTorsion.addPerTorsionParameter('theta0')
                                    harmonicTorsion.addPerTorsionParameter('k')
955
                                    harmonicTorsion.setName('HarmonicTorsionForce')
956
                                    sys.addForce(harmonicTorsion)
Stephen Constable's avatar
Stephen Constable committed
957
958
                                # map phi0 into correct space
                                phi0 = phi0 - 360 if phi0 > 180 else phi0
Stephen Constable's avatar
Stephen Constable committed
959
                                harmonicTorsion.addTorsion(baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], baseAtomIndex+atoms[3], (phi0*degToRad, k))
960
961
962
963
964
965
966
                        else:
                            # RB Torsion
                            c = [float(x) for x in params[5:11]]
                            if any(x != 0 for x in c):
                                if rb is None:
                                    rb = mm.RBTorsionForce()
                                    sys.addForce(rb)
967
968
969
                                if dihedralType == '5':
                                    # Convert Fourier coefficients to RB coefficients.
                                    c = [c[1]+0.5*(c[0]+c[2]), 0.5*(-c[0]+3*c[2]), -c[1]+4*c[3], -2*c[2], -4*c[3], 0]
970
                                rb.addTorsion(baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], baseAtomIndex+atoms[3], c[0], c[1], c[2], c[3], c[4], c[5])
Justin MacCallum's avatar
Justin MacCallum committed
971

972
973
974
975
                # Add CMAP terms.

                for fields in moleculeType.cmaps:
                    atoms = [int(x)-1 for x in fields[:5]]
976
                    types = tuple(bondedTypes[i] for i in atoms)
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
                    if len(fields) >= 8 and len(fields) >= 8+int(fields[6])*int(fields[7]):
                        params = fields
                    elif types in self._cmapTypes:
                        params = self._cmapTypes[types]
                    elif types[::-1] in self._cmapTypes:
                        params = self._cmapTypes[types[::-1]]
                    else:
                        raise ValueError('No parameters specified for cmap: '+fields[0]+', '+fields[1]+', '+fields[2]+', '+fields[3]+', '+fields[4])
                    if cmap is None:
                        cmap = mm.CMAPTorsionForce()
                        sys.addForce(cmap)
                    mapSize = int(params[6])
                    if mapSize != int(params[7]):
                        raise ValueError('Non-square CMAPs are not supported')
                    map = []
                    for i in range(mapSize):
                        for j in range(mapSize):
994
                            map.append(float(params[8+mapSize*((j+mapSize//2)%mapSize)+((i+mapSize//2)%mapSize)]))
995
996
997
998
999
1000
                    map = tuple(map)
                    if map not in mapIndices:
                        mapIndices[map] = cmap.addMap(mapSize, map)
                    cmap.addTorsion(mapIndices[map], baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], baseAtomIndex+atoms[3],
                                 baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], baseAtomIndex+atoms[3], baseAtomIndex+atoms[4])

Peter Eastman's avatar
Peter Eastman committed
1001
                # Set nonbonded parameters for particles.
Justin MacCallum's avatar
Justin MacCallum committed
1002

Peter Eastman's avatar
Peter Eastman committed
1003
1004
                for fields in moleculeType.atoms:
                    params = self._atomTypes[fields[1]]
Sunhwan Jo's avatar
Sunhwan Jo committed
1005

Peter Eastman's avatar
Peter Eastman committed
1006
1007
1008
                    if len(fields) > 6:
                        q = float(fields[6])
                    else:
1009
                        q = float(params[4])
Stephen Constable's avatar
Stephen Constable committed
1010

Sunhwan Jo's avatar
Sunhwan Jo committed
1011
                    if has_nbfix_terms:
1012
                        nb.addParticle(q, 1.0, 0.0)
Sunhwan Jo's avatar
Sunhwan Jo committed
1013
                        atom_charges.append(q)
1014
                        lj.addParticle([0])
Sunhwan Jo's avatar
Sunhwan Jo committed
1015
1016
1017
                    else:
                        if self._defaults[1] == '1':
                            nb.addParticle(q, 1.0, 0.0)
1018
                            lj.addParticle([math.sqrt(float(params[6])), math.sqrt(float(params[7]))])
Sunhwan Jo's avatar
Sunhwan Jo committed
1019
1020
                        elif self._defaults[1] == '2':
                            nb.addParticle(q, float(params[6]), float(params[7]))
1021
1022
1023
1024
1025
                        elif self._defaults[1] == '3':
                            nb.addParticle(q, 1.0, 0.0)
                            sigma = float(params[6])
                            epsilon = float(params[7])
                            lj.addParticle([math.sqrt(4*epsilon*sigma**6), math.sqrt(4*epsilon*sigma**12)])
Stephen Constable's avatar
Stephen Constable committed
1026

Peter Eastman's avatar
Peter Eastman committed
1027
1028
1029
1030
1031
1032
1033
1034
                    if implicitSolvent is OBC2:
                        if fields[1] not in self._implicitTypes:
                            raise ValueError('No implicit solvent parameters specified for atom type: '+fields[1])
                        gbparams = self._implicitTypes[fields[1]]
                        gb.addParticle(q, float(gbparams[4]), float(gbparams[5]))
                for fields in moleculeType.bonds:
                    atoms = [int(x)-1 for x in fields[:2]]
                    bondIndices.append((baseAtomIndex+atoms[0], baseAtomIndex+atoms[1]))
1035
1036
1037
1038
                for fields in moleculeType.constraints:
                    if fields[2] == '1':
                        atoms = [int(x)-1 for x in fields[:2]]
                        bondIndices.append((baseAtomIndex+atoms[0], baseAtomIndex+atoms[1]))
Sunhwan Jo's avatar
Sunhwan Jo committed
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
                if has_nbfix_terms:
                    for fields in moleculeType.bonds:
                        atoms = [int(x)-1 for x in fields[:2]]
                        atom_partners[baseAtomIndex+atoms[0]]['bond'].add(baseAtomIndex+atoms[1])
                        atom_partners[baseAtomIndex+atoms[1]]['bond'].add(baseAtomIndex+atoms[0])
                    for fields in moleculeType.angles:
                        atoms = [int(x)-1 for x in fields[:3]]
                        angleIndices.append((baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], baseAtomIndex+atoms[2]))
                        for pair in combinations(atoms, 2):
                            atom_partners[baseAtomIndex+pair[0]]['angle'].add(baseAtomIndex+pair[1])
                            atom_partners[baseAtomIndex+pair[1]]['angle'].add(baseAtomIndex+pair[0])
                    for fields in moleculeType.dihedrals:
                        atoms = [int(x)-1 for x in fields[:4]]
                        torsionIndices.append((baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], baseAtomIndex+atoms[3]))
                        for pair in combinations(atoms, 2):
                            atom_partners[baseAtomIndex+pair[0]]['torsion'].add(baseAtomIndex+pair[1])
                            atom_partners[baseAtomIndex+pair[1]]['torsion'].add(baseAtomIndex+pair[0])
Justin MacCallum's avatar
Justin MacCallum committed
1056

1057
                # Record nonbonded exceptions.
Justin MacCallum's avatar
Justin MacCallum committed
1058

1059
1060
1061
                for fields in moleculeType.pairs:
                    atoms = [int(x)-1 for x in fields[:2]]
                    types = tuple(atomTypes[i] for i in atoms)
1062
1063
1064
1065
                    atom1params = nb.getParticleParameters(baseAtomIndex+atoms[0])
                    atom2params = nb.getParticleParameters(baseAtomIndex+atoms[1])
                    atom1params = [x.value_in_unit_system(unit.md_unit_system) for x in atom1params]
                    atom2params = [x.value_in_unit_system(unit.md_unit_system) for x in atom2params]
1066
                    if len(fields) >= 5:
1067
                        params = [float(x) for x in fields[3:5]]
1068
                    elif types in self._pairTypes:
1069
                        params = [float(x) for x in self._pairTypes[types][3:5]]
1070
                    elif types[::-1] in self._pairTypes:
1071
                        params = [float(x) for x in self._pairTypes[types[::-1]][3:5]]
Jason Swails's avatar
Jason Swails committed
1072
1073
1074
                    elif not self._genpairs:
                        raise ValueError('No pair parameters defined for atom '
                                         'types %s and gen-pairs is "no"' % types)
1075
1076
                    elif has_nbfix_terms:
                        continue
1077
                    else:
1078
1079
1080
1081
1082
1083
1084
                        # Generate the parameters based on the atom parameters.
                        if self._defaults[1] == '2':
                            params = [0.5*(atom1params[1]+atom2params[1]), fudgeLJ*math.sqrt(atom1params[2]*atom2params[2])]
                        else:
                            atom1lj = lj.getParticleParameters(baseAtomIndex+atoms[0])
                            atom2lj = lj.getParticleParameters(baseAtomIndex+atoms[1])
                            params = [fudgeLJ*atom1lj[0]*atom2lj[0], fudgeLJ*atom1lj[1]*atom2lj[1]]
1085
                    pairs.append((baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], atom1params[0]*atom2params[0]*fudgeQQ, params[0], params[1]))
Stephen Constable's avatar
Stephen Constable committed
1086
1087
1088
                for fields in moleculeType.exclusions:
                    atoms = [int(x)-1 for x in fields]
                    for atom in atoms[1:]:
1089
                        exclusions.append((baseAtomIndex+atoms[0], baseAtomIndex+atom))
1090
1091
                for atoms in exclusionsFromBonds:
                    exclusions.append((baseAtomIndex+atoms[0], baseAtomIndex+atoms[1]))
Robert McGibbon's avatar
Robert McGibbon committed
1092

Sunhwan Jo's avatar
Sunhwan Jo committed
1093
1094
1095
1096
1097
1098
1099
                # Record virtual sites

                for fields in moleculeType.vsites2:
                    atoms = [int(x)-1 for x in fields[:3]]
                    c1 = float(fields[4])
                    vsite = mm.TwoParticleAverageSite(baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], (1-c1), c1)
                    sys.setVirtualSite(baseAtomIndex+atoms[0], vsite)
1100
1101
1102
1103
1104
1105
                for fields in moleculeType.vsites3:
                    atoms = [int(x)-1 for x in fields[:4]]
                    c1 = float(fields[5])
                    c2 = float(fields[6])
                    vsite = mm.ThreeParticleAverageSite(baseAtomIndex+atoms[1], baseAtomIndex+atoms[2], baseAtomIndex+atoms[3], 1-c1-c2, c1, c2)
                    sys.setVirtualSite(baseAtomIndex+atoms[0], vsite)
Justin MacCallum's avatar
Justin MacCallum committed
1106

1107
1108
1109
1110
                # Add explicitly specified constraints.

                for fields in moleculeType.constraints:
                    atoms = [int(x)-1 for x in fields[:2]]
1111
                    length = float(fields[3])
1112
1113
                    sys.addConstraint(baseAtomIndex+atoms[0], baseAtomIndex+atoms[1], length)

1114
        # Create nonbonded exceptions.
Sunhwan Jo's avatar
Sunhwan Jo committed
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137

        if not has_nbfix_terms:
            nb.createExceptionsFromBonds(bondIndices, fudgeQQ, fudgeLJ)
        else:
            excluded_atom_pairs = set() # save these pairs so we don't zero them out
            for tor in torsionIndices:
                # First check to see if atoms 1 and 4 are already excluded because
                # they are 1-2 or 1-3 pairs (would happen in 6-member rings or
                # fewer). Then check that they're not already added as exclusions
                if 'bond' in atom_partners[tor[3]] and tor[0] in atom_partners[tor[3]]['bond']: continue
                if 'angle' in atom_partners[tor[3]] and tor[0] in atom_partners[tor[3]]['angle']: continue
                key = min((tor[0], tor[3]),
                          (tor[3], tor[0]))
                if key in excluded_atom_pairs: continue # multiterm...
                params1 = self._atomTypes[atom_types[tor[0]]]
                params4 = self._atomTypes[atom_types[tor[3]]]
                q1 = atom_charges[tor[0]]
                rmin1 = float(params1[6])
                eps1 = float(params1[7])
                q4 = atom_charges[tor[3]]
                rmin4 = float(params4[6])
                eps4 = float(params4[7])

1138
1139
1140
1141
1142
1143
                charge_prod = fudgeQQ*q1*q4
                epsilon = math.sqrt(abs(eps1 * eps4))
                if self._defaults[1] == '2':
                   rmin14 = (rmin1 + rmin4) / 2
                else:
                   rmin14 = math.sqrt(rmin1 * rmin4)
Sunhwan Jo's avatar
Sunhwan Jo committed
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
                nb.addException(tor[0], tor[3], charge_prod, rmin14, epsilon)
                excluded_atom_pairs.add(key)

            # Add excluded atoms
            for atom_idx, atom in atom_partners.items():
                # Exclude all bonds and angles
                for atom2 in atom['bond']:
                    if atom2 > atom_idx:
                        nb.addException(atom_idx, atom2, 0.0, 1.0, 0.0)
                        excluded_atom_pairs.add((atom_idx, atom2))
                for atom2 in atom['angle']:
                    if ((atom_idx, atom2) in excluded_atom_pairs):
                        continue
                    if atom2 > atom_idx:
                        nb.addException(atom_idx, atom2, 0.0, 1.0, 0.0)
                        excluded_atom_pairs.add((atom_idx, atom2))
                for atom2 in atom['dihedral']:
                    if atom2 <= atom_idx: continue
                    if ((atom_idx, atom2) in excluded_atom_pairs):
                        continue
                    nb.addException(atom_idx, atom2, 0.0, 1.0, 0.0)

1166
1167
        for exclusion in exclusions:
            nb.addException(exclusion[0], exclusion[1], 0.0, 1.0, 0.0, True)
Sunhwan Jo's avatar
Sunhwan Jo committed
1168

1169
        if lj is not None:
1170
1171
            # We're using a CustomNonbondedForce for LJ interactions, so also create a CustomBondForce
            # to handle the exceptions.
Sunhwan Jo's avatar
Sunhwan Jo committed
1172

1173
            pair_bond = mm.CustomBondForce('-C/r^6+A/r^12')
Stephen Constable's avatar
Stephen Constable committed
1174
1175
            pair_bond.addPerBondParameter('C')
            pair_bond.addPerBondParameter('A')
1176
            pair_bond.setName('LennardJonesExceptions')
Stephen Constable's avatar
Stephen Constable committed
1177
1178
            sys.addForce(pair_bond)
            for pair in pairs:
1179
                nb.addException(pair[0], pair[1], pair[2], 1.0, 0.0, True)
1180
                pair_bond.addBond(pair[0], pair[1], [pair[3], pair[4]])
1181
1182
1183
            for i in range(nb.getNumExceptions()):
                ii, jj, q, eps, sig = nb.getExceptionParameters(i)
                lj.addExclusion(ii, jj)
Stephen Constable's avatar
Stephen Constable committed
1184
        elif self._defaults[1] == '2':
1185
            for pair in pairs:
1186
                nb.addException(pair[0], pair[1], pair[2], pair[3], pair[4], True)
1187

Peter Eastman's avatar
Peter Eastman committed
1188
        # Finish configuring the NonbondedForce.
Justin MacCallum's avatar
Justin MacCallum committed
1189

Peter Eastman's avatar
Peter Eastman committed
1190
1191
1192
1193
        methodMap = {ff.NoCutoff:mm.NonbondedForce.NoCutoff,
                     ff.CutoffNonPeriodic:mm.NonbondedForce.CutoffNonPeriodic,
                     ff.CutoffPeriodic:mm.NonbondedForce.CutoffPeriodic,
                     ff.Ewald:mm.NonbondedForce.Ewald,
Peter Eastman's avatar
Peter Eastman committed
1194
1195
                     ff.PME:mm.NonbondedForce.PME,
                     ff.LJPME:mm.NonbondedForce.LJPME}
Peter Eastman's avatar
Peter Eastman committed
1196
1197
        nb.setNonbondedMethod(methodMap[nonbondedMethod])
        nb.setCutoffDistance(nonbondedCutoff)
1198
        nb.setEwaldErrorTolerance(ewaldErrorTolerance)
1199
1200
1201
        if switchDistance is not None:
            nb.setUseSwitchingFunction(True)
            nb.setSwitchingDistance(switchDistance)
1202
        if lj is not None:
1203
1204
1205
1206
1207
1208
1209
1210
            methodMap = {ff.NoCutoff:mm.CustomNonbondedForce.NoCutoff,
                         ff.CutoffNonPeriodic:mm.CustomNonbondedForce.CutoffNonPeriodic,
                         ff.CutoffPeriodic:mm.CustomNonbondedForce.CutoffPeriodic,
                         ff.Ewald:mm.CustomNonbondedForce.CutoffPeriodic,
                         ff.PME:mm.CustomNonbondedForce.CutoffPeriodic,
                         ff.LJPME:mm.CustomNonbondedForce.CutoffPeriodic}
            lj.setNonbondedMethod(methodMap[nonbondedMethod])
            lj.setCutoffDistance(nonbondedCutoff)
1211
1212
            if nonbondedMethod in (ff.PME, ff.LJPME, ff.Ewald, ff.CutoffPeriodic):
                lj.setUseLongRangeCorrection(True)
1213
1214
1215
            if switchDistance is not None:
                lj.setUseSwitchingFunction(True)
                lj.setSwitchingDistance(switchDistance)
1216
            lj.setName('LennardJonesForce')
Robert McGibbon's avatar
Robert McGibbon committed
1217

Sunhwan Jo's avatar
Sunhwan Jo committed
1218
1219
1220
1221
1222
1223
1224
1225
        if has_nbfix_terms:
            atom_nbfix_types = set([])
            for pair in self._nonbondTypes:
                atom_nbfix_types.add(pair[0])
                atom_nbfix_types.add(pair[1])

            lj_idx_list = [0 for _ in atom_types]
            lj_radii, lj_depths = [], []
1226
            atom_params = []
Sunhwan Jo's avatar
Sunhwan Jo committed
1227
1228
1229
1230
1231
            num_lj_types = 0
            lj_type_list = []
            for i,atom_type in enumerate(atom_types):
                atom = self._atomTypes[atom_type]
                if lj_idx_list[i]: continue # already assigned
1232
1233
                ljtype = (float(atom[6]), float(atom[7]))
                atom_params.append(ljtype)
Sunhwan Jo's avatar
Sunhwan Jo committed
1234
1235
1236
1237
1238
1239
1240
                num_lj_types += 1
                lj_idx_list[i] = num_lj_types
                lj_type_list.append(atom)
                for j in range(i+1, len(atom_types)):
                    atom_type2 = atom_types[j]
                    if lj_idx_list[j] > 0: continue # already assigned
                    atom2 = self._atomTypes[atom_type2]
1241
                    ljtype2 = (float(atom2[6]), float(atom2[7]))
Sunhwan Jo's avatar
Sunhwan Jo committed
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
                    if atom2 is atom:
                        lj_idx_list[j] = num_lj_types
                    elif atom_type not in atom_nbfix_types:
                        # Only non-NBFIXed atom types can be compressed
                        if ljtype == ljtype2:
                            lj_idx_list[j] = num_lj_types

            # Now everything is assigned. Create the A-coefficient and
            # B-coefficient arrays
            acoef = [0 for i in range(num_lj_types*num_lj_types)]
            bcoef = acoef[:]
            for i in range(num_lj_types):
                namei = lj_type_list[i][0]
                for j in range(num_lj_types):
                    namej = lj_type_list[j][0]
                    try:
1258
1259
1260
1261
1262
1263
1264
1265
                        types = self._nonbondTypes[tuple(sorted((namei, namej)))]
                        params = (float(types[3]), float(types[4]))
                        if self._defaults[1] == '2':
                            c6 = 4 * params[1] * params[0]**6
                            c12 = 4 * params[1] * params[0]**12
                        else:
                            c6 = params[0]
                            c12 = params[1]
Sunhwan Jo's avatar
Sunhwan Jo committed
1266
                    except KeyError:
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
                        params1 = atom_params[i]
                        params2 = atom_params[j]
                        if self._defaults[1] == '1':
                            c6 = math.sqrt(params1[0]*params2[0])
                            c12 = math.sqrt(params1[1]*params2[1])
                        else:
                            if self._defaults[1] == '2':
                                sigma = (params1[0] + params2[0]) / 2
                            else:
                                sigma = math.sqrt(params1[0] + params2[0])
                            epsilon = math.sqrt(params1[1] * params2[1])
                            c6 = 4 * epsilon * sigma**6
                            c12 = 4 * epsilon * sigma**12
                    acoef[i+num_lj_types*j] = math.sqrt(c12)
                    bcoef[i+num_lj_types*j] = c6
            lj.addTabulatedFunction('acoef', mm.Discrete2DFunction(num_lj_types, num_lj_types, acoef))
            lj.addTabulatedFunction('bcoef', mm.Discrete2DFunction(num_lj_types, num_lj_types, bcoef))
            for i, idx in enumerate(lj_idx_list):
                lj.setParticleParameters(i, [idx-1]) # adjust for indexing from 0
Sunhwan Jo's avatar
Sunhwan Jo committed
1286

1287
        # Adjust masses.
Robert McGibbon's avatar
Robert McGibbon committed
1288

1289
1290
1291
1292
        if hydrogenMass is not None:
            for atom1, atom2 in self.topology.bonds():
                if atom1.element == elem.hydrogen:
                    (atom1, atom2) = (atom2, atom1)
1293
1294
                if rigidWater and atom2.residue.name == 'HOH':
                    continue
1295
1296
1297
1298
                if atom2.element == elem.hydrogen and atom1.element not in (elem.hydrogen, None):
                    transferMass = hydrogenMass-sys.getParticleMass(atom2.index)
                    sys.setParticleMass(atom2.index, hydrogenMass)
                    sys.setParticleMass(atom1.index, sys.getParticleMass(atom1.index)-transferMass)
Peter Eastman's avatar
Peter Eastman committed
1299
1300
1301
1302
1303
1304

        # Add a CMMotionRemover.

        if removeCMMotion:
            sys.addForce(mm.CMMotionRemover())
        return sys
1305
1306

def _defaultGromacsIncludeDir():
Robert McGibbon's avatar
Robert McGibbon committed
1307
1308
    """Find the location where gromacs #include files are referenced from, by
    searching for (1) gromacs environment variables, (2) for the gromacs binary
1309
1310
    'pdb2gmx' or 'gmx' in the PATH, or (3) just using the default gromacs
    install location, /usr/local/gromacs/share/gromacs/top """
1311
1312
1313
1314
1315
1316
1317
1318
    if 'GMXDATA' in os.environ:
        return os.path.join(os.environ['GMXDATA'], 'top')
    if 'GMXBIN' in os.environ:
        return os.path.abspath(os.path.join(os.environ['GMXBIN'], '..', 'share', 'gromacs', 'top'))

    pdb2gmx_path = distutils.spawn.find_executable('pdb2gmx')
    if pdb2gmx_path is not None:
        return os.path.abspath(os.path.join(os.path.dirname(pdb2gmx_path), '..', 'share', 'gromacs', 'top'))
1319
1320
1321
1322
    else:
        gmx_path = distutils.spawn.find_executable('gmx')
        if gmx_path is not None:
            return os.path.abspath(os.path.join(os.path.dirname(gmx_path), '..', 'share', 'gromacs', 'top'))
1323
1324

    return '/usr/local/gromacs/share/gromacs/top'