Skip to content

Commit

Permalink
Need to reinitialise context for constraint change to take effect.
Browse files Browse the repository at this point in the history
  • Loading branch information
lohedges committed Oct 11, 2024
1 parent f05f142 commit c0ce1f5
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/somd2/runner/_dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def _setup_dynamics(self, equilibration=False):
else:
pressure = None

# Create the dynamics object.
self._dyn = self._system.dynamics(
integrator=self._config.integrator,
temperature=self._config.temperature,
Expand Down Expand Up @@ -222,6 +223,18 @@ def _setup_dynamics(self, equilibration=False):
map=self._config._extra_args,
)

# We now need to re-initialize the context so that the constraints
# are updated correctly.

# Get the current positions.
positions = self._dyn._d._omm_mols.getState(getPositions=True).getPositions()

# Reinitialize the context to update the constraints.
self._dyn._d._omm_mols.reinitialize()

# Set the positions.
self._dyn._d._omm_mols.setPositions(positions)

def _minimisation(
self, lambda_min=None, constraint="none", perturbable_constraint="none"
):
Expand All @@ -239,6 +252,7 @@ def _minimisation(
if lambda_min is None:
_logger.info(f"Minimising at {_lam_sym} = {self._lambda_val}")
try:
# Create a minimisation object.
m = self._system.minimisation(
cutoff_type=self._config.cutoff_type,
cutoff=self._config.cutoff,
Expand All @@ -256,13 +270,27 @@ def _minimisation(
shift_delta=self._config.shift_delta,
map=self._config._extra_args,
)

# We now need to re-initialize the context so that the constraints
# are updated correctly.

# Get the current positions.
positions = m._d._omm_mols.getState(getPositions=True).getPositions()

# Reinitialize the context to update the constraints.
m._d._omm_mols.reinitialize()

# Set the positions.
m._d._omm_mols.setPositions(positions)

m.run(timeout=self._config.timeout)
self._system = m.commit()
except:
raise
else:
_logger.info(f"Minimising at {_lam_sym} = {lambda_min}")
try:
# Create a minimisation object.
m = self._system.minimisation(
cutoff_type=self._config.cutoff_type,
cutoff=self._config.cutoff,
Expand All @@ -280,6 +308,20 @@ def _minimisation(
shift_delta=self._config.shift_delta,
map=self._config._extra_args,
)

# We now need to re-initialize the context so that the constraints
# are updated correctly.

# Get the current positions.
positions = m._d._omm_mols.getState(getPositions=True).getPositions()

# Reinitialize the context to update the constraints.
m._d._omm_mols.reinitialize()

# Set the positions.
m._d._omm_mols.setPositions(positions)

# Minimise and commit the changes.
m.run(timeout=self._config.timeout)
self._system = m.commit()
except:
Expand Down

0 comments on commit c0ce1f5

Please sign in to comment.