context.setPositions does not update positions

The functionality of OpenMM will (eventually) include everything that one would need to run modern molecular simulation.
POST REPLY
User avatar
Alexej Jerschow
Posts: 22
Joined: Tue Mar 10, 2020 7:31 am

context.setPositions does not update positions

Post by Alexej Jerschow » Mon Jan 08, 2024 10:37 am

I am trying to do setPositions for additional particles added, but they are not updated, see code below (using OpenMM 7.7). What could be the problem?
Thank you!
Alexej

Code: Select all

from openff.toolkit.topology import Molecule
from openmmforcefields.generators import GAFFTemplateGenerator

from openmm import *
from openmm import XmlSerializer, LangevinMiddleIntegrator
from openmm.unit import *
from openmm.app import *    
from sys import stdout
import numpy as np

molecule = Molecule.from_smiles('C=O')
# Create the GAFF template generator
gaff = GAFFTemplateGenerator(molecules=molecule)
# Create an OpenMM ForceField object with AMBER ff14SB and TIP3P with compatible ions
forcefield = ForceField('amber/protein.ff14SB.xml', 'amber/tip3p_standard.xml', 'amber/tip3p_HFE_multivalent.xml')
# Register the GAFF template generator
forcefield.registerTemplateGenerator(gaff.generator)

pdb_ch2o = PDBFile('ch2o.pdb')

modeller = Modeller(pdb_ch2o.topology, pdb_ch2o.positions)
system = forcefield.createSystem(modeller.topology, nonbondedCutoff=1.0*nanometer, constraints=HBonds)
integrator = LangevinMiddleIntegrator(300*kelvin, 1/picosecond, 0.001*picoseconds)
simulation = Simulation(modeller.topology, system, integrator)
simulation.context.setPositions(modeller.positions)

# add two additional particles with zero mass, so they will be immovable
id1 = system.addParticle(0.0) 
id2 = system.addParticle(0.0) 

# try to set new positions
origpos = simulation.context.getState(getPositions=True).getPositions().copy()
origpos[4] = Vec3(10,10,10) * nanometers
simulation.context.setPositions(origpos)

# check positions, the positions are not updated in simulation.context
print(origpos[4])
print(simulation.context.getState(getPositions=True).getPositions()[4])
Output is

Code: Select all

Vec3(x=10, y=10, z=10) nm
Vec3(x=0.0, y=0.0, z=0.0) nm
so the positions were not updated
Attachments
ch2o.pdb.zip
(683 Bytes) Downloaded 148 times

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

Re: context.setPositions does not update positions

Post by Peter Eastman » Mon Jan 08, 2024 10:56 am

Any change you make to a System doesn't affect already existing Simulations. You need to add the particles before you create the Simulation. See the FAQ for details.

User avatar
Alexej Jerschow
Posts: 22
Joined: Tue Mar 10, 2020 7:31 am

Re: context.setPositions does not update positions

Post by Alexej Jerschow » Mon Jan 08, 2024 10:59 am

Indeed, that could be the reason, but it was not clear to me how I can add particles without having a system.

Currently I am using system.addParticle, and later I was planning to add a nonbonding force only.

How would you suggest to add the particles before system?

Thank you

User avatar
Alexej Jerschow
Posts: 22
Joined: Tue Mar 10, 2020 7:31 am

Re: context.setPositions does not update positions

Post by Alexej Jerschow » Mon Jan 08, 2024 11:34 am

Also tried just now to set positions before first setting positions (so I guess that means before context is created?).
That also does not assign the proper positions to the added particles.
Alexej

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

Re: context.setPositions does not update positions

Post by Peter Eastman » Mon Jan 08, 2024 11:50 am

You need to move the calls to addParticle() to before you create the Simulation.

User avatar
Alexej Jerschow
Posts: 22
Joined: Tue Mar 10, 2020 7:31 am

Re: context.setPositions does not update positions

Post by Alexej Jerschow » Sat Jan 13, 2024 2:29 pm

Thank you very much! This worked, I combined the atoms in the modeler before creating the simulation.
Alexej

POST REPLY