Example #1
0
def test_phase_creation():
    """Phases are initialized correctly by Yank.create()."""
    phase_name = 'my-solvent-phase'
    toluene = testsystems.TolueneImplicit()
    protocol = AbsoluteAlchemicalFactory.defaultSolventProtocolImplicit()
    atom_indices = find_components(toluene.system, toluene.topology, 'resname TOL')

    phase = AlchemicalPhase(phase_name, toluene.system, toluene.topology,
                            toluene.positions, atom_indices, protocol)
    thermodynamic_state = ThermodynamicState(temperature=300.0*unit.kelvin)

    # Create new simulation.
    with enter_temp_directory():
        output_dir = 'output'
        utils.config_root_logger(verbose=False)
        yank = Yank(store_directory=output_dir)
        yank.create(thermodynamic_state, phase)

        # Netcdf dataset has been created
        nc_path = os.path.join(output_dir, phase_name + '.nc')
        assert os.path.isfile(nc_path)

        # Read data
        try:
            nc_file = netcdf.Dataset(nc_path, mode='r')
            metadata_group = nc_file.groups['metadata']
            serialized_topology = metadata_group.variables['topology'][0]
        finally:
            nc_file.close()

        # Topology has been stored correctly
        deserialized_topology = utils.deserialize_topology(serialized_topology)
        assert deserialized_topology == mdtraj.Topology.from_openmm(toluene.topology)
Example #2
0
def test_no_alchemical_atoms():
    """Test whether Yank raises exception when no alchemical atoms are specified."""
    toluene = testsystems.TolueneImplicit()

    # Create parameters. With the exception of atom_indices, all other
    # parameters must be legal, we don't want to catch an exception
    # different than the one we are testing.
    phase = AlchemicalPhase(name='solvent-implicit', reference_system=toluene.system,
                            reference_topology=toluene.topology,
                            positions=toluene.positions, atom_indices={'ligand': []},
                            protocol=AbsoluteAlchemicalFactory.defaultSolventProtocolImplicit())
    thermodynamic_state = ThermodynamicState(temperature=300.0*unit.kelvin)

    # Create new simulation.
    with enter_temp_directory():
        yank = Yank(store_directory='output')
        yank.create(thermodynamic_state, phase)
Example #3
0
def test_resuming():
    """Test that sampling correctly resumes."""
    # Prepare ModifiedHamiltonianExchange arguments
    toluene_test = testsystems.TolueneImplicit()
    ligand_atoms = range(15)
    alchemical_factory = AbsoluteAlchemicalFactory(toluene_test.system,
                                                   ligand_atoms=ligand_atoms)

    base_state = ThermodynamicState(temperature=300.0 * unit.kelvin)
    base_state.system = alchemical_factory.alchemically_modified_system

    alchemical_state1 = AlchemicalState(lambda_electrostatics=1.0,
                                        lambda_sterics=1.0)
    alchemical_state0 = AlchemicalState(lambda_electrostatics=0.0,
                                        lambda_sterics=0.0)
    alchemical_states = [alchemical_state1, alchemical_state0]

    positions = toluene_test.positions

    # We pass as fully_interacting_expanded_state and noninteracting_expanded_state the normal
    # reference state as we just want to check that they are correctly
    # set on resume
    fully_interacting_expanded_state = ThermodynamicState(temperature=300.0 *
                                                          unit.kelvin)
    fully_interacting_expanded_state.system = toluene_test.system
    noninteracting_expanded_state = copy.deepcopy(base_state)

    with enter_temp_directory():
        store_file_name = 'simulation.nc'
        simulation = ModifiedHamiltonianExchange(store_file_name)
        simulation.create(
            base_state,
            alchemical_states,
            positions,
            mc_atoms=ligand_atoms,
            fully_interacting_expanded_state=fully_interacting_expanded_state,
            noninteracting_expanded_state=noninteracting_expanded_state)

        # Clean up simulation and resume
        del simulation
        simulation = ModifiedHamiltonianExchange(store_file_name)
        simulation.resume()
        assert simulation.fully_interacting_expanded_state is not None
        assert simulation.noninteracting_expanded_state is not None