|
| 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 = 1 |
| 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.femtosecond |
| 22 | + dt_in_ps = dt_as_unit.value_in_unit(unit.picosecond) |
| 23 | + dt = dt_as_unit.value_in_unit(unit.second) |
| 24 | + |
| 25 | + kbT = 1.380649 * 6.02214076 * 1e-3 * temp |
| 26 | + |
| 27 | + mdtraj_topology = md.Topology.from_openmm(init_pdb.topology) |
| 28 | + |
| 29 | + # Construct the mass matrix |
| 30 | + mass = [a.element.mass.value_in_unit(unit.dalton) for a in init_pdb.topology.atoms()] |
| 31 | + new_mass = [] |
| 32 | + for mass_ in mass: |
| 33 | + for _ in range(3): |
| 34 | + new_mass.append(mass_) |
| 35 | + mass = jnp.array(new_mass) |
| 36 | + # Obtain xi |
| 37 | + xi = jnp.sqrt(2 * kbT / mass / gamma) |
| 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 | + |
| 53 | + @jax.jit |
| 54 | + def U(_x): |
| 55 | + """ |
| 56 | + Calling U by U(x, box, pairs, ff.paramset.parameters), x is [22, 3] and output the energy, if it is batched, use vmap |
| 57 | + """ |
| 58 | + _U = potentials.getPotentialFunc() |
| 59 | + |
| 60 | + return _U(_x.reshape(22, 3), box, pairs, ff.paramset.parameters) |
| 61 | + |
| 62 | + |
| 63 | + def dUdx_fn_unscaled(_x): |
| 64 | + return jax.grad(lambda _x: U(_x).sum())(_x) |
| 65 | + |
| 66 | + |
| 67 | + dUdx_fn_unscaled = jax.vmap(dUdx_fn_unscaled) |
| 68 | + dUdx_fn_unscaled = jax.jit(dUdx_fn_unscaled) |
| 69 | + |
| 70 | + |
| 71 | + @jax.jit |
| 72 | + def dUdx_fn(_x): |
| 73 | + return dUdx_fn_unscaled(_x) / mass / gamma |
| 74 | + |
| 75 | + |
| 76 | + @jax.jit |
| 77 | + def step_langevin(_x, _v, _key): |
| 78 | + alpha = jnp.exp(-gamma_in_ps * dt_in_ps) |
| 79 | + f_scale = (1 - alpha) / gamma_in_ps |
| 80 | + new_v_det = alpha * _v + f_scale * -dUdx_fn_unscaled(_x) / mass |
| 81 | + new_v = new_v_det + jnp.sqrt(kbT * (1 - alpha ** 2) / mass) * jax.random.normal(_key, _x.shape) |
| 82 | + |
| 83 | + return _x + dt_in_ps * new_v, new_v |
| 84 | + |
| 85 | + |
| 86 | + def step_langevin_units(_x, _v, _key): |
| 87 | + _x = unit.Quantity(_x.reshape(22, 3), unit.nanometer) |
| 88 | + _v = unit.Quantity(_v.reshape(22, 3), unit.nanometer / unit.picosecond) |
| 89 | + |
| 90 | + alpha = jnp.exp(-gamma_as_unit * dt_as_unit) |
| 91 | + f_scale = (1 - alpha) / gamma_as_unit |
| 92 | + |
| 93 | + grad = unit.Quantity(value=dUdx_fn_unscaled(_x.value_in_unit(unit.nanometer).reshape(1, 66)).reshape(22, 3), |
| 94 | + unit=unit.kilojoule_per_mole / unit.nanometer) |
| 95 | + mass_as_unit = unit.Quantity(mass.reshape(22, 3), unit.dalton) |
| 96 | + new_v_det = alpha * _v + f_scale * -grad / mass_as_unit |
| 97 | + |
| 98 | + _rand = jax.random.normal(_key, _x.shape) |
| 99 | + |
| 100 | + # we convert the unadjusted noise variance to SI units |
| 101 | + unadjusted_noise_variance = unit.BOLTZMANN_CONSTANT_kB * temp_as_unit * (1 - alpha ** 2) / mass_as_unit |
| 102 | + # to do this, we need to convert daltons to kg |
| 103 | + noise_scale_SI_units = 1 / physical_constants['unified atomic mass unit'][ |
| 104 | + 0] * unadjusted_noise_variance.value_in_unit(unit.joule / unit.dalton) |
| 105 | + |
| 106 | + # in the end, we need to convert the noise back to nanometers |
| 107 | + # since we are working in SI units, our noise is in meters |
| 108 | + noise = unit.Quantity(jnp.sqrt(noise_scale_SI_units) * _rand, unit.meter / unit.second) |
| 109 | + |
| 110 | + new_v = new_v_det + noise |
| 111 | + return ((_x + dt_as_unit * new_v).value_in_unit(unit.nanometer).reshape(1, 66), |
| 112 | + new_v.value_in_unit(unit.nanometer / unit.picosecond).reshape(1, 66)) |
| 113 | + |
| 114 | + |
| 115 | + key = jax.random.PRNGKey(1) |
| 116 | + key, velocity_key = jax.random.split(key) |
| 117 | + steps = 100_000 |
| 118 | + |
| 119 | + _x = jnp.array(init_pdb.getPositions(asNumpy=True).value_in_unit(unit.nanometer)).reshape(1, -1) |
| 120 | + |
| 121 | + # sample the initial velocities from the boltzmann distribution |
| 122 | + # again, we compare the velocities in the same way as we did with the positions |
| 123 | + _v_v1 = jax.random.normal(velocity_key, _x.shape) * jnp.sqrt(kbT / mass) |
| 124 | + |
| 125 | + velocity_variance = unit.Quantity(1 / mass, unit=1 / unit.dalton) * unit.BOLTZMANN_CONSTANT_kB * unit.Quantity(temp, unit=unit.kelvin) |
| 126 | + # Although velocity+variance is of the unit J / Da = m^2 / s^2, openmm cannot handle this directly and we need to convert it |
| 127 | + velocity_variance_in_si = 1 / physical_constants['unified atomic mass unit'][ |
| 128 | + 0] * velocity_variance.value_in_unit(unit.joule / unit.dalton) |
| 129 | + # velocity_variance_in_si = unit.Quantity(velocity_variance_in_si, unit.meter / unit.second) |
| 130 | + |
| 131 | + _v_v2 = jnp.sqrt(velocity_variance_in_si) * jax.random.normal(velocity_key, _x.shape) |
| 132 | + _v_v2 = unit.Quantity(_v_v2, unit.meter / unit.second).value_in_unit(unit.nanometer / unit.picosecond) |
| 133 | + |
| 134 | + assert jnp.allclose(_v_v1, _v_v2), "Initial velocities are not the same!" |
| 135 | + |
| 136 | + _v = _v_v1 |
| 137 | + |
| 138 | + for i in trange(steps): |
| 139 | + key, iter_key = jax.random.split(key) |
| 140 | + _x_v1, _v_v1 = step_langevin(_x, _v, iter_key) |
| 141 | + _x_v2, _v_v2 = step_langevin_units(_x, _v, iter_key) |
| 142 | + |
| 143 | + assert jnp.allclose(_x_v1, _x_v2), "Positions are not the same!" |
| 144 | + assert jnp.allclose(_v_v1, _v_v2), "Velocities are not the same!" |
| 145 | + |
| 146 | + _x = _x_v1 |
| 147 | + _v = _v_v1 |
| 148 | + |
| 149 | + print('All tests passed!') |
0 commit comments