Commit b665dfcb authored by peastman's avatar peastman
Browse files

Merge pull request #1083 from rmcgibbo/wip-single-23-codebase

Single py2/3 codebase
parents 69a3f678 83d0e5e3
......@@ -102,10 +102,10 @@ class Unit(object):
# TODO - also handle non-simple units, i.e. units with multiple BaseUnits/ScaledUnits
assert len(self._top_base_units) == 1
assert len(self._scaled_units) == 0
dimension = self._top_base_units.iterkeys().next()
dimension = next(iter(self._top_base_units))
base_unit_dict = self._top_base_units[dimension]
assert len(base_unit_dict) == 1
parent_base_unit = base_unit_dict.iterkeys().next()
parent_base_unit = next(iter(base_unit_dict))
parent_exponent = base_unit_dict[parent_base_unit]
new_base_unit = BaseUnit(parent_base_unit.dimension, name, symbol)
# BaseUnit scale might be different depending on exponent
......@@ -121,10 +121,8 @@ class Unit(object):
Yields (BaseDimension, exponent) tuples comprising this unit.
"""
# There might be two units with the same dimension? No.
for dimension in sorted(self._all_base_units.iterkeys()):
exponent = 0
for base_unit in sorted(self._all_base_units[dimension].iterkeys()):
exponent += self._all_base_units[dimension][base_unit]
for dimension in sorted(self._all_base_units.keys()):
exponent = sum(self._all_base_units[dimension].values())
if exponent != 0:
yield (dimension, exponent)
......@@ -135,8 +133,8 @@ class Unit(object):
There might be multiple BaseUnits with the same dimension.
"""
for dimension in sorted(self._all_base_units.iterkeys()):
for base_unit in sorted(self._all_base_units[dimension].iterkeys()):
for dimension in sorted(self._all_base_units.keys()):
for base_unit in sorted(self._all_base_units[dimension].keys()):
exponent = self._all_base_units[dimension][base_unit]
yield (base_unit, exponent)
......@@ -144,8 +142,8 @@ class Unit(object):
"""
Yields (BaseUnit, exponent) tuples in this Unit, excluding those within BaseUnits.
"""
for dimension in sorted(self._top_base_units.iterkeys()):
for unit in sorted(self._top_base_units[dimension].iterkeys()):
for dimension in sorted(self._top_base_units.keys()):
for unit in sorted(self._top_base_units[dimension].keys()):
exponent = self._top_base_units[dimension][unit]
yield (unit, exponent)
......@@ -518,7 +516,7 @@ class ScaledUnit(object):
self.symbol = symbol
def __iter__(self):
for dim in sorted(self.base_units.iterkeys()):
for dim in sorted(self.base_units.keys()):
yield self.base_units[dim]
def iter_base_units(self):
......@@ -602,8 +600,7 @@ class UnitSystem(object):
if not len(self.base_units) == len(self.units):
raise ArithmeticError("UnitSystem must have same number of units as base dimensions")
# self.dimensions is a dict of {BaseDimension: index}
dimensions = base_units.keys()
dimensions.sort()
dimensions = sorted(base_units.keys())
self.dimensions = {}
for d in range(len(dimensions)):
self.dimensions[dimensions[d]] = d
......
......@@ -23,23 +23,23 @@ class TestAmberPrmtopFile(unittest.TestCase):
def test_NonbondedMethod(self):
"""Test all five options for the nonbondedMethod parameter."""
methodMap = {NoCutoff:NonbondedForce.NoCutoff,
CutoffNonPeriodic:NonbondedForce.CutoffNonPeriodic,
CutoffPeriodic:NonbondedForce.CutoffPeriodic,
methodMap = {NoCutoff:NonbondedForce.NoCutoff,
CutoffNonPeriodic:NonbondedForce.CutoffNonPeriodic,
CutoffPeriodic:NonbondedForce.CutoffPeriodic,
Ewald:NonbondedForce.Ewald, PME: NonbondedForce.PME}
for method in methodMap:
system = prmtop1.createSystem(nonbondedMethod=method)
forces = system.getForces()
self.assertTrue(any(isinstance(f, NonbondedForce) and
f.getNonbondedMethod()==methodMap[method]
self.assertTrue(any(isinstance(f, NonbondedForce) and
f.getNonbondedMethod()==methodMap[method]
for f in forces))
def test_Cutoff(self):
"""Test to make sure the nonbondedCutoff parameter is passed correctly."""
for method in [CutoffNonPeriodic, CutoffPeriodic, Ewald, PME]:
system = prmtop1.createSystem(nonbondedMethod=method,
nonbondedCutoff=2*nanometer,
system = prmtop1.createSystem(nonbondedMethod=method,
nonbondedCutoff=2*nanometer,
constraints=HBonds)
cutoff_distance = 0.0*nanometer
cutoff_check = 2.0*nanometer
......@@ -52,8 +52,8 @@ class TestAmberPrmtopFile(unittest.TestCase):
"""Test to make sure the ewaldErrorTolerance parameter is passed correctly."""
for method in [Ewald, PME]:
system = prmtop1.createSystem(nonbondedMethod=method,
ewaldErrorTolerance=1e-6,
system = prmtop1.createSystem(nonbondedMethod=method,
ewaldErrorTolerance=1e-6,
constraints=HBonds)
tolerance = 0
tolerance_check = 1e-6
......@@ -76,13 +76,13 @@ class TestAmberPrmtopFile(unittest.TestCase):
topology = prmtop1.topology
for constraints_value in [None, HBonds, AllBonds, HAngles]:
for rigidWater_value in [True, False]:
system = prmtop1.createSystem(constraints=constraints_value,
system = prmtop1.createSystem(constraints=constraints_value,
rigidWater=rigidWater_value)
validateConstraints(self, topology, system,
validateConstraints(self, topology, system,
constraints_value, rigidWater_value)
def test_ImplicitSolvent(self):
"""Test the four types of implicit solvents using the implicitSolvent
"""Test the four types of implicit solvents using the implicitSolvent
parameter.
"""
......@@ -93,7 +93,7 @@ class TestAmberPrmtopFile(unittest.TestCase):
force_type = CustomGBForce
else:
force_type = GBSAOBCForce
self.assertTrue(any(isinstance(f, force_type) for f in forces))
def test_ImplicitSolventParameters(self):
......@@ -102,7 +102,7 @@ class TestAmberPrmtopFile(unittest.TestCase):
CutoffNonPeriodic:NonbondedForce.CutoffNonPeriodic}
for implicitSolvent_value in [HCT, OBC1, OBC2, GBn]:
for method in methodMap:
system = prmtop2.createSystem(implicitSolvent=implicitSolvent_value,
system = prmtop2.createSystem(implicitSolvent=implicitSolvent_value,
solventDielectric=50.0, soluteDielectric=0.9, nonbondedMethod=method)
if implicitSolvent_value in set([HCT, OBC1, GBn]):
for force in system.getForces():
......@@ -122,12 +122,12 @@ class TestAmberPrmtopFile(unittest.TestCase):
if isinstance(force, NonbondedForce):
self.assertEqual(force.getReactionFieldDielectric(), 1.0)
self.assertEqual(force.getNonbondedMethod(), methodMap[method])
self.assertTrue(found_matching_solvent_dielectric and
self.assertTrue(found_matching_solvent_dielectric and
found_matching_solute_dielectric)
def test_HydrogenMass(self):
"""Test that altering the mass of hydrogens works correctly."""
topology = prmtop1.topology
hydrogenMass = 4*amu
system1 = prmtop1.createSystem()
......@@ -279,7 +279,7 @@ class TestAmberPrmtopFile(unittest.TestCase):
def test_ImplicitSolventForces(self):
"""Compute forces for different implicit solvent types, and compare them to ones generated with a previous version of OpenMM to ensure they haven't changed."""
solventType = [HCT, OBC1, OBC2, GBn, GBn2]
nonbondedMethod = [NoCutoff, CutoffNonPeriodic, CutoffNonPeriodic, NoCutoff, NoCutoff]
salt = [0.0, 0.0, 0.5, 0.5, 0.0]*(moles/liter)
......@@ -288,7 +288,7 @@ class TestAmberPrmtopFile(unittest.TestCase):
for i in range(5):
system = prmtop2.createSystem(implicitSolvent=solventType[i], nonbondedMethod=nonbondedMethod[i], implicitSolventSaltConc=salt[i])
integrator = VerletIntegrator(0.001)
context = Context(system, integrator, Platform.getPlatformByName("CPU"))
context = Context(system, integrator, Platform.getPlatformByName("Reference"))
context.setPositions(pdb.positions)
state1 = context.getState(getForces=True)
state2 = XmlSerializer.deserialize(open('systems/alanine-dipeptide-implicit-forces/'+file[i]+'.xml').read())
......@@ -307,7 +307,7 @@ class TestAmberPrmtopFile(unittest.TestCase):
system.addForce(MonteCarloBarostat(1.0 * atmospheres, temperature, 1))
integrator = LangevinIntegrator(temperature, 1.0 / picosecond, 0.0001 * picoseconds)
simulation = Simulation(prmtop.topology, system, integrator)
simulation.context.setPositions(inpcrd.positions)
simulation.context.setPeriodicBoxVectors(*inpcrd.boxVectors)
......
......@@ -8,7 +8,7 @@ import simtk.openmm.app.element as elem
class TestCharmmFiles(unittest.TestCase):
"""Test the GromacsTopFile.createSystem() method."""
def setUp(self):
"""Set up the tests by loading the input files."""
......@@ -23,14 +23,14 @@ class TestCharmmFiles(unittest.TestCase):
def test_NonbondedMethod(self):
"""Test both non-periodic methods for the systems"""
methodMap = {NoCutoff:NonbondedForce.NoCutoff,
methodMap = {NoCutoff:NonbondedForce.NoCutoff,
CutoffNonPeriodic:NonbondedForce.CutoffNonPeriodic}
for top in (self.psf_c, self.psf_x, self.psf_v):
for method in methodMap:
system = top.createSystem(self.params, nonbondedMethod=method)
forces = system.getForces()
self.assertTrue(any(isinstance(f, NonbondedForce) and
f.getNonbondedMethod()==methodMap[method]
self.assertTrue(any(isinstance(f, NonbondedForce) and
f.getNonbondedMethod()==methodMap[method]
for f in forces))
def test_Cutoff(self):
......@@ -39,7 +39,7 @@ class TestCharmmFiles(unittest.TestCase):
for top in (self.psf_c, self.psf_x, self.psf_v):
for method in [CutoffNonPeriodic]:
system = top.createSystem(self.params, nonbondedMethod=method,
nonbondedCutoff=2*nanometer,
nonbondedCutoff=2*nanometer,
constraints=HBonds)
cutoff_distance = 0.0*nanometer
cutoff_check = 2.0*nanometer
......@@ -67,7 +67,7 @@ class TestCharmmFiles(unittest.TestCase):
"""
system = self.psf_x.createSystem(self.params, implicitSolvent=GBn,
solventDielectric=50.0,
solventDielectric=50.0,
soluteDielectric = 0.9)
for force in system.getForces():
if isinstance(force, NonbondedForce):
......@@ -75,7 +75,7 @@ class TestCharmmFiles(unittest.TestCase):
def test_HydrogenMass(self):
"""Test that altering the mass of hydrogens works correctly."""
topology = self.psf_v.topology
hydrogenMass = 4*amu
system1 = self.psf_v.createSystem(self.params)
......@@ -131,7 +131,7 @@ class TestCharmmFiles(unittest.TestCase):
for i in range(5):
system = self.psf_c.createSystem(self.params, implicitSolvent=solventType[i], nonbondedMethod=nonbondedMethod[i], implicitSolventSaltConc=salt[i])
integrator = VerletIntegrator(0.001)
context = Context(system, integrator, Platform.getPlatformByName("CPU"))
context = Context(system, integrator, Platform.getPlatformByName("Reference"))
context.setPositions(self.pdb.positions)
state1 = context.getState(getForces=True)
#out = open('systems/ala-ala-ala-implicit-forces/'+file[i]+'.xml', 'w')
......
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