Skip to content

Commit ecec24d

Browse files
committed
Add test to verify that the units in our euler step are correct
1 parent d8bb79e commit ecec24d

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

test_euler_step_openmm.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import openmm.app as app
2+
import mdtraj as md
3+
import openmm.unit as unit
4+
import jax.numpy as jnp
5+
import jax
6+
from dmff import Hamiltonian, NeighborList
7+
from scipy.constants import physical_constants
8+
from tqdm import trange
9+
10+
if __name__ == '__main__':
11+
init_pdb = app.PDBFile("./files/AD_c7eq.pdb")
12+
temp_as_unit = 300 * unit.kelvin
13+
temp = temp_as_unit.value_in_unit(unit.kelvin)
14+
15+
# The unit system is weird when you have 1 / ps, so we specify it in ps and then convert back to seconds
16+
gamma_in_ps = 1e12
17+
gamma_as_unit = gamma_in_ps / unit.picosecond
18+
gamma = gamma_in_ps * unit.picosecond
19+
gamma = gamma.value_in_unit(unit.second)
20+
21+
dt_as_unit = 1.0 * unit.microsecond
22+
dt = dt_as_unit.value_in_unit(unit.second)
23+
24+
kbT = 1.380649 * 6.02214076 * 1e-3 * temp
25+
26+
mdtraj_topology = md.Topology.from_openmm(init_pdb.topology)
27+
28+
# Construct the mass matrix
29+
mass = [a.element.mass.value_in_unit(unit.dalton) for a in init_pdb.topology.atoms()]
30+
new_mass = []
31+
for mass_ in mass:
32+
for _ in range(3):
33+
new_mass.append(mass_)
34+
mass = jnp.array(new_mass)
35+
# Obtain xi
36+
xi = jnp.sqrt(2 * kbT / mass / gamma)
37+
38+
39+
# Initialize the potential energy with amber forcefields
40+
ff = Hamiltonian('amber14/protein.ff14SB.xml', 'amber14/tip3p.xml')
41+
potentials = ff.createPotential(init_pdb.topology,
42+
nonbondedMethod=app.NoCutoff,
43+
nonbondedCutoff=1.0 * unit.nanometers,
44+
constraints=None,
45+
ewaldErrorTolerance=0.0005)
46+
# Create a box used when calling
47+
box = jnp.array([[50.0, 0.0, 0.0], [0.0, 50.0, 0.0], [0.0, 0.0, 50.0]])
48+
nbList = NeighborList(box, 4.0, potentials.meta["cov_map"])
49+
nbList.allocate(init_pdb.getPositions(asNumpy=True).value_in_unit(unit.nanometer))
50+
pairs = nbList.pairs
51+
52+
@jax.jit
53+
def U(_x):
54+
"""
55+
Calling U by U(x, box, pairs, ff.paramset.parameters), x is [22, 3] and output the energy, if it is batched, use vmap
56+
"""
57+
_U = potentials.getPotentialFunc()
58+
59+
return _U(_x.reshape(22, 3), box, pairs, ff.paramset.parameters)
60+
61+
def dUdx_fn_unscaled(_x):
62+
return jax.grad(lambda _x: U(_x).sum())(_x)
63+
64+
dUdx_fn_unscaled = jax.vmap(dUdx_fn_unscaled)
65+
dUdx_fn_unscaled = jax.jit(dUdx_fn_unscaled)
66+
67+
@jax.jit
68+
def dUdx_fn(_x):
69+
return dUdx_fn_unscaled(_x) / mass / gamma
70+
71+
@jax.jit
72+
def step(_x, _key):
73+
"""Perform one step of forward euler"""
74+
return _x - dt * dUdx_fn(_x) + jnp.sqrt(dt) * xi * jax.random.normal(_key, _x.shape)
75+
76+
def step_units(_x, _key):
77+
_x = unit.Quantity(_x.reshape(22, 3), unit.nanometer)
78+
grad = unit.Quantity(value=dUdx_fn_unscaled(_x.value_in_unit(unit.nanometer).reshape(1, 66)).reshape(22, 3),
79+
unit=unit.kilojoule_per_mole / unit.nanometer)
80+
mass_as_unit = unit.Quantity(mass.reshape(22, 3), unit.dalton)
81+
82+
new_x_det = _x - dt_as_unit * grad / mass_as_unit / gamma_as_unit
83+
_rand = jax.random.normal(_key, _x.shape)
84+
85+
# we convert the unadjusted noise variance to SI units
86+
unadjusted_noise_variance = 2 * dt_as_unit * unit.BOLTZMANN_CONSTANT_kB * temp_as_unit / mass_as_unit / gamma_as_unit
87+
# to do this, we need to convert daltons to kg
88+
noise_scale_SI_units = 1 / physical_constants['unified atomic mass unit'][
89+
0] * unadjusted_noise_variance.value_in_unit(unit.seconds * unit.seconds * unit.joule / unit.dalton)
90+
91+
# in the end, we need to convert the noise back to nanometers
92+
# since we are working in SI units, our noise is in meters
93+
noise = unit.Quantity(jnp.sqrt(noise_scale_SI_units) * _rand, unit.meter)
94+
95+
new_x = new_x_det + noise
96+
return new_x.value_in_unit(unit.nanometer).reshape(1, 66)
97+
98+
key = jax.random.PRNGKey(1)
99+
key, velocity_key = jax.random.split(key)
100+
steps = 100_000
101+
102+
_x = jnp.array(init_pdb.getPositions(asNumpy=True).value_in_unit(unit.nanometer)).reshape(1, -1)
103+
104+
for i in trange(steps):
105+
key, iter_key = jax.random.split(key)
106+
_x_v1 = step(_x, iter_key)
107+
_x_v2 = step_units(_x, iter_key)
108+
109+
assert jnp.allclose(_x_v1, _x_v2)
110+
111+
_x = _x_v1
112+
113+
print('All tests passed!')

0 commit comments

Comments
 (0)