Page 1 of 1

context.setPositions does not update positions

Posted: Mon Jan 08, 2024 10:37 am
by ajerschow
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

Re: context.setPositions does not update positions

Posted: Mon Jan 08, 2024 10:56 am
by peastman
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.

Re: context.setPositions does not update positions

Posted: Mon Jan 08, 2024 10:59 am
by ajerschow
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

Re: context.setPositions does not update positions

Posted: Mon Jan 08, 2024 11:34 am
by ajerschow
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

Re: context.setPositions does not update positions

Posted: Mon Jan 08, 2024 11:50 am
by peastman
You need to move the calls to addParticle() to before you create the Simulation.

Re: context.setPositions does not update positions

Posted: Sat Jan 13, 2024 2:29 pm
by ajerschow
Thank you very much! This worked, I combined the atoms in the modeler before creating the simulation.
Alexej