def error_oracle(dt): integrator.setStepSize(dt * unit.femtosecond) W_F, W_R = sim.collect_protocol_samples(n_protocol_samples, protocol_length) DeltaF_neq, sq_unc = estimate_nonequilibrium_free_energy(W_F, W_R) print("\t{:.3f} +/- {:.3f}".format(DeltaF_neq, np.sqrt(sq_unc))) if error_threshold > np.abs(DeltaF_neq): return 1 else: return -1
def run(self): exp = self.experiment_descriptor simulator = NonequilibriumSimulator(exp.equilibrium_simulator, ContinuousLangevinSplittingIntegrator( splitting=exp.splitting_string, timestep=exp.timestep_in_fs * unit.femtosecond, collision_rate=exp.collision_rate)) self.result = simulator.collect_protocol_samples( exp.n_protocol_samples, exp.protocol_length, exp.marginal, store_potential_energy_traces=(exp.marginal == "full" and self.store_potential_energy_traces)) DeltaF_neq, squared_uncertainty = estimate_nonequilibrium_free_energy(self.result[0], self.result[1]) print(self) print("\t{:.3f} +/- {:.3f}".format(DeltaF_neq, np.sqrt(squared_uncertainty)))
def get_curves( fnames, timesteps, scheme_names, system_name, marginal="full", collision_rate_name="low", ): curves = {} # map from scheme_name to DeltaF_neq curve error_curves = {} # map from scheme_name to error curve for scheme in set(scheme_names): curves[scheme] = np.zeros(len(timesteps)) * np.nan error_curves[scheme] = np.zeros(len(timesteps)) * np.nan t_to_index = dict(zip(timesteps, range(len(timesteps)))) for fname in fnames: try: with open(fname, "rb") as f: x = load(f) if x["descriptor"]["splitting_name"] in scheme_names and x["descriptor"]["marginal"] == marginal and \ x["descriptor"]["collision_rate_name"] == collision_rate_name and x["descriptor"][ "system_name"] == system_name: scheme, t = x["descriptor"]["splitting_name"], x[ "descriptor"]["timestep_in_fs"] DeltaF_neq, err = estimate_nonequilibrium_free_energy( *x["result"]) i = t_to_index[t] n_dof = get_n_dof(system_name) curves[scheme][i] = DeltaF_neq / n_dof error_curves[scheme][i] = np.sqrt(err) / n_dof except: pass return curves, error_curves
if error_threshold > np.abs(DeltaF_neq): return 1 else: return -1 return error_oracle if __name__ == "__main__": test_system = alanine_constrained reference_integrator = LangevinSplittingIntegrator("O V R V O", timestep=2.0 * unit.femtosecond) reference_sim = NonequilibriumSimulator(test_system, reference_integrator) W_F, W_R = reference_sim.collect_protocol_samples(1000, 1000) DeltaF_neq, sq_unc = estimate_nonequilibrium_free_energy(W_F, W_R) results = {} for scheme in ["V R O R V", "R V O V R", "O V R V O", "O R V R O"]: print(scheme) error_oracle = error_oracle_factory(DeltaF_neq, scheme) results[scheme] = probabilistic_bisection(error_oracle, (0, 10), n_iterations=100) from pickle import dump with open("error_thresholds.pkl", "wb") as f: dump((results, DeltaF_neq, sq_unc), f)