Unverified Commit 5aa19c2d authored by peastman's avatar peastman Committed by GitHub
Browse files

Merge pull request #2464 from peastman/element

Improved determination of elements from Gromacs top files
parents ba62a938 8316b626
...@@ -563,20 +563,27 @@ class GromacsTopFile(object): ...@@ -563,20 +563,27 @@ class GromacsTopFile(object):
if atomName in atomReplacements: if atomName in atomReplacements:
atomName = atomReplacements[atomName] atomName = atomReplacements[atomName]
# Try to guess the element. # Try to determine the element.
upper = atomName.upper() atomicNumber = self._atomTypes[fields[1]][2]
if upper.startswith('CL'): if atomicNumber is None:
element = elem.chlorine # Try to guess the element from the name.
elif upper.startswith('NA'): upper = atomName.upper()
element = elem.sodium if upper.startswith('CL'):
elif upper.startswith('MG'): element = elem.chlorine
element = elem.magnesium 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
else: else:
try: element = elem.Element.getByAtomicNumber(int(atomicNumber))
element = elem.get_by_symbol(atomName[0])
except KeyError:
element = None
atoms.append(top.addAtom(atomName, element, r)) atoms.append(top.addAtom(atomName, element, r))
# Add bonds to the topology # Add bonds to the topology
......
...@@ -172,6 +172,13 @@ class TestGromacsTopFile(unittest.TestCase): ...@@ -172,6 +172,13 @@ class TestGromacsTopFile(unittest.TestCase):
top = GromacsTopFile('systems/bnz.top') top = GromacsTopFile('systems/bnz.top')
gro = GromacsGroFile('systems/bnz.gro') gro = GromacsGroFile('systems/bnz.gro')
for atom in top.topology.atoms():
if atom.name.startswith('C'):
self.assertEqual(elem.carbon, atom.element)
elif atom.name.startswith('H'):
self.assertEqual(elem.hydrogen, atom.element)
else:
self.assertIsNone(atom.element)
system = top.createSystem() system = top.createSystem()
self.assertEqual(26, system.getNumParticles()) self.assertEqual(26, system.getNumParticles())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment