Ejemplo n.º 1
0
def test_to_pandas():
    """A test to ensure that data sets are convertable to pandas objects."""

    source = CalculationSource('Dummy', {})

    pure_substance = Substance.from_components('C')
    binary_substance = Substance.from_components('C', 'O')

    data_set = PhysicalPropertyDataSet()

    data_set.properties[pure_substance.identifier] = []
    data_set.properties[binary_substance.identifier] = []

    for temperature in [298 * unit.kelvin, 300 * unit.kelvin, 302 * unit.kelvin]:

        thermodynamic_state = ThermodynamicState(temperature=temperature, pressure=1.0 * unit.atmosphere)

        density_property = Density(thermodynamic_state=thermodynamic_state,
                                   phase=PropertyPhase.Liquid,
                                   substance=pure_substance,
                                   value=1 * unit.gram / unit.milliliter,
                                   uncertainty=0.11 * unit.gram / unit.milliliter,
                                   source=source)

        dielectric_property = DielectricConstant(thermodynamic_state=thermodynamic_state,
                                                 phase=PropertyPhase.Liquid,
                                                 substance=pure_substance,
                                                 value=1 * unit.dimensionless,
                                                 uncertainty=0.11 * unit.dimensionless,
                                                 source=source)

        data_set.properties[pure_substance.identifier].append(density_property)
        data_set.properties[pure_substance.identifier].append(dielectric_property)

    for temperature in [298 * unit.kelvin, 300 * unit.kelvin, 302 * unit.kelvin]:

        thermodynamic_state = ThermodynamicState(temperature=temperature, pressure=1.0 * unit.atmosphere)

        enthalpy_property = EnthalpyOfMixing(thermodynamic_state=thermodynamic_state,
                                             phase=PropertyPhase.Liquid,
                                             substance=binary_substance,
                                             value=1 * unit.kilojoules / unit.mole,
                                             uncertainty=0.11 * unit.kilojoules / unit.mole,
                                             source=source)

        excess_property = ExcessMolarVolume(thermodynamic_state=thermodynamic_state,
                                            phase=PropertyPhase.Liquid,
                                            substance=binary_substance,
                                            value=1 * unit.meter**3 / unit.mole,
                                            uncertainty=0.11 * unit.meter**3 / unit.mole,
                                            source=source)

        data_set.properties[binary_substance.identifier].append(enthalpy_property)
        data_set.properties[binary_substance.identifier].append(excess_property)

    data_set_pandas = data_set.to_pandas()

    assert data_set_pandas is not None
    assert len(data_set_pandas) == 6
Ejemplo n.º 2
0
def test_weight_by_mole_fraction_protocol(component_smiles, value):

    full_substance = Substance.from_components('C', 'CC', 'CCC')
    component = Substance.from_components(component_smiles)

    mole_fraction = next(iter(full_substance.get_amounts(component_smiles))).value

    with tempfile.TemporaryDirectory() as temporary_directory:

        weight_protocol = WeightByMoleFraction('weight')
        weight_protocol.value = value
        weight_protocol.full_substance = full_substance
        weight_protocol.component = component

        result = weight_protocol.execute(temporary_directory, ComputeResources())

        assert not isinstance(result, PropertyEstimatorException)
        assert weight_protocol.weighted_value == value * mole_fraction
Ejemplo n.º 3
0
def test_local_simulation_storage():
    """A simple test to that force fields can be stored and
    retrieved using the local storage backend."""

    substance = Substance.from_components(r'C', r'C/C=C/C=C/COC(=O)',
                                          r'CCOC(=O)/C=C(/C)\O')

    dummy_simulation_data = StoredSimulationData()

    dummy_simulation_data.thermodynamic_state = ThermodynamicState(
        298.0 * unit.kelvin, 1.0 * unit.atmosphere)

    dummy_simulation_data.statistical_inefficiency = 1.0
    dummy_simulation_data.force_field_id = 'tmp_ff_id'

    dummy_simulation_data.substance = substance

    with tempfile.TemporaryDirectory() as base_directory:

        temporary_data_directory = os.path.join(base_directory, 'temp_data')
        temporary_backend_directory = os.path.join(base_directory,
                                                   'storage_dir')

        os.makedirs(temporary_data_directory)
        os.makedirs(temporary_backend_directory)

        local_storage = LocalFileStorage(temporary_backend_directory)
        dummy_simulation_data.unique_id = local_storage.store_simulation_data(
            dummy_simulation_data, temporary_data_directory)

        retrieved_data_directories = local_storage.retrieve_simulation_data(
            substance)

        assert substance.identifier in retrieved_data_directories
        assert len(retrieved_data_directories[substance.identifier]) == 1

        retrieved_data, retrieved_data_directory = retrieved_data_directories[
            substance.identifier][0]

        assert dummy_simulation_data.thermodynamic_state == retrieved_data.thermodynamic_state
        assert dummy_simulation_data.statistical_inefficiency == retrieved_data.statistical_inefficiency
        assert dummy_simulation_data.force_field_id == retrieved_data.force_field_id
        assert dummy_simulation_data.substance == retrieved_data.substance

        local_storage_new = LocalFileStorage(temporary_backend_directory)

        retrieved_data, retrieved_data_directory = local_storage_new.retrieve_simulation_data_by_id(
            dummy_simulation_data.unique_id)

        assert retrieved_data is not None
        assert os.path.isdir(retrieved_data_directory)
Ejemplo n.º 4
0
def _build_input_output_substances():
    """Builds sets if input and expected substances for the
    `test_build_coordinate_composition` test.

    Returns
    -------
    list of tuple of Substance and Substance
        A list of input and expected substances.
    """

    # Start with some easy cases
    substances = [
        (Substance.from_components('O'), Substance.from_components('O')),
        (Substance.from_components('O',
                                   'C'), Substance.from_components('O', 'C')),
        (Substance.from_components('O', 'C', 'CO'),
         Substance.from_components('O', 'C', 'CO'))
    ]

    # Handle some cases where rounding will need to occur.
    input_substance = Substance()
    input_substance.add_component(Substance.Component('O'),
                                  Substance.MoleFraction(0.41))
    input_substance.add_component(Substance.Component('C'),
                                  Substance.MoleFraction(0.59))

    expected_substance = Substance()
    expected_substance.add_component(Substance.Component('O'),
                                     Substance.MoleFraction(0.4))
    expected_substance.add_component(Substance.Component('C'),
                                     Substance.MoleFraction(0.6))

    substances.append((input_substance, expected_substance))

    input_substance = Substance()
    input_substance.add_component(Substance.Component('O'),
                                  Substance.MoleFraction(0.59))
    input_substance.add_component(Substance.Component('C'),
                                  Substance.MoleFraction(0.41))

    expected_substance = Substance()
    expected_substance.add_component(Substance.Component('O'),
                                     Substance.MoleFraction(0.6))
    expected_substance.add_component(Substance.Component('C'),
                                     Substance.MoleFraction(0.4))

    substances.append((input_substance, expected_substance))

    return substances
Ejemplo n.º 5
0
def test_calculate_reduced_potential_openmm():

    substance = Substance.from_components('O')
    thermodynamic_state = ThermodynamicState(298 * unit.kelvin,
                                             1.0 * unit.atmosphere)

    with tempfile.TemporaryDirectory() as directory:
        force_field_path = path.join(directory, 'ff.json')

        with open(force_field_path, 'w') as file:
            file.write(build_tip3p_smirnoff_force_field().json())

        build_coordinates = BuildCoordinatesPackmol('build_coordinates')
        build_coordinates.max_molecules = 10
        build_coordinates.mass_density = 0.05 * unit.grams / unit.milliliters
        build_coordinates.substance = substance
        build_coordinates.execute(directory, None)

        assign_parameters = BuildSmirnoffSystem(f'assign_parameters')
        assign_parameters.force_field_path = force_field_path
        assign_parameters.coordinate_file_path = build_coordinates.coordinate_file_path
        assign_parameters.substance = substance
        assign_parameters.execute(directory, None)

        reduced_potentials = CalculateReducedPotentialOpenMM(f'reduced_potentials')
        reduced_potentials.substance = substance
        reduced_potentials.thermodynamic_state = thermodynamic_state
        reduced_potentials.reference_force_field_paths = [force_field_path]
        reduced_potentials.system_path = assign_parameters.system_path
        reduced_potentials.trajectory_file_path = get_data_filename('test/trajectories/water.dcd')
        reduced_potentials.coordinate_file_path = get_data_filename('test/trajectories/water.pdb')
        reduced_potentials.kinetic_energies_path = get_data_filename('test/statistics/stats_pandas.csv')
        reduced_potentials.high_precision = False

        result = reduced_potentials.execute(directory, ComputeResources())

        assert not isinstance(result, PropertyEstimatorException)
        assert path.isfile(reduced_potentials.statistics_file_path)

        final_array = StatisticsArray.from_pandas_csv(reduced_potentials.statistics_file_path)
        assert ObservableType.ReducedPotential in final_array
def _setup_dummy_system(directory):

    force_field_path = path.join(directory, 'ff.json')

    with open(force_field_path, 'w') as file:
        file.write(build_tip3p_smirnoff_force_field().json())

    substance = Substance.from_components('C')

    build_coordinates = BuildCoordinatesPackmol('build_coordinates')
    build_coordinates.max_molecules = 1
    build_coordinates.mass_density = 0.001 * unit.grams / unit.milliliters
    build_coordinates.substance = substance
    build_coordinates.execute(directory, None)

    assign_parameters = BuildSmirnoffSystem(f'assign_parameters')
    assign_parameters.force_field_path = force_field_path
    assign_parameters.coordinate_file_path = build_coordinates.coordinate_file_path
    assign_parameters.substance = substance
    assign_parameters.execute(directory, None)

    return build_coordinates.coordinate_file_path, assign_parameters.system_path
Ejemplo n.º 7
0
def test_gradient_reduced_potentials(use_subset):

    substance = Substance.from_components('O')
    thermodynamic_state = ThermodynamicState(298 * unit.kelvin,
                                             1.0 * unit.atmosphere)

    with tempfile.TemporaryDirectory() as directory:

        force_field_path = path.join(directory, 'ff.json')

        with open(force_field_path, 'w') as file:
            file.write(build_tip3p_smirnoff_force_field().json())

        reduced_potentials = GradientReducedPotentials(f'reduced_potentials')
        reduced_potentials.substance = substance
        reduced_potentials.thermodynamic_state = thermodynamic_state
        reduced_potentials.reference_force_field_paths = [force_field_path]
        reduced_potentials.reference_statistics_path = get_data_filename(
            'test/statistics/stats_pandas.csv')
        reduced_potentials.force_field_path = force_field_path
        reduced_potentials.trajectory_file_path = get_data_filename(
            'test/trajectories/water.dcd')
        reduced_potentials.coordinate_file_path = get_data_filename(
            'test/trajectories/water.pdb')
        reduced_potentials.use_subset_of_force_field = use_subset
        reduced_potentials.enable_pbc = True
        reduced_potentials.parameter_key = ParameterGradientKey(
            'vdW', '[#1]-[#8X2H2+0:1]-[#1]', 'epsilon')

        result = reduced_potentials.execute(directory, ComputeResources())
        assert not isinstance(result, PropertyEstimatorException)

        assert path.isfile(reduced_potentials.forward_potentials_path)
        assert path.isfile(reduced_potentials.reverse_potentials_path)

        for reference_path in reduced_potentials.reference_force_field_paths:
            assert path.isfile(reference_path)
Ejemplo n.º 8
0
def generate_trajectories():
    """Generates trajectories to subsample.
    """

    setup_timestamp_logging()

    logger = logging.getLogger()

    substance = Substance.from_components('C(C(C(C(C(F)(F)Br)(F)F)(F)F)(F)F)(C(C(C(F)(F)F)(F)F)(F)F)(F)F')

    logger.info('Building system.')

    build_system = BuildSmirnoffSystem('build_system')
    build_system.coordinate_file_path = 'coords.pdb'
    build_system.substance = substance
    build_system.force_field_path = 'smirnoff99Frosst-1.1.0.offxml'
    build_system.execute('', None)

    logger.info('System built.')

    production_simulation = RunOpenMMSimulation(f'production_simulation')
    production_simulation.steps_per_iteration = 500
    production_simulation.output_frequency = 1
    production_simulation.timestep = 2.0 * unit.femtosecond
    production_simulation.thermodynamic_state = ThermodynamicState(temperature=298.15*unit.kelvin,
                                                                   pressure=1.0*unit.atmosphere)
    production_simulation.input_coordinate_file = 'coords.pdb'
    production_simulation.system_path = 'system.xml'

    compute_resources = ComputeResources(number_of_threads=4)

    logger.info(f'Simulation started.')
    production_simulation_schema = production_simulation.schema
    production_simulation.execute('', compute_resources)
    production_simulation.schema = production_simulation_schema
    logger.info(f'Simulation finished.')