def general_restraint_run(options): """ Generalized restraint simulation run to test free energy = standard state correction. options : Dict. A dictionary of substitutions for restraint_test_yaml """ with mmtools.utils.temporary_directory() as output_directory: # TODO refactor this to use AlchemicalPhase API rather than a YAML script. options['output_directory'] = output_directory # run both setup and experiment yaml_builder = experiment.ExperimentBuilder(restraint_test_yaml % options) yaml_builder.run_experiments() # Estimate Free Energies ncfile_path = os.path.join(output_directory, 'experiments', 'complex.nc') ncfile = netcdf.Dataset(ncfile_path, 'r') Deltaf_ij, dDeltaf_ij = analyze.estimate_free_energies(ncfile) # Correct the sign for the fact that we are adding vs removing the restraints DeltaF_simulated = Deltaf_ij[-1, 0] dDeltaF_simulated = dDeltaf_ij[-1, 0] DeltaF_restraints = ncfile.groups['metadata'].variables[ 'standard_state_correction'][0] ncfile.close() # Check if they are close assert np.allclose(DeltaF_restraints, DeltaF_simulated, rtol=dDeltaF_simulated)
def general_restraint_run(options): """ Generalized restraint simulation run to test free energy = standard state correction. options : Dict. A dictionary of substitutions for restraint_test_yaml """ with mmtools.utils.temporary_directory() as output_directory: # TODO refactor this to use AlchemicalPhase API rather than a YAML script. options['input_directory'] = get_data_filename( os.path.join('tests', 'data')) options['output_directory'] = output_directory # run both setup and experiment yaml_builder = experiment.ExperimentBuilder(restraint_test_yaml % options) yaml_builder.run_experiments() # Estimate Free Energies ncfile_path = os.path.join(output_directory, 'experiments', 'complex.nc') reporter = multistate.MultiStateReporter(ncfile_path, open_mode='r') #analyzer = multistate.MultiStateSamplerAnalyzer(reporter) analyzer = YankMultiStateSamplerAnalyzer(reporter) Deltaf_ij, dDeltaf_ij = analyzer.get_free_energy() # Correct the sign for the fact that we are adding vs removing the restraints DeltaF_simulated = Deltaf_ij[-1, 0] dDeltaF_simulated = dDeltaf_ij[-1, 0] print('Standard state correction:') #ncfile = netcdf.Dataset(ncfile_path, 'r') #print(ncfile.groups['metadata'].variables['standard_state_correction'][:]) #print(float(ncfile.groups['metadata'].variables['standard_state_correction'][:])) #ncfile.close() DeltaF_restraints = analyzer.get_standard_state_correction() # Check if they are close msg = '' msg += 'Computed: %8.3f kT\n' % (DeltaF_restraints) msg += 'Actual: %8.3f +- %8.3f kT\n' % (DeltaF_simulated, dDeltaF_simulated) msg += 'ERROR: %8.3f +- %8.3f kT\n' % ( DeltaF_restraints - DeltaF_simulated, dDeltaF_simulated) # DEBUG print(msg) assert np.allclose( DeltaF_restraints, DeltaF_simulated, rtol=2 * dDeltaF_simulated), 'Standard state correction is inaccurate.\n' + msg