Removing Forces

The functionality of OpenMM will (eventually) include everything that one would need to run modern molecular simulation.
POST REPLY
User avatar
Gideon Simpson
Posts: 28
Joined: Tue Sep 26, 2017 11:15 pm

Removing Forces

Post by Gideon Simpson » Tue Oct 29, 2019 6:33 pm

I was experimenting with a problem where I let my system run for some amount of time, call

Code: Select all

simulation.system.removeForce(idx)
where idx corresponds to the index of the force I want to remove, and then call

Code: Select all

simulation.step(nsteps)
to run for some additional time. I am finding that the system continues to evolve as though the removed force were still present. Is that not what the removeForce method does? Or must something else be done?

User avatar
Peter Eastman
Posts: 2562
Joined: Thu Aug 09, 2007 1:25 pm

Re: Removing Forces

Post by Peter Eastman » Tue Oct 29, 2019 8:50 pm

When you create a Context, it copies out all the information about the System. From that point on, any changes to the System have no effect on the existing Context.

You can force it to pick up the changes by calling reinitialize() on the Context. That throws out all existing information and rebuilds its data structures from scratch, which means state information (positions, velocities, etc.) is lost. If you specify preserveState=True, it will try to preserve the state information by first creating a checkpoint, then trying to reload the checkpoint after the context is reinitialized. That will work as long as nothing has changed that alters the content of checkpoints. For removing a Force, that basically means the Force can't have defined any global parameters, since those get stored in checkpoints. Otherwise, you'll have to manually restore all relevant state information after reinitializing the context.

User avatar
Gideon Simpson
Posts: 28
Joined: Tue Sep 26, 2017 11:15 pm

Re: Removing Forces

Post by Gideon Simpson » Wed Oct 30, 2019 4:01 am

I'll give that a shot. Two follow up questions. Is this the intended use of removeForce? Is there a simpler/better way to accomplish what I'm trying to do?

User avatar
Peter Eastman
Posts: 2562
Joined: Thu Aug 09, 2007 1:25 pm

Re: Removing Forces

Post by Peter Eastman » Wed Oct 30, 2019 8:36 am

I'm not sure exactly what you're trying to do, so I don't know if there's a better way. But that sounds like one reasonable way of doing it.

User avatar
Gideon Simpson
Posts: 28
Joined: Tue Sep 26, 2017 11:15 pm

Re: Removing Forces

Post by Gideon Simpson » Wed Oct 30, 2019 11:31 am

What I'm trying to do is to simulate a system where I turn certain forces on/off at specific times.

POST REPLY