testInstallation.py 2.33 KB
Newer Older
Robert McGibbon's avatar
Robert McGibbon committed
1
from __future__ import print_function
2
3
# First make sure OpenMM is installed.

Robert McGibbon's avatar
Robert McGibbon committed
4

5
6
7
8
9
10
import sys
try:
    from simtk.openmm.app import *
    from simtk.openmm import *
    from simtk.unit import *
except ImportError as err:
Robert McGibbon's avatar
Robert McGibbon committed
11
12
    print("Failed to import OpenMM packages:", err.message)
    print("Make sure OpenMM is installed and the library path is set correctly.")
13
14
15
16
17
18
19
20
21
22
23
    sys.exit()

# Create a System for the tests.

pdb = PDBFile('input.pdb')
forcefield = ForceField('amber99sb.xml', 'tip3p.xml')
system = forcefield.createSystem(pdb.topology, nonbondedMethod=PME, nonbondedCutoff=1*nanometer, constraints=HBonds)

# List all installed platforms and compute forces with each one.

numPlatforms = Platform.getNumPlatforms()
Robert McGibbon's avatar
Robert McGibbon committed
24
25
print("There are", numPlatforms, "Platforms available:")
print()
26
forces = [None]*numPlatforms
27
platformErrors = {}
28
29
for i in range(numPlatforms):
    platform = Platform.getPlatform(i)
Robert McGibbon's avatar
Robert McGibbon committed
30
    print(i+1, platform.getName(), end=" ")
31
32
    integrator = LangevinIntegrator(300*kelvin, 1/picosecond, 0.002*picoseconds)
    try:
33
34
        simulation = Simulation(pdb.topology, system, integrator, platform)
        simulation.context.setPositions(pdb.positions)
35
        forces[i] = simulation.context.getState(getForces=True).getForces()
36
        del simulation
Robert McGibbon's avatar
Robert McGibbon committed
37
        print("- Successfully computed forces")
38
    except:
Robert McGibbon's avatar
Robert McGibbon committed
39
        print("- Error computing forces with", platform.getName(), "platform")
40
41
42
43
44
45
46
        platformErrors[platform.getName()] = sys.exc_info()[1]

# Give details of any errors.

for platform in platformErrors:
    print()
    print("%s platform error: %s" % (platform, platformErrors[platform]))
47
48
49

# See how well the platforms agree.

50
if numPlatforms > 1:
Robert McGibbon's avatar
Robert McGibbon committed
51
52
53
    print()
    print("Median difference in forces between platforms:")
    print()
54
55
56
57
58
59
60
61
    for i in range(numPlatforms):
        for j in range(i):
            if forces[i] is not None and forces[j] is not None:
                errors = []
                for f1, f2 in zip(forces[i], forces[j]):
                    d = f1-f2
                    error = sqrt((d[0]*d[0]+d[1]*d[1]+d[2]*d[2])/(f1[0]*f1[0]+f1[1]*f1[1]+f1[2]*f1[2]))
                    errors.append(error)
Robert McGibbon's avatar
Robert McGibbon committed
62
63
64
                print("{} vs. {}: {:g}".format(Platform.getPlatform(j).getName(),
                                              Platform.getPlatform(i).getName(),
                                              sorted(errors)[len(errors)//2]))