Commit 8316b626 authored by peastman's avatar peastman
Browse files

Improved determination of elements from Gromacs top files

parent ba62a938
...@@ -563,8 +563,11 @@ class GromacsTopFile(object): ...@@ -563,8 +563,11 @@ 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.
atomicNumber = self._atomTypes[fields[1]][2]
if atomicNumber is None:
# Try to guess the element from the name.
upper = atomName.upper() upper = atomName.upper()
if upper.startswith('CL'): if upper.startswith('CL'):
element = elem.chlorine element = elem.chlorine
...@@ -577,6 +580,10 @@ class GromacsTopFile(object): ...@@ -577,6 +580,10 @@ class GromacsTopFile(object):
element = elem.get_by_symbol(atomName[0]) element = elem.get_by_symbol(atomName[0])
except KeyError: except KeyError:
element = None element = None
elif atomicNumber == '0':
element = None
else:
element = elem.Element.getByAtomicNumber(int(atomicNumber))
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