def solve_x(x_gamma_poyinting, gamma, poyinting, T, x_guess): if x_guess is None: x_guess = x_gamma_poyinting args = (x_gamma_poyinting, gamma, poyinting, T) try: x = flx.aitken(x_iter, x_guess, 1e-10, args=args, checkiter=False) except InfeasibleRegion: x = x_gamma_poyinting return x
def solve_y(y_phi, phi, T, P, y_guess): if isinstance(phi, IdealFugacityCoefficients): return y_phi elif y_guess is None: y_guess = y_phi return flx.aitken(y_iter, y_phi, 1e-9, args=(y_phi, phi, T, P), checkiter=False)
def _y_iter(self, y, Psats_over_P, T, P): phi = self._phi(y, T, P) Psat_over_P_phi = Psats_over_P / phi f = self._x_iter args = (Psat_over_P_phi, ) x = flx.aitken(f, self._x, 1e-12, args, checkiter=False) self._x = f(x, *args) v = self._F_mol_vle * self._V * x * self._Ks return fn.normalize(v)
def xsolve_T(self, phase_mol, H, T_guess, P): """Solve for temperature in Kelvin.""" args = (H, self.xH, tuple(phase_mol), P, self.xCn(phase_mol, T_guess)) return flx.aitken(xiter_temperature, T_guess, 1e-6, args, 10, checkiter=False)
def solve_T(self, phase, mol, H, T_guess, P): """Solve for temperature in Kelvin.""" args = (H, self.H, phase, mol, P, self.Cn(phase, mol, T_guess)) return flx.aitken(iter_temperature, T_guess, 1e-6, args, 10, checkiter=False)
def _solve_distillate_recoveries(self): distillate_recoveries = self._estimate_distillate_recoveries() try: distillate_recoveries = flx.aitken( self._recompute_distillate_recoveries, distillate_recoveries, 1e-4) except flx.SolverError as error: distillate_recoveries = self._recompute_distillate_recoveries( error.x) except flx.InfeasibleRegion: pass self._update_distillate_recoveries(distillate_recoveries)
def test_fixedpoint_array_solvers2(): original_feed = feed.copy() p = flx.Profiler(f2) solution = flx.wegstein(p, feed, xtol=1e-8, maxiter=200) assert_allclose(solution, real_solution2) p.archive('Wegstein') with pytest.raises(RuntimeError): solution = flx.wegstein(f2, feed, convergenceiter=4, xtol=1e-8, maxiter=200) with pytest.raises(RuntimeError): solution = flx.wegstein(f2, feed, xtol=1e-8, maxiter=20) with pytest.raises(RuntimeError): solution = flx.aitken(f2, feed, convergenceiter=4, xtol=1e-8, maxiter=200) solution = flx.wegstein(p, feed, checkconvergence=False, convergenceiter=4, xtol=1e-8) p.archive('Wegstein early termination') assert_allclose(solution, real_solution2, rtol=1e-3) solution = flx.aitken(p, feed, xtol=1e-8, maxiter=10000) assert_allclose(feed, original_feed) assert_allclose(solution, real_solution2) p.archive('Aitken') solution = flx.aitken(p, feed, checkconvergence=False, convergenceiter=4, xtol=1e-8) assert_allclose(feed, original_feed) assert_allclose(solution, real_solution2, rtol=1e-3) p.archive('Aitken early termination') solution = flx.fixed_point(p, feed, xtol=5e-8, maxiter=200) assert_allclose(feed, original_feed) assert_allclose(solution, real_solution2) p.archive('Fixed point') solution = flx.fixed_point(p, feed, maxiter=500, checkconvergence=False, convergenceiter=4, xtol=5e-8) assert_allclose(feed, original_feed) assert_allclose(solution, real_solution2, rtol=1e-3) p.archive('Fixed point early termination') assert p.sizes() == {'Wegstein': 61, 'Wegstein early termination': 18, 'Aitken': 392, 'Aitken early termination': 91, 'Fixed point': 191, 'Fixed point early termination': 191}
def solve_x(x_gamma_poyinting, gamma, poyinting, T, x_guess): if x_guess is None: x_guess = x_gamma_poyinting args = (x_gamma_poyinting, gamma, poyinting, T) try: x = flx.aitken(x_iter, x_guess, 1e-8, args=args) except flx.SolverError as solver_error: try: x = flx.fixed_point(x_iter, solver_error.x, 1e-8, args=args) except: x = x_gamma_poyinting except flx.InfeasibleRegion: x = x_gamma_poyinting return x
def _solve_x(self, T): solute_chemical = self.chemicals.tuple[self._solute_index] Tm = solute_chemical.Tm if Tm is None: raise RuntimeError(f"solute {solute_chemical} does not have a melting temperature, Tm") Cpl = solute_chemical.Cn.l(T) Cps = solute_chemical.Cn.s(T) Hm = solute_chemical.Hfus if Hm is None: raise RuntimeError(f"solute {solute_chemical} does not have a heat of fusion, Hfus") gamma = 1. x = solubility_eutectic(T, Tm, Hm, Cpl, Cps, gamma) # Initial guess args = (T, Tm, Hm, Cpl, Cps) if isinstance(self._gamma, IdealActivityCoefficients): return solubility_eutectic(T, Tm, Hm, Cpl, Cps, self.activity_coefficient or 1.) return flx.aitken(self._x_iter, x, xtol=1e-6, args=args, checkiter=False, maxiter=100)
def _y_iter(self, y, Psats_over_P, T, P): phi = self._phi(y, T, P) Psat_over_P_phi = Psats_over_P / phi try: x = flx.wegstein(self._x_iter, self._x, 1e-12, args=(Psat_over_P_phi, )) except flx.SolverError as solver_error: x = flx.aitken(self._x_iter, solver_error.x, 1e-6, args=(Psat_over_P_phi, )) self._x = x v = self._F_mol_vle * self._V * x * self._Ks return fn.normalize(v)
def _solve_v(self, T, P): """Solve for vapor mol""" Psats_over_P = np.array([i(T) for i in self._bubble_point.Psats]) / P self._T = T v = self._v y = fn.normalize(v) l = self._mol - v self._x = fn.normalize(l) if isinstance(self._phi, IdealFugacityCoefficients): self._y = self._y_iter(y, Psats_over_P, T, P) else: self._y = flx.aitken(self._y_iter, v / v.sum(), 1e-12, args=(Psats_over_P, T, P), checkiter=False) self._v = self._F_mol_vle * self._V * self._y return self._v
def test_fixedpoint_array_solvers(): original_feed = feed.copy() p = flx.Profiler(f) solution = flx.wegstein(p, feed, convergenceiter=4, xtol=1e-8, maxiter=200) assert_allclose(feed, original_feed) assert_allclose(solution, real_solution) p.archive('Wegstein') solution = flx.aitken(p, feed, convergenceiter=4, xtol=1e-8, maxiter=200) assert_allclose(feed, original_feed) assert_allclose(solution, real_solution) p.archive('Aitken') solution = flx.fixed_point(p, feed, convergenceiter=4, xtol=5e-8, maxiter=200) assert_allclose(feed, original_feed) assert_allclose(solution, real_solution) p.archive('Fixed point') assert p.sizes() == {'Wegstein': 5, 'Aitken': 5, 'Fixed point': 194}
def _solve_distillate_recoveries(self): distillate_recoveries = self._distillate_recoveries flx.aitken(self._recompute_distillate_recoveries, distillate_recoveries, 1e-8, checkiter=False)
p.archive('[Scipy] Brent-Q') # Save/archive results with given name x_brenth = opt.brenth(p, x0, x1) p.archive('[Scipy] Brent-H') x_IQ = flx.IQ_interpolation(p, x0, x1) p.archive('IQ-interpolation') x_false_position = flx.false_position(p, x0, x1) p.archive('False position') p.plot(r'$f(x) = 0 = x^3 + 2 \cdot x - 40$ where $-5 < x < 5$') p = flx.Profiler(f) x_guess = -5 x_aitken_secant = flx.aitken_secant(p, x_guess) p.archive('Aitken') x_secant = flx.secant(p, x_guess) p.archive('Secant') x_newton = opt.newton(p, x_guess) p.archive('[Scipy] Newton') p.plot(r'$f(x) = 0 = x^3 + 2 \cdot x - 40$') # Note that x = 40/x^2 - 2/x is the same # objective function as x**3 - 40 + 2*x = 0 f = lambda x: 40/x**2 - 2/x p = flx.Profiler(f) x_guess = 5. x_wegstein = flx.wegstein(p, x_guess) p.archive('Wegstein') x_aitken = flx.aitken(p, x_guess) p.archive('Aitken') p.plot(r'$f(x) = x = \frac{40}{x^2} - \frac{2}{x}$', markbounds=False) # ^ Fixed-point iteration is non-convergent for this equation; so we don't include it here