Closed annamherz closed 7 months ago
You can call getForces()
on the System to get all the forces it contains. You can do something like this:
force = [f for f in system.getForces() if isinstance(f, TorchForce)][0]
I see thank you! Actually for some reason in the 'getForces()' there isn't a TorchForce
after it is added to the system, but I can find it using:
torch_force = [f for f in mace_system.getForces() if f.getName() == "TorchForce"][0]
print(torch_force)
print(type(torch_force))
where the type of it after it was added to the system seems to be <class 'openmm.openmm.Force'>
instead of <class 'openmmtorch.TorchForce'>
which it was before being added to the system. I'm not sure if this is important? But either way I think it will now work for what I need the force for after, thank you!
Oh right, sorry about that. It's a quirk of the SWIG generated wrappers. getForces()
only knows about the Force classes in the main OpenMM package. Those ones get represented with the correct subclasses. Others just get represented with generic Force objects. You can use TorchForce.isinstance(f)
to check if f
is a TorchForce, and TorchForce.cast(f)
to cast it to the correct class.
That makes sense, thank you :blush:
Hello! I need to access just the TorchForce created for the system with the MLPotential. I was wondering if it would be possible to assign the force to an attribute so it can be accessed separately? So for example in the mace potential, something like this:
In this way then I could get the force for my hypothetical system using:
Alternatively if there is some other way to get the force from this that I missed?
Thank you so much!