forked from choderalab/fah-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-pme-parameters-in-system.py
118 lines (90 loc) · 3.24 KB
/
set-pme-parameters-in-system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/env python
"""
Set the PME parameters explicitly in system.xml file
Usage:
> python set-pme-parameters-in-system.py system.xml
Old file is backed up to system.xml.old
New file is created as system.xml
"""
import os
import sys
from simtk import openmm, unit
from simtk.openmm import XmlSerializer
#
# SUBROUTINES
#
def calc_pme_parameters(system):
"""Calculate PME parameters using scheme similar to OpenMM OpenCL platform.
Parameters
----------
system : simtk.openmm.System
The system for which parameters are to be computed.
Returns
-------
alpha : float
The PME alpha parameter
nx, ny, nz : int
The grid numbers in each dimension
"""
# Find nonbonded force.
forces = { system.getForce(index).__class__.__name__ : system.getForce(index) for index in range(system.getNumForces()) }
force = forces['NonbondedForce']
tol = force.getEwaldErrorTolerance()
boxVectors = system.getDefaultPeriodicBoxVectors()
from numpy import sqrt, log, ceil
from math import pow
alpha = (1.0/force.getCutoffDistance())*sqrt(-log(2.0*tol))
xsize = int(ceil(2*alpha*boxVectors[0][0]/(3*pow(tol, 0.2))))
ysize = int(ceil(2*alpha*boxVectors[1][1]/(3*pow(tol, 0.2))))
zsize = int(ceil(2*alpha*boxVectors[2][2]/(3*pow(tol, 0.2))))
def findLegalDimension(minimum):
while (True):
# Attempt to factor the current value.
unfactored = int(minimum)
for factor in range(2, 8):
while (unfactored > 1) and (unfactored%factor == 0):
unfactored /= factor
if (unfactored == 1):
return int(minimum)
minimum += 1
nx = findLegalDimension(xsize)
ny = findLegalDimension(ysize)
nz = findLegalDimension(zsize)
return (alpha, nx, ny, nz)
def read_file(filename):
infile = open(filename, 'r')
contents = infile.read()
infile.close()
return contents
def write_file(filename, contents):
outfile = open(filename, 'w')
outfile.write(contents)
outfile.close()
return
#
# MAIN
#
def fix_system(system_xml_filename):
"""
Set the PME parameters explicitly in a specified system XML file if they are not already set.
The file is renamed with '.old' appended, and a corrected file written in its place.
Parameters
----------
system_xml_filename : str
The name of the serialized system XML file to be modified
"""
system = XmlSerializer.deserialize(read_file(system_xml_filename))
forces = { system.getForce(force_index).__class__.__name__ : system.getForce(force_index) for force_index in range(system.getNumForces()) }
force = forces['NonbondedForce']
(alpha, nx, ny, nz) = force.getPMEParameters()
if alpha == 0.0 / unit.nanometers:
(alpha, nx, ny, nz) = calc_pme_parameters(system)
force.setPMEParameters(alpha, nx, ny, nz)
serialized_system = XmlSerializer.serialize(system)
os.rename(system_xml_filename, system_xml_filename + '.old')
write_file(system_xml_filename, serialized_system)
return
if __name__ == '__main__':
if len(sys.argv) != 2:
raise Exception('usage: python set-pme-parameters-in-system.py system.xml')
fix_system(sys.argv[1])