Skip to content

How to create a CompoundThermodynamicState from a torchForce object with global parameter #147

@xiaowei-xie2

Description

@xiaowei-xie2

Hi,

I would like to do a Hamiltonian REMD with custom defined states, with each state specified by a torchForce object with a different global parameter. But I am having trouble creating a CompoundThermodynamicState to be used with ReplicaExchangeSampler. I can create a GlobalParameterState, but when I use that to create CompoundThermodynamicState, it complains there is no global parameter in the system. I have no trouble doing the same thing with a MM force field. Any idea what might be going wrong?

Thank you!

Here is the structure of the code I was using:

force = TorchForce('model.pt')
force.addGlobalParameter('a', 0.5)
force.addGlobalParameter('b', 0.3)
force.setUsesPeriodicBoundaryConditions(True)

# define system
system = ...

# Remove MM constraints
while system.getNumConstraints() > 0:
  system.removeConstraint(0)

# Remove MM forces
while system.getNumForces() > 0:
  system.removeForce(0)

assert system.getNumConstraints() == 0
assert system.getNumForces() == 0

system.addForce(force)

barostat = MonteCarloBarostat(1*bar, 298.15*kelvin)
system.addForce(barostat)

class LambdaState(GlobalParameterState):
    a = GlobalParameterState.GlobalParameter('a', standard_value=1.0)
    b = GlobalParameterState.GlobalParameter('b', standard_value=1.0)

    def set_rest_parameters(self, value_a, value_b):
        """Set all defined lambda parameters to the given value.

        The undefined parameters (i.e. those being set to None) remain undefined.

        Parameters
        ----------
        new_value : float
            The new value for all defined parameters.
        """
        lambda_functions = {'a': lambda a, b : value_a,
                 'b' : lambda a, b : value_b,
                 }

        for parameter_name in self._parameters:
            if self._parameters[parameter_name] is not None:
                new_value = lambda_functions[parameter_name](a, b)
                setattr(self, parameter_name, new_value)


lambda_state = LambdaState(a=0.5, b=0.3)
print('lambda_state.a:', lambda_state.a)
print('lambda_state.b:', lambda_state.b)

thermostate = ThermodynamicState(system, temperature=298.15 * unit.kelvin)
compound_thermodynamic_state = CompoundThermodynamicState(thermostate, composable_states=[lambda_state])

And I am getting the following error:

lambda_state.a: 0.5
lambda_state.b: 0.3
Traceback (most recent call last):
  File "/scr/xie1/training_xtb_test/openmm_FEP_lambdastate_REMD.py", line 129, in <module>
    compound_thermodynamic_state = CompoundThermodynamicState(thermostate, composable_states=[lambda_state])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xie1/miniconda3/lib/python3.12/site-packages/openmmtools/states.py", line 2790, in __init__
    self.set_system(self._standard_system, fix_state=True)
  File "/home/xie1/miniconda3/lib/python3.12/site-packages/openmmtools/states.py", line 2843, in set_system
    s.apply_to_system(system)
  File "/home/xie1/miniconda3/lib/python3.12/site-packages/openmmtools/states.py", line 3521, in apply_to_system
    raise self._GLOBAL_PARAMETER_ERROR(err_msg.format(parameter_name))
openmmtools.states.GlobalParameterError: Could not find global parameter a in the system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions