Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add energy derivatives to README #145

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,72 @@ to return forces.
torch_force.setOutputsForces(True)
```

Computing energy derivatives with respect to global parameters
--------------------------------------------------------------

Its possible to query `TorchForce` for the derivative of the energy with respect to global parameters. In order to do so the global parameters must be registered as energy derivatives. This is done by calling `addEnergyParameterDerivative()` for each parameter.
RaulPPelaez marked this conversation as resolved.
Show resolved Hide resolved

The parameter derivatives can be queried by calling `getEnergyParameterDerivatives()` on the `State` object returned by `Context.getState()`. The result is a dictionary with the parameter names as keys and the derivatives as values.

```python
import torch as pt
from torch import Tensor
from openmmtorch import TorchForce
import openmm as mm

class ForceWithParameters(pt.nn.Module):

def __init__(self):
super(ForceWithParameters, self).__init__()

def forward(
self, positions: Tensor, parameter1: Tensor, parameter2: Tensor
) -> Tensor:
x2 = positions.pow(2).sum(dim=1)
u_harmonic = ((parameter1 + parameter2**2) * x2).sum()
return u_harmonic
RaulPPelaez marked this conversation as resolved.
Show resolved Hide resolved


def example():
RaulPPelaez marked this conversation as resolved.
Show resolved Hide resolved
numParticles = 10
system = mm.System()
positions = np.random.rand(numParticles, 3)
for _ in range(numParticles):
system.addParticle(1.0)

pt_force = ForceWithParameters()
model = pt.jit.script(pt_force)
tforce = TorchForce(model)
parameter1 = 1.0
parameter2 = 1.0
force.setOutputsForces(False)
force.addGlobalParameter("parameter1", parameter1)
force.addEnergyParameterDerivative("parameter1")
force.addGlobalParameter("parameter2", parameter2)
force.addEnergyParameterDerivative("parameter2")
system.addForce(force)
integ = mm.VerletIntegrator(1.0)
platform = mm.Platform.getPlatformByName(platform)
context = mm.Context(system, integ, platform)
context.setPositions(positions)
state = context.getState(
getEnergy=True, getForces=True, getParameterDerivatives=True
)
# The network defines a potential of the form E(r) = (parameter1 + parameter2**2)*|r|^2
r2 = np.sum(positions * positions)
expectedEnergy = (parameter1 + parameter2**2) * r2
assert np.allclose(
r2,
state.getEnergyParameterDerivatives()["parameter1"],
)
assert np.allclose(
2 * parameter2 * r2,
state.getEnergyParameterDerivatives()["parameter2"],
)
```



Recording the model into a CUDA graph
-------------------------------------

Expand Down
Loading