def solve_VLE(self, Temp_VLE, show_plot=True): ''' Determine optimal values of mu that result in equal pressures by minimizing the square difference of the weights in the liquid and vapor phases. Subsequentally, calls the function to compute the saturation properties. ''' self.Temp_VLE = Temp_VLE self.build_MBAR_VLE_matrices() mu_VLE_guess, mu_lower_bound, mu_upper_bound = self.mu_guess_bounds() ### Optimization of mu mu_VLE_guess, mu_lower_bound, mu_upper_bound = self.mu_scan(Temp_VLE) mu_opt = GOLDEN_multi(self.sqdeltaW, mu_VLE_guess, mu_lower_bound, mu_upper_bound, TOL=0.00001, maxit=30) sqdeltaW_opt = self.sqdeltaW(mu_opt) self.f_k_opt = self.f_k_guess.copy() self.mu_opt = mu_opt self.calc_rhosat() self.calc_Psat() self.print_VLE() if show_plot: plt.plot(Temp_VLE, mu_opt, 'k-', label=r'$\mu_{\rm opt}$') plt.plot(self.Temp_sim, self.mu_sim, 'ro', mfc='None', label='Simulation') plt.plot(Temp_VLE, mu_VLE_guess, 'b--', label=r'$\mu_{\rm guess}$') plt.xlabel(r'$T$ (K)') plt.ylabel(r'$\mu_{\rm opt}$ (K)') plt.xlim([300, 550]) # plt.ylim([-4200,-3600]) plt.legend() plt.show() plt.plot(Temp_VLE, sqdeltaW_opt, 'ko') plt.xlabel(r'$T$ (K)') plt.ylabel(r'$(\Delta W^{\rm sat})^2$') plt.show() print("Effective number of samples") print(self.mbar.computeEffectiveSampleNumber()) print('\nWhich is approximately ' + str(self.mbar.computeEffectiveSampleNumber() / self.sumN_k * 100.) + '% of the total snapshots')
### Bounds for mu mu_sim_low = np.ones(len(Temp_VLE))*mu_sim.min() mu_sim_high = np.ones(len(Temp_VLE))*mu_sim.max() Temp_sim_mu_low = Temp_sim[np.argmin(mu_sim)] Temp_sim_mu_high = Temp_sim[np.argmax(mu_sim)] ### Guess for mu mu_guess = lambda Temp: mu_sim_high + (mu_sim_low - mu_sim_high)/(Temp_sim_mu_low-Temp_sim_mu_high) * (Temp-Temp_sim_mu_high) mu_VLE_guess = mu_guess(Temp_VLE) print(r'$(\Delta W)^2$ for $\mu_{\rm guess}$ =') print(sqdeltaW(mu_VLE_guess)) mu_opt = GOLDEN_multi(sqdeltaW,mu_VLE_guess,mu_sim_low,mu_sim_high,TOL=0.001,maxit=30) sqdeltaW_opt = sqdeltaW(mu_opt) plt.plot(Temp_VLE,mu_opt,'k-') plt.plot(Temp_sim,mu_sim,'ro',mfc='None') plt.xlabel(r'$T$ (K)') plt.ylabel(r'$\mu_{\rm opt}$ (K)') plt.xlim([300,550]) plt.ylim([-4200,-3600]) plt.show() plt.plot(Temp_VLE,sqdeltaW_opt,'ko') plt.xlabel(r'$T$ (K)') plt.ylabel(r'$(\Delta W^{\rm sat})^2$') plt.show()
mu_VLE_guess = mu_guess(Temp_VLE) mu_VLE_guess[mu_VLE_guess < mu_sim.min()] = mu_sim.min() mu_VLE_guess[mu_VLE_guess > mu_sim.max()] = mu_sim.max() print(mu_VLE_guess) mu_lower_bound = mu_sim_low * 1.005 mu_upper_bound = mu_sim_high * 0.995 print(r'$(\Delta W)^2$ for $\mu_{\rm guess}$ =') print(sqdeltaW(mu_VLE_guess)) ### Optimize mu mu_opt = GOLDEN_multi(sqdeltaW, mu_VLE_guess, mu_lower_bound, mu_upper_bound, TOL=0.0001, maxit=30) sqdeltaW_opt = sqdeltaW(mu_opt) plt.plot(Temp_VLE, mu_opt, 'k-', label=r'$\mu_{\rm opt}$') plt.plot(Temp_sim, mu_sim, 'ro', mfc='None', label='Simulation') plt.plot(Temp_VLE, mu_VLE_guess, 'b--', label=r'$\mu_{\rm guess}$') plt.xlabel(r'$T$ (K)') plt.ylabel(r'$\mu_{\rm opt}$ (K)') plt.xlim([300, 550]) plt.ylim([-4200, -3600]) plt.legend() plt.show() plt.plot(Temp_VLE, sqdeltaW_opt, 'ko')