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

How to use global parameter and PBC at the same time? #140

Open
xiaowei-xie2 opened this issue May 14, 2024 · 10 comments
Open

How to use global parameter and PBC at the same time? #140

xiaowei-xie2 opened this issue May 14, 2024 · 10 comments

Comments

@xiaowei-xie2
Copy link

Hi,

I was trying to add a global parameter to my torchForce, but I could not figure out how to make it work along with the PBC. Could you help me have a look?

Here is the structure of my torchForce class:

class ForceModule(torch.nn.Module):
def __init__(self):
    ...
def forward(self, coordinates: torch.Tensor, cell: torch.Tensor, scale: float, RcNL: float = 10.0):
     return energy

Then when I try to load the model with

force = TorchForce('model.pt')
force.setUsesPeriodicBoundaryConditions(True)
force.addGlobalParameter('scale', 0.9)

and try to run a MD with it, I get the following error:

Traceback (most recent call last):
  File "/home/xie1/torchSANI_jit/scripts/openmm_test_3.py", line 128, in <module>
    state = simulation.context.getState(getEnergy=True, groups={10})
  File "/home/xie1/psi4conda/envs/nnpops_env/lib/python3.10/site-packages/openmm/openmm.py", line 12111, in getState
    state = _openmm.Context_getState(self, types, enforcePeriodicBox, groups_mask)
openmm.OpenMMException: forward() Expected a value of type 'float' for argument 'scale' but instead found type 'Tensor'.
Position: 3
Declaration: forward(__torch__.___torch_mangle_0.ForceModule self, Tensor coordinates, Tensor cell, float scale, float RcNL=10.) -> Tensor

Thank you so much!

@peastman
Copy link
Member

You declare that the scale parameter should be a float:

def forward(self, coordinates: torch.Tensor, cell: torch.Tensor, scale: float, RcNL: float = 10.0):

But when OpenMM calls your module, it passes all the arguments as Tensors, leading to the exception

openmm.OpenMMException: forward() Expected a value of type 'float' for argument 'scale' but instead found type 'Tensor'.

If you change your declaration to scale: torch.Tensor, it should work.

@xiaowei-xie2
Copy link
Author

That worked, thank you! A follow-up question: can I get the derivative w.r.t. that global parameter along the MD trajectory?

@peastman
Copy link
Member

No, but that would be a reasonable feature to add. Is that something you would find useful?

@xiaowei-xie2
Copy link
Author

Yes, I think that would be super helpful for any FEP type calculations with ML force fields! Do you know any workarounds at the moment? If I can write out the derivative expression myself, what would be a way to save that value at each MD frame?

@peastman
Copy link
Member

I created a feature request for it (#141). I'll try to do it soon. I don't think it should be difficult.

@xiaowei-xie2
Copy link
Author

Thank you so much!

@peastman
Copy link
Member

The implementation is at #143. If you care to try it out, please let us know whether it works for you.

@xiaowei-xie2
Copy link
Author

Wow, thank you! Do I need to build from source from your branch to test it out?

@peastman
Copy link
Member

Yes, if that's not too difficult. Otherwise you'll need to wait for the next release.

@xiaowei-xie2
Copy link
Author

No problem, I will give it a try soon and let you know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants