def test_state_equality(): state_a = ThermodynamicState(temperature=1.0 * unit.kelvin, pressure=1.0 * unit.pascals) state_b = ThermodynamicState(temperature=1.0004 * unit.kelvin, pressure=1.0004 * unit.pascals) assert state_a == state_b state_c = ThermodynamicState(temperature=1.001 * unit.kelvin, pressure=1.001 * unit.pascals) assert state_a != state_c assert hash(state_a) != hash(state_c) state_d = ThermodynamicState(temperature=1.0005 * unit.kelvin, pressure=1.0005 * unit.pascals) assert state_a == state_d assert state_c != state_d state_e = ThermodynamicState(temperature=0.9995 * unit.kelvin, pressure=0.9995 * unit.pascals) assert state_a == state_e
def test_validate_data_set(): valid_property = Density( ThermodynamicState(298 * unit.kelvin, 1 * unit.atmosphere), PropertyPhase.Liquid, Substance.from_components("O"), 0.0 * unit.gram / unit.milliliter, 0.0 * unit.gram / unit.milliliter, ) data_set = PhysicalPropertyDataSet() data_set.add_properties(valid_property) data_set.validate() invalid_property = Density( ThermodynamicState(-1 * unit.kelvin, 1 * unit.atmosphere), PropertyPhase.Liquid, Substance.from_components("O"), 0.0 * unit.gram / unit.milliliter, 0.0 * unit.gram / unit.milliliter, ) with pytest.raises(AssertionError): data_set.add_properties(invalid_property) data_set.add_properties(invalid_property, validate=False) with pytest.raises(AssertionError): data_set.validate()
def create_filterable_data_set(): """Creates a dummy data with a diverse set of properties to be filtered, namely: - a liquid density measured at 298 K and 0.5 atm with 1 component containing only carbon. - a gaseous dielectric measured at 288 K and 1 atm with 2 components containing only nitrogen. - a solid EoM measured at 308 K and 1.5 atm with 3 components containing only oxygen. Returns ------- PhysicalPropertyDataSet The created data set. """ source = CalculationSource("Dummy", {}) carbon_substance = create_dummy_substance(number_of_components=1, elements=["C"]) density_property = Density( thermodynamic_state=ThermodynamicState(temperature=298 * unit.kelvin, pressure=0.5 * unit.atmosphere), phase=PropertyPhase.Liquid, substance=carbon_substance, value=1 * unit.gram / unit.milliliter, uncertainty=0.11 * unit.gram / unit.milliliter, source=source, ) nitrogen_substance = create_dummy_substance(number_of_components=2, elements=["N"]) dielectric_property = DielectricConstant( thermodynamic_state=ThermodynamicState(temperature=288 * unit.kelvin, pressure=1 * unit.atmosphere), phase=PropertyPhase.Gas, substance=nitrogen_substance, value=1 * unit.dimensionless, uncertainty=0.11 * unit.dimensionless, source=source, ) oxygen_substance = create_dummy_substance(number_of_components=3, elements=["O"]) enthalpy_property = EnthalpyOfMixing( thermodynamic_state=ThermodynamicState(temperature=308 * unit.kelvin, pressure=1.5 * unit.atmosphere), phase=PropertyPhase.Solid, substance=oxygen_substance, value=1 * unit.kilojoules / unit.mole, uncertainty=0.11 * unit.kilojoules / unit.mole, source=source, ) data_set = PhysicalPropertyDataSet() data_set.add_properties(density_property, dielectric_property, enthalpy_property) return data_set
def estimated_reference_sets(): estimated_density = Density( thermodynamic_state=ThermodynamicState(298.15 * unit.kelvin, pressure=1.0 * unit.atmosphere), phase=PropertyPhase.Liquid, substance=Substance.from_components("O", "CC=O"), value=1.0 * unit.kilogram / unit.meter**3, uncertainty=0.1 * unit.kilogram / unit.meter**3, ) estimated_density.id = "1" estimated_enthalpy = EnthalpyOfMixing( thermodynamic_state=ThermodynamicState(298.15 * unit.kelvin, pressure=1.0 * unit.atmosphere), phase=PropertyPhase.Liquid, substance=Substance.from_components("O", "CC=O"), value=1.0 * unit.kilocalorie / unit.mole, uncertainty=0.1 * unit.kilojoule / unit.mole, ) estimated_enthalpy.id = "2" estimated_data_set = PhysicalPropertyDataSet() estimated_data_set.add_properties(estimated_density, estimated_enthalpy) reference_density = DataSetEntry( id=1, property_type="Density", temperature=298.15, pressure=101.325, value=0.001, std_error=0.0001, doi=" ", components=[ Component(smiles="O", mole_fraction=0.5), Component(smiles="CC=O", mole_fraction=0.5), ], ) reference_enthalpy = DataSetEntry( id=2, property_type="EnthalpyOfMixing", temperature=298.15, pressure=101.325, value=4.184, std_error=0.1, doi=" ", components=[ Component(smiles="O", mole_fraction=0.5), Component(smiles="CC=O", mole_fraction=0.5), ], ) reference_data_set = DataSet( id="ref", description=" ", authors=[Author(name=" ", email="*****@*****.**", institute=" ")], entries=[reference_density, reference_enthalpy], ) return estimated_data_set, reference_data_set
def create_dummy_property(property_class): """Create a dummy liquid property of specified type measured at 298 K and 1 atm, with 2 components of methane and ethane. The property also contains a dummy receptor metadata entry. Parameters ---------- property_class : type of PhysicalProperty The type of property, e.g. Density, DielectricConstant... Returns ------- PhysicalProperty The created property. """ substance = create_dummy_substance(number_of_components=2) dummy_property = property_class( thermodynamic_state=ThermodynamicState(temperature=298 * unit.kelvin, pressure=1 * unit.atmosphere), phase=PropertyPhase.Liquid, substance=substance, value=10.0 * property_class.default_unit(), uncertainty=1.0 * property_class.default_unit(), ) dummy_property.source = CalculationSource(fidelity="dummy", provenance={}) # Make sure the property has the meta data required for more # involved properties. dummy_property.metadata = {"receptor_mol2": "unknown_path.mol2"} return dummy_property
def test_filter_ionic_liquid(): thermodynamic_state = ThermodynamicState( temperature=298.15 * unit.kelvin, pressure=101.325 * unit.kilopascal, ) # Ensure ionic liquids are filtered. data_set = PhysicalPropertyDataSet() data_set.add_properties( Density( thermodynamic_state=thermodynamic_state, phase=PropertyPhase.Liquid, value=1.0 * Density.default_unit(), uncertainty=1.0 * Density.default_unit(), source=MeasurementSource(doi=" "), substance=Substance.from_components("[Na+].[Cl-]"), ), Density( thermodynamic_state=thermodynamic_state, phase=PropertyPhase.Liquid, value=1.0 * Density.default_unit(), uncertainty=1.0 * Density.default_unit(), source=MeasurementSource(doi=" "), substance=Substance.from_components("C"), ), ) data_frame = data_set.to_pandas() filtered_frame = FilterByIonicLiquid.apply( data_frame, FilterByIonicLiquidSchema(), ) assert len(filtered_frame) == 1
def test_nested_input(): dict_protocol = DummyInputOutputProtocol("dict_protocol") dict_protocol.input_value = {"a": ThermodynamicState(1.0 * unit.kelvin)} quantity_protocol = DummyInputOutputProtocol("quantity_protocol") quantity_protocol.input_value = ProtocolPath("output_value[a].temperature", dict_protocol.id) schema = WorkflowSchema() schema.protocol_schemas = [dict_protocol.schema, quantity_protocol.schema] schema.validate() workflow = Workflow({}) workflow.schema = schema workflow_graph = workflow.to_graph() with tempfile.TemporaryDirectory() as temporary_directory: with DaskLocalCluster() as calculation_backend: results_futures = workflow_graph.execute(temporary_directory, calculation_backend) assert len(results_futures) == 1 result = results_futures[0].result() assert isinstance(result, WorkflowResult)
def test_compute_state_energy_gradients(tmpdir): build_tip3p_smirnoff_force_field().json(os.path.join(tmpdir, "ff.json")) _, parameterized_system = _setup_dummy_system( tmpdir, Substance.from_components("O"), 10, os.path.join(tmpdir, "ff.json")) protocol = SolvationYankProtocol("") protocol.thermodynamic_state = ThermodynamicState(298.15 * unit.kelvin, 1.0 * unit.atmosphere) protocol.gradient_parameters = [ ParameterGradientKey("vdW", "[#1]-[#8X2H2+0:1]-[#1]", "epsilon") ] gradients = protocol._compute_state_energy_gradients( mdtraj.load_dcd( get_data_filename("test/trajectories/water.dcd"), get_data_filename("test/trajectories/water.pdb"), ), parameterized_system.topology, parameterized_system.force_field.to_force_field(), True, ComputeResources(), ) assert len(gradients) == 1 assert not np.isclose(gradients[0].value, 0.0 * unit.dimensionless)
def _build_entry(*smiles: str) -> Density: """Builds a density data entry measured at ambient conditions and for a system containing the specified smiles patterns in equal amounts. Parameters ---------- smiles The smiles to build components for. Returns ------- The built components. """ assert len(smiles) > 0 return Density( thermodynamic_state=ThermodynamicState( temperature=298.15 * unit.kelvin, pressure=101.325 * unit.kilopascal, ), phase=PropertyPhase.Liquid, value=1.0 * Density.default_unit(), uncertainty=1.0 * Density.default_unit(), source=MeasurementSource(doi=" "), substance=Substance.from_components(*smiles), )
def test_average_free_energies_protocol(): """Tests adding together two free energies.""" compute_resources = ComputeResources(number_of_threads=1) delta_g_one = (-10.0 * unit.kilocalorie / unit.mole).plus_minus( 1.0 * unit.kilocalorie / unit.mole) delta_g_two = (-20.0 * unit.kilocalorie / unit.mole).plus_minus( 2.0 * unit.kilocalorie / unit.mole) thermodynamic_state = ThermodynamicState(298 * unit.kelvin, 1 * unit.atmosphere) sum_protocol = AverageFreeEnergies("average_free_energies") sum_protocol.values = [delta_g_one, delta_g_two] sum_protocol.thermodynamic_state = thermodynamic_state sum_protocol.execute("", compute_resources) result_value = sum_protocol.result.value.to(unit.kilocalorie / unit.mole) result_uncertainty = sum_protocol.result.error.to(unit.kilocalorie / unit.mole) assert isinstance(sum_protocol.result, unit.Measurement) assert result_value.magnitude == pytest.approx(-20.0, abs=0.2) assert result_uncertainty.magnitude == pytest.approx(2.0, abs=0.2)
def data_frame() -> pandas.DataFrame: temperatures = [303.15, 298.15] property_types = [Density, EnthalpyOfVaporization] data_set_entries = [] def _temperature_noise(): return (numpy.random.rand() / 2.0 + 0.51) / 10.0 for temperature in temperatures: for index, property_type in enumerate(property_types): noise = _temperature_noise() noise *= 1 if index == 0 else -1 data_set_entries.append( property_type( thermodynamic_state=ThermodynamicState( temperature=temperature * unit.kelvin, pressure=101.325 * unit.kilopascal, ), phase=PropertyPhase.Liquid, value=1.0 * property_type.default_unit(), uncertainty=1.0 * property_type.default_unit(), source=MeasurementSource(doi=" "), substance=Substance.from_components("C"), ), ) data_set_entries.append( property_type( thermodynamic_state=ThermodynamicState( temperature=(temperature + noise) * unit.kelvin, pressure=101.325 * unit.kilopascal, ), phase=PropertyPhase.Liquid, value=1.0 * property_type.default_unit(), uncertainty=1.0 * property_type.default_unit(), source=MeasurementSource(doi=" "), substance=Substance.from_components("C"), ), ) data_set = PhysicalPropertyDataSet() data_set.add_properties(*data_set_entries) data_frame = data_set.to_pandas() return data_frame
def test_average_free_energies_protocol(): """Tests adding together two free energies.""" delta_g_one = Observable( value=(-10.0 * unit.kilocalorie / unit.mole).plus_minus( 1.0 * unit.kilocalorie / unit.mole), gradients=[ ParameterGradient( key=ParameterGradientKey("vdW", "[#6:1]", "sigma"), value=0.1 * unit.kilocalorie / unit.mole / unit.angstrom, ) ], ) delta_g_two = Observable( value=(-20.0 * unit.kilocalorie / unit.mole).plus_minus( 2.0 * unit.kilocalorie / unit.mole), gradients=[ ParameterGradient( key=ParameterGradientKey("vdW", "[#6:1]", "sigma"), value=0.2 * unit.kilocalorie / unit.mole / unit.angstrom, ) ], ) thermodynamic_state = ThermodynamicState(298 * unit.kelvin, 1 * unit.atmosphere) sum_protocol = AverageFreeEnergies("") sum_protocol.values = [delta_g_one, delta_g_two] sum_protocol.thermodynamic_state = thermodynamic_state sum_protocol.execute() result_value = sum_protocol.result.value.to(unit.kilocalorie / unit.mole) result_uncertainty = sum_protocol.result.error.to(unit.kilocalorie / unit.mole) assert isinstance(sum_protocol.result, Observable) assert result_value.magnitude == pytest.approx(-20.0, abs=0.2) assert result_uncertainty.magnitude == pytest.approx(2.0, abs=0.2) assert (sum_protocol.confidence_intervals[0] > result_value > sum_protocol.confidence_intervals[1]) gradient_value = sum_protocol.result.gradients[0].value.to( unit.kilocalorie / unit.mole / unit.angstrom) beta = 1.0 / (298.0 * unit.kelvin * unit.molar_gas_constant).to( unit.kilocalorie / unit.mole) assert np.isclose( gradient_value.magnitude, (0.1 * np.exp(-beta.magnitude * -10.0) + 0.2 * np.exp(-beta.magnitude * -20.0)) / (np.exp(-beta.magnitude * -10.0) + np.exp(-beta.magnitude * -20.0)), )
def test_from_pandas(): """A test to ensure that data sets may be created from pandas objects.""" thermodynamic_state = ThermodynamicState(temperature=298.15 * unit.kelvin, pressure=1.0 * unit.atmosphere) original_data_set = PhysicalPropertyDataSet() original_data_set.add_properties( Density( thermodynamic_state=thermodynamic_state, phase=PropertyPhase.Liquid, substance=Substance.from_components("CO", "O"), value=1.0 * unit.kilogram / unit.meter**3, uncertainty=1.0 * unit.kilogram / unit.meter**3, source=MeasurementSource(doi="10.5281/zenodo.596537"), ), EnthalpyOfVaporization( thermodynamic_state=thermodynamic_state, phase=PropertyPhase.from_string("Liquid + Gas"), substance=Substance.from_components("C"), value=2.0 * unit.kilojoule / unit.mole, source=MeasurementSource(reference="2"), ), DielectricConstant( thermodynamic_state=thermodynamic_state, phase=PropertyPhase.Liquid, substance=Substance.from_components("C"), value=3.0 * unit.dimensionless, source=MeasurementSource(reference="3"), ), ) data_frame = original_data_set.to_pandas() recreated_data_set = PhysicalPropertyDataSet.from_pandas(data_frame) assert len(original_data_set) == len(recreated_data_set) for original_property in original_data_set: recreated_property = next(x for x in recreated_data_set if x.id == original_property.id) assert (original_property.thermodynamic_state == recreated_property.thermodynamic_state) assert original_property.phase == recreated_property.phase assert original_property.substance == recreated_property.substance assert numpy.isclose(original_property.value, recreated_property.value) if original_property.uncertainty == UNDEFINED: assert original_property.uncertainty == recreated_property.uncertainty else: assert numpy.isclose(original_property.uncertainty, recreated_property.uncertainty) assert original_property.source.doi == recreated_property.source.doi assert original_property.source.reference == recreated_property.source.reference
def test_reindex_data_set_no_mole_fraction(): """Tests that the ``reindex_data_set`` function behaves as expected when exact amounts are present.""" setup_timestamp_logging(logging.INFO) substance = substances.Substance() substance.add_component(substances.Component(smiles="O"), amount=substances.MoleFraction(1.0)) substance.add_component( substances.Component(smiles="CO", role=substances.Component.Role.Solute), amount=substances.ExactAmount(1), ) evaluator_data_set = PhysicalPropertyDataSet() evaluator_data_set.add_properties( SolvationFreeEnergy( thermodynamic_state=ThermodynamicState( temperature=298.15 * unit.kelvin, pressure=1.0 * unit.atmosphere), phase=PropertyPhase.Liquid, substance=substance, value=1.0 * SolvationFreeEnergy.default_unit(), uncertainty=1.0 * SolvationFreeEnergy.default_unit(), ), ) data_set = DataSet( id="data-set", description=" ", authors=[Author(name=" ", email="*****@*****.**", institute=" ")], entries=[ DataSetEntry( id=1, property_type="SolvationFreeEnergy", temperature=298.15, pressure=101.325, value=1.0, std_error=1.0, doi=" ", components=[ Component(smiles="O", mole_fraction=1.0), Component(smiles="CO", mole_fraction=0.0, exact_amount=1, role="Solute"), ], ) ], ) reindex_data_set(evaluator_data_set, data_set) assert evaluator_data_set.properties[0].id == "1"
def to_evaluator(self) -> "PhysicalProperty": from openff.evaluator import properties, substances, unit from openff.evaluator.attributes import UNDEFINED from openff.evaluator.datasets import MeasurementSource, PropertyPhase from openff.evaluator.thermodynamics import ThermodynamicState if not hasattr(properties, self.property_type): raise UnrecognisedPropertyType(self.property_type) property_class: Type[PhysicalProperty] = getattr( properties, self.property_type) thermodynamic_state = ThermodynamicState( temperature=self.temperature * unit.kelvin, pressure=self.pressure * unit.kilopascal, ) phase = PropertyPhase.from_string(self.phase) substance = substances.Substance() for component in self.components: off_component = substances.Component( smiles=component.smiles, role=substances.Component.Role[component.role]) if component.mole_fraction > 0: mole_fraction = substances.MoleFraction( component.mole_fraction) substance.add_component(off_component, mole_fraction) if component.exact_amount > 0: exact_amount = substances.ExactAmount(component.exact_amount) substance.add_component(off_component, exact_amount) internal_unit = unit.Unit(self.units) physical_property = property_class( thermodynamic_state=thermodynamic_state, phase=phase, substance=substance, value=self.value * internal_unit, uncertainty=UNDEFINED if self.std_error is None else self.std_error * internal_unit, source=MeasurementSource(doi=self.doi), ) physical_property.id = str(self.id) return physical_property
def data_frame() -> pandas.DataFrame: data_set = PhysicalPropertyDataSet() data_set.add_properties( Density( thermodynamic_state=ThermodynamicState( temperature=298.15 * unit.kelvin, pressure=101.325 * unit.kilopascal, ), phase=PropertyPhase.Liquid, value=1.0 * Density.default_unit(), uncertainty=1.0 * Density.default_unit(), source=MeasurementSource(doi=" "), substance=Substance.from_components("C"), ), Density( thermodynamic_state=ThermodynamicState( temperature=305.15 * unit.kelvin, pressure=101.325 * unit.kilopascal, ), phase=PropertyPhase.Liquid, value=1.0 * Density.default_unit(), uncertainty=1.0 * Density.default_unit(), source=MeasurementSource(doi=" "), substance=Substance.from_components("C"), ), Density( thermodynamic_state=ThermodynamicState( temperature=298.15 * unit.kelvin, pressure=105.325 * unit.kilopascal, ), phase=PropertyPhase.Liquid, value=1.0 * Density.default_unit(), uncertainty=1.0 * Density.default_unit(), source=MeasurementSource(doi=" "), substance=Substance.from_components("C"), ), ) return data_set.to_pandas()
def data_frame() -> pandas.DataFrame: temperatures = [298.15, 318.15] pressures = [101.325, 101.0] properties = [Density, EnthalpyOfMixing] mole_fractions = [(1.0, ), (1.0, ), (0.25, 0.75), (0.75, 0.25)] smiles = {1: [("C(F)(Cl)(Br)", ), ("C", )], 2: [("CO", "C"), ("C", "CO")]} loop_variables = [( temperature, pressure, property_type, mole_fraction, ) for temperature in temperatures for pressure in pressures for property_type in properties for mole_fraction in mole_fractions] data_entries = [] for temperature, pressure, property_type, mole_fraction in loop_variables: n_components = len(mole_fraction) for smiles_tuple in smiles[n_components]: substance = Substance() for smiles_pattern, x in zip(smiles_tuple, mole_fraction): substance.add_component(Component(smiles_pattern), MoleFraction(x)) data_entries.append( property_type( thermodynamic_state=ThermodynamicState( temperature=temperature * unit.kelvin, pressure=pressure * unit.kilopascal, ), phase=PropertyPhase.Liquid, value=1.0 * property_type.default_unit(), uncertainty=1.0 * property_type.default_unit(), source=MeasurementSource(doi=" "), substance=substance, )) data_set = PhysicalPropertyDataSet() data_set.add_properties(*data_entries) return data_set.to_pandas()
def test_run_openmm_simulation_checkpoints(): import mdtraj thermodynamic_state = ThermodynamicState(298 * unit.kelvin, 1.0 * unit.atmosphere) with tempfile.TemporaryDirectory() as directory: coordinate_path, parameterized_system = _setup_dummy_system(directory) # Check that executing twice doesn't run the simulation twice npt_equilibration = OpenMMSimulation("npt_equilibration") npt_equilibration.total_number_of_iterations = 1 npt_equilibration.steps_per_iteration = 4 npt_equilibration.output_frequency = 1 npt_equilibration.thermodynamic_state = thermodynamic_state npt_equilibration.input_coordinate_file = coordinate_path npt_equilibration.parameterized_system = parameterized_system npt_equilibration.execute(directory, ComputeResources()) assert os.path.isfile(npt_equilibration._checkpoint_path) npt_equilibration.execute(directory, ComputeResources()) assert len(npt_equilibration.observables) == 4 assert (len( mdtraj.load(npt_equilibration.trajectory_file_path, top=coordinate_path)) == 4) # Make sure that the output files are correctly truncating if more frames # than expected are written with open(npt_equilibration._checkpoint_path, "r") as file: checkpoint = json.load(file, cls=TypedJSONDecoder) # Fake having saved more frames than expected npt_equilibration.steps_per_iteration = 8 checkpoint.steps_per_iteration = 8 npt_equilibration.output_frequency = 2 checkpoint.output_frequency = 2 with open(npt_equilibration._checkpoint_path, "w") as file: json.dump(checkpoint, file, cls=TypedJSONEncoder) npt_equilibration.execute(directory, ComputeResources()) assert len(npt_equilibration.observables) == 4 assert (len( mdtraj.load(npt_equilibration.trajectory_file_path, top=coordinate_path)) == 4)
def test_average_dielectric_constant(): with tempfile.TemporaryDirectory() as temporary_directory: average_observable = AverageDielectricConstant("") average_observable.dipole_moments = ObservableArray( np.zeros((1, 3)) * unit.elementary_charge * unit.nanometer) average_observable.volumes = ObservableArray( np.ones((1, 1)) * unit.nanometer**3) average_observable.thermodynamic_state = ThermodynamicState( 298.15 * unit.kelvin, 1.0 * unit.atmosphere) average_observable.bootstrap_iterations = 1 average_observable.execute(temporary_directory) assert np.isclose(average_observable.value.value, 1.0 * unit.dimensionless)
def test_compute_symmetry_correction(temperature, n_microstates): protocol = ComputeSymmetryCorrection("") protocol.thermodynamic_state = ThermodynamicState( temperature=temperature * unit.kelvin ) protocol.n_microstates = n_microstates protocol.execute() assert protocol.result != UNDEFINED assert numpy.isclose(protocol.result.error.magnitude, 0.0) expected_value = -protocol.thermodynamic_state.inverse_beta * numpy.log( n_microstates ) assert numpy.isclose(protocol.result.value, expected_value)
def test_same_component_batching(): thermodynamic_state = ThermodynamicState(temperature=1.0 * unit.kelvin, pressure=1.0 * unit.atmosphere) data_set = PhysicalPropertyDataSet() data_set.add_properties( Density( thermodynamic_state=thermodynamic_state, substance=Substance.from_components("O", "C"), value=0.0 * unit.kilogram / unit.meter**3, ), EnthalpyOfVaporization( thermodynamic_state=thermodynamic_state, substance=Substance.from_components("O", "C"), value=0.0 * unit.kilojoule / unit.mole, ), Density( thermodynamic_state=thermodynamic_state, substance=Substance.from_components("O", "CO"), value=0.0 * unit.kilogram / unit.meter**3, ), EnthalpyOfVaporization( thermodynamic_state=thermodynamic_state, substance=Substance.from_components("O", "CO"), value=0.0 * unit.kilojoule / unit.mole, ), ) options = RequestOptions() submission = EvaluatorClient._Submission() submission.dataset = data_set submission.options = options with DaskLocalCluster() as calculation_backend: server = EvaluatorServer(calculation_backend) batches = server._batch_by_same_component(submission, "") assert len(batches) == 2 assert len(batches[0].queued_properties) == 2 assert len(batches[1].queued_properties) == 2
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("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 = OpenMMReducedPotentials("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 reduced_potentials.execute(directory, ComputeResources()) 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 test_reweight_dielectric_constant(): with tempfile.TemporaryDirectory() as directory: reweight_protocol = ReweightDielectricConstant("") reweight_protocol.dipole_moments = ObservableArray( value=np.zeros((10, 3)) * unit.elementary_charge * unit.nanometers) reweight_protocol.volumes = ObservableArray(value=np.ones((10, 1)) * unit.nanometer**3) reweight_protocol.reference_reduced_potentials = [ ObservableArray(value=np.zeros(10) * unit.dimensionless) ] reweight_protocol.target_reduced_potentials = ObservableArray( value=np.zeros(10) * unit.dimensionless) reweight_protocol.thermodynamic_state = ThermodynamicState( 298.15 * unit.kelvin, 1.0 * unit.atmosphere) reweight_protocol.frame_counts = [10] reweight_protocol.bootstrap_uncertainties = True reweight_protocol.required_effective_samples = 0 reweight_protocol.execute(directory, ComputeResources())
def test_evaluate_energies_openmm(): substance = Substance.from_components("O") thermodynamic_state = ThermodynamicState(298 * unit.kelvin, 1.0 * unit.atmosphere) with tempfile.TemporaryDirectory() as directory: coordinate_path, parameterized_system = _setup_dummy_system(directory) reduced_potentials = OpenMMEvaluateEnergies("") reduced_potentials.substance = substance reduced_potentials.thermodynamic_state = thermodynamic_state reduced_potentials.parameterized_system = parameterized_system reduced_potentials.trajectory_file_path = get_data_filename( "test/trajectories/water.dcd") reduced_potentials.execute(directory, ComputeResources()) assert ObservableType.ReducedPotential in reduced_potentials.output_observables assert ObservableType.PotentialEnergy in reduced_potentials.output_observables
def test_run_openmm_simulation(): thermodynamic_state = ThermodynamicState(298 * unit.kelvin, 1.0 * unit.atmosphere) with tempfile.TemporaryDirectory() as directory: coordinate_path, parameterized_system = _setup_dummy_system(directory) npt_equilibration = OpenMMSimulation("npt_equilibration") npt_equilibration.steps_per_iteration = 2 npt_equilibration.output_frequency = 1 npt_equilibration.thermodynamic_state = thermodynamic_state npt_equilibration.input_coordinate_file = coordinate_path npt_equilibration.parameterized_system = parameterized_system npt_equilibration.execute(directory, ComputeResources()) assert path.isfile(npt_equilibration.output_coordinate_file) assert path.isfile(npt_equilibration.trajectory_file_path) assert len(npt_equilibration.observables) == 2
def test_compute_gradients(tmpdir, smirks, all_zeros): # Load a short trajectory. coordinate_path = get_data_filename("test/trajectories/water.pdb") trajectory_path = get_data_filename("test/trajectories/water.dcd") trajectory = mdtraj.load_dcd(trajectory_path, coordinate_path) observables = ObservableFrame({ "PotentialEnergy": ObservableArray( np.zeros(len(trajectory)) * unit.kilojoule / unit.mole) }) _compute_gradients( [ParameterGradientKey("vdW", smirks, "epsilon")], observables, ForceField("openff-1.2.0.offxml"), ThermodynamicState(298.15 * unit.kelvin, 1.0 * unit.atmosphere), Topology.from_mdtraj(trajectory.topology, [Molecule.from_smiles("O")]), trajectory, ComputeResources(), True, ) assert len( observables["PotentialEnergy"].gradients[0].value) == len(trajectory) if all_zeros: assert np.allclose( observables["PotentialEnergy"].gradients[0].value, 0.0 * unit.kilojoule / unit.kilocalorie, ) else: assert not np.allclose( observables["PotentialEnergy"].gradients[0].value, 0.0 * unit.kilojoule / unit.kilocalorie, )
def test_compute_reference_work(tmp_path, complex_file_path): # Generate a dummy set of pull restraints restraints_protocol = GeneratePullRestraints("") restraints_protocol.complex_coordinate_path = complex_file_path restraints_protocol.attach_lambdas = [0.0, 1.0] restraints_protocol.n_pull_windows = 2 restraints_protocol.restraint_schemas = { "guest": [ { "atoms": ":DM1 :7@C4", "attach": {"force_constant": 5, "target": 6}, "pull": {"force_constant": 5, "target": 24}, }, { "atoms": ":DM2 :DM1 :7@C4", "attach": {"force_constant": 100, "target": 180}, "pull": {"force_constant": 100, "target": 180}, }, { "atoms": ":DM1 :7@C4 :7@N1", "attach": {"force_constant": 100, "target": 180}, "pull": {"force_constant": 100, "target": 180}, }, ] } restraints_protocol.execute(str(tmp_path)) protocol = ComputeReferenceWork("") protocol.thermodynamic_state = ThermodynamicState(temperature=298.15 * unit.kelvin) protocol.restraints_path = restraints_protocol.restraints_path protocol.execute(str(tmp_path)) assert protocol.result != UNDEFINED assert numpy.isclose(protocol.result.error.magnitude, 0.0) assert numpy.isclose(protocol.result.value.magnitude, 7.141515)
def simple_evaluator_data_set(): """Create a simple evaluator `PhysicalPropertyDataSet` which contains a simple binary density measurement. Returns ------- PhysicalPropertyDataSet """ evaluator_density = Density( thermodynamic_state=ThermodynamicState(298.15 * unit.kelvin, pressure=1.0 * unit.atmosphere), phase=PropertyPhase.Liquid, substance=Substance.from_components("O", "CC=O"), value=1.0 * unit.kilogram / unit.meter**3, uncertainty=0.1 * unit.kilogram / unit.meter**3, source=MeasurementSource(doi="10.1000/xyz123"), ) evaluator_density.id = "1" evaluator_data_set = PhysicalPropertyDataSet() evaluator_data_set.add_properties(evaluator_density) return evaluator_data_set
def create_dummy_simulation_data( directory_path, substance, force_field_id="dummy_ff_id", coordinate_file_name="output.pdb", trajectory_file_name="trajectory.dcd", statistics_file_name="statistics.csv", statistical_inefficiency=1.0, phase=PropertyPhase.Liquid, number_of_molecules=1, calculation_id=None, ): """Creates a dummy `StoredSimulationData` object and the corresponding data directory. Parameters ---------- directory_path: str The path to the dummy data directory to create. substance: Substance force_field_id coordinate_file_name trajectory_file_name statistics_file_name statistical_inefficiency phase number_of_molecules calculation_id Returns ------- StoredSimulationData The dummy stored data object. """ os.makedirs(directory_path, exist_ok=True) data = StoredSimulationData() data.substance = substance data.force_field_id = force_field_id data.thermodynamic_state = ThermodynamicState(1.0 * unit.kelvin) data.property_phase = phase data.coordinate_file_name = coordinate_file_name data.trajectory_file_name = trajectory_file_name data.statistics_file_name = statistics_file_name with open(os.path.join(directory_path, coordinate_file_name), "w") as file: file.write("") with open(os.path.join(directory_path, trajectory_file_name), "w") as file: file.write("") with open(os.path.join(directory_path, statistics_file_name), "w") as file: file.write("") data.statistical_inefficiency = statistical_inefficiency data.number_of_molecules = number_of_molecules if calculation_id is None: calculation_id = str(uuid.uuid4()) data.source_calculation_id = calculation_id return data
def test_analysed_result_from_evaluator(): """Tests the `AnalysedResult.from_evaluator` function.""" expected_mean = 0.0 expected_std = numpy.random.rand() + 1.0 values = numpy.random.normal(expected_mean, expected_std, 1000) estimated_properties = [] reference_entries = [] for index, value in enumerate(values): property_id = index + 1 estimated_density = Density( thermodynamic_state=ThermodynamicState(298.15 * unit.kelvin, pressure=1.0 * unit.atmosphere), phase=PropertyPhase.Liquid, substance=Substance.from_components("O"), value=value * Density.default_unit(), uncertainty=0.0 * Density.default_unit(), ) estimated_density.id = str(property_id) estimated_properties.append(estimated_density) reference_density = DataSetEntry( id=property_id, property_type="Density", temperature=298.15, pressure=101.325, value=expected_mean, std_error=None, doi=" ", components=[Component(smiles="O", mole_fraction=1.0)], ) reference_entries.append(reference_density) estimated_data_set = PhysicalPropertyDataSet() estimated_data_set.add_properties(*estimated_properties) reference_data_set = DataSet( id="ref", description=" ", authors=[Author(name=" ", email="*****@*****.**", institute=" ")], entries=reference_entries, ) analysis_environments = [ChemicalEnvironment.Aqueous] analysed_results = DataSetResult.from_evaluator( reference_data_set=reference_data_set, estimated_data_set=estimated_data_set, analysis_environments=analysis_environments, statistic_types=[StatisticType.RMSE], bootstrap_iterations=1000, ) assert len(analysed_results.result_entries) == len(estimated_properties) full_statistics = next( iter(x for x in analysed_results.statistic_entries if x.category is None)) assert full_statistics.property_type == "Density" assert full_statistics.n_components == 1 assert full_statistics.statistic_type == StatisticType.RMSE assert numpy.isclose(full_statistics.value, expected_std, rtol=0.10)