def test_fuzz_K_to_discharge_coefficient(): ''' # Testing the different formulas from sympy import * C, beta, K = symbols('C, beta, K') expr = Eq(K, (sqrt(1 - beta**4*(1 - C*C))/(C*beta**2) - 1)**2) solns = solve(expr, C) [i.subs({'K': 5.2314291729754, 'beta': 0.05/0.07366}) for i in solns] [-sqrt(-beta**4/(-2*sqrt(K)*beta**4 + K*beta**4) + 1/(-2*sqrt(K)*beta**4 + K*beta**4)), sqrt(-beta**4/(-2*sqrt(K)*beta**4 + K*beta**4) + 1/(-2*sqrt(K)*beta**4 + K*beta**4)), -sqrt(-beta**4/(2*sqrt(K)*beta**4 + K*beta**4) + 1/(2*sqrt(K)*beta**4 + K*beta**4)), sqrt(-beta**4/(2*sqrt(K)*beta**4 + K*beta**4) + 1/(2*sqrt(K)*beta**4 + K*beta**4))] # Getting the formula from sympy import * C, beta, K = symbols('C, beta, K') expr = Eq(K, (sqrt(1 - beta**4*(1 - C*C))/(C*beta**2) - 1)**2) print(latex(solve(expr, C)[3])) ''' Ds = logspace(log10(1-1E-9), log10(1E-9), 8) for D_ratio in Ds: Ks = logspace(log10(1E-9), log10(50000), 8) Ks_recalc = [] for K in Ks: C = K_to_discharge_coefficient(D=1.0, Do=D_ratio, K=K) K_calc = discharge_coefficient_to_K(D=1.0, Do=D_ratio, C=C) Ks_recalc.append(K_calc) assert_close1d(Ks, Ks_recalc)
def test_Colebrook_numerical_mpmath(): # tested at n=500 for both Re and eD Res = logspace(log10(1e-12), log10(1E12), 30) # 1E12 is too large for sympy - it slows down too much eDs = logspace(log10(1e-20), log10(.1), 21) # 1-1e-9 for Re in Res: for eD in eDs: fd_exact = Colebrook(Re, eD, tol=0) fd_numerical = Colebrook(Re, eD, tol=1e-12) assert_close(fd_exact, fd_numerical, rtol=1e-5)
def test_Colebrook_vs_Clamond(): Res = logspace(log10(10), log10(1E50), 40) eDs = logspace(log10(1e-20), log10(1), 40) for Re in Res: for eD in eDs: fd_exact = Colebrook(Re, eD) fd_clamond = Clamond(Re, eD) # Interestingly, matches to rtol=1e-9 vs. numerical solver # But does not have such accuracy compared to mpmath if isnan(fd_exact) or isnan(fd_clamond): continue # older scipy on 3.4 returns a nan sometimes assert_close(fd_exact, fd_clamond, rtol=1e-9)
def test_logspace(): from fluids.numerics import logspace calc = logspace(3,10, endpoint=True, num=8) expect = np.logspace(3,10, endpoint=True, num=8) assert_allclose(calc, expect) calc = logspace(3,10, endpoint=False, num=20) expect = np.logspace(3,10, endpoint=False, num=20) assert_allclose(calc, expect) calc = logspace(0,1e-10, endpoint=False, num=3) expect = np.logspace(0,1e-10, endpoint=False, num=3) assert_allclose(calc, expect) calc = logspace(0,1e-10, endpoint=False, num=2) expect = np.logspace(0,1e-10, endpoint=False, num=2) assert_allclose(calc, expect) calc = logspace(0,1e-10, endpoint=False, num=1) expect = np.logspace(0,1e-10, endpoint=False, num=1) assert_allclose(calc, expect) calc = logspace(0,1e-10, endpoint=False, num=2) expect = np.logspace(0,1e-10, endpoint=False, num=2) assert_allclose(calc, expect) calc = logspace(0,1e-10, endpoint=False, num=1) expect = np.logspace(0,1e-10, endpoint=False, num=1) assert_allclose(calc, expect) calc = logspace(100, 200, endpoint=False, num=21) expect = np.logspace(100, 200, endpoint=False, num=21) assert_allclose(calc, expect)
def test_Colebrook_scipy_mpmath(): # Faily grueling test - check the lambertw implementations are matching mostly # NOTE the test is to Re = 1E7; at higher Res the numerical solver is almost # always used Res = logspace(log10(1e-12), log10(1e7), 20) # 1E12 is too large for sympy eDs = logspace(log10(1e-20), log10(.1), 19) # 1-1e-9 for Re in Res: for eD in eDs: Re = float(Re) eD = float(eD) fd_exact = Colebrook(Re, eD, tol=0) fd_scipy = Colebrook(Re, eD) assert_close(fd_exact, fd_scipy, rtol=1e-9)
def test_PSDLognormal_dn_order_0_1_2(): '''Simple point to test where the order of n should be 0 Yes, the integrals need this many points (which makes them slow) to get the right accuracy. They've been tested and reduced already quite a bit. ''' from scipy.integrate import quad # test 2, 0 -> 2, 0 disc = PSDLognormal(s=0.5, d_characteristic=5E-6) to_int = lambda d: d**2 * disc.pdf(d=d, n=0) points = [5E-6 * i for i in logspace(log10(.1), log10(50), 8)] ans_numerical = (quad(to_int, 1E-7, 5E-3, points=points)[0])**0.5 ans_analytical = 3.0326532985631672e-06 # The integral is able to give over to decimals! assert_close(ans_numerical, ans_analytical, rtol=1E-10) # test 2, 1 -> 1, 1 integrated pdf to_int = lambda d: d * disc.pdf(d=d, n=1) ans_numerical = (quad(to_int, 1E-7, 5E-3, points=points)[0])**1 ans_analytical = 3.4364463939548618e-06 assert_close(ans_numerical, ans_analytical, rtol=1E-10) # test 3, 2 -> 1, 2 integrated pdf to_int = lambda d: d * disc.pdf(d=d, n=2) ans_numerical = (quad(to_int, 1E-7, 5E-3, points=points)[0])**1 ans_analytical = 4.4124845129229773e-06 assert_close(ans_numerical, ans_analytical, rtol=1E-8)
def test_pdf_lognormal_basis_integral_fuzz(): from scipy.integrate import quad # The actual integral testing analytical_vales = [] numerical_values = [] for n in [-3, -2, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]: # Errors at -2 (well, prevision loss anyway) for d_max in [ 1e-3, 1e-2, 2e-2, 3e-2, 4e-2, 5e-2, 6e-2, 7e-2, 8e-2, 1e-1 ]: d_max = d_max / 100 # Make d smaller analytical = (pdf_lognormal_basis_integral( d_max, d_characteristic=1E-5, s=1.1, n=n) - pdf_lognormal_basis_integral( 1e-20, d_characteristic=1E-5, s=1.1, n=n)) to_int = lambda d: d**n * pdf_lognormal( d, d_characteristic=1E-5, s=1.1) points = logspace(log10(d_max / 1000), log10(d_max * .999), 40) numerical = quad(to_int, 1e-9, d_max, points=points)[0] # points=points analytical_vales.append(analytical) numerical_values.append(numerical) assert_close1d(analytical_vales, numerical_values, rtol=2E-6)
def func_vs_naive_tester(func, func_naive, T_min=1.0, T_max=5000.0, rho_min=1e-5, rho_max=50000.0, N=400, Tc=132.6312, rhoc=10447.7): errs = [] rerr = 0 Ts = linspace(T_min, T_max, N) rhoc_inv = 1.0 / rhoc for i, T in enumerate(Ts): tau = Tc / T rhos = logspace(log10(rho_min), log10(rho_max), N) for rho in rhos: delta = rho * rhoc_inv val = func(tau, delta) val_naive = func_naive(tau, delta) rerri = abs(1.0 - val / val_naive) rerr += rerri errs.append(rerri) AARD, std, max_err = rerr / N**2, np.std(errs), np.max(errs) return AARD, std, max_err
def test_Prandtl_von_Karman_Nikuradse_full(): # Tested to a very high number of points fds = [] fds_numeric = [] for Re in logspace(1E-15, 30, 40): fds.append(Prandtl_von_Karman_Nikuradse_numeric(Re)) fds_numeric.append(Prandtl_von_Karman_Nikuradse(Re)) assert_close1d(fds, fds_numeric)
def test_Lastovka_Shaw_T_for_Hm_fuzz(): T_ref = 298.15 factor = 1.0 # Originally tested with 50 points in everything similarity_variables = linspace(.05, .5, 8) MWs = linspace(12, 1200, 8) Hms = [-i for i in logspace(log10(1e4), log10(1), 15)] + logspace(log10(1), log10(1e7), 15) for sv in similarity_variables: for MW in MWs: for Hm in Hms: try: Lastovka_Shaw_T_for_Hm(Hm=Hm, MW=MW, similarity_variable=sv, T_ref=T_ref) except Exception as e: if 'negative temperature' in str(e): continue
def test_Nu_sphere_Churchill(): Nu_Res = [Nu_sphere_Churchill(.7, i) for i in logspace(0, 10, 11)] Nu_Res_values = [ 2.415066377224484, 2.7381040025746382, 3.3125553308635283, 4.3340933312726548, 6.1507272232235417, 9.3821675084055443, 15.145453144794978, 25.670869440317578, 47.271761310748289, 96.479305628419823, 204.74310854292045 ] assert_close1d(Nu_Res, Nu_Res_values)
def test_pdf_Rosin_Rammler_basis_integral_fuzz(): from scipy.integrate import quad for n in [1.0, 2.0, 3.0]: # Lower d_maxes have for d_max in [ 1e-3, 1e-2, 2e-2, 3e-2, 4e-2, 5e-2, 6e-2, 7e-2, 8e-2, 1e-1 ]: analytical = (pdf_Rosin_Rammler_basis_integral(d_max, 200, 2, n) - pdf_Rosin_Rammler_basis_integral(1E-20, 200, 2, n)) to_int = lambda d: d**n * pdf_Rosin_Rammler(d, 200, 2) points = logspace(log10(d_max / 2000), log10(d_max * .999), 30) numerical = quad(to_int, 1E-20, d_max, points=points)[0] assert_close(analytical, numerical, rtol=1E-5)
def test_Nu_cylinder_Zukauskas(): Nu = Nu_cylinder_Zukauskas(7992, 0.707, 0.69) assert_allclose(Nu, 50.523612661934386) Nus_allRe = [ Nu_cylinder_Zukauskas(Re, 0.707, 0.69) for Re in logspace(0, 6, 8) ] Nus_allRe_values = [ 0.66372630070423799, 1.4616593536687801, 3.2481853039940831, 8.7138930573143227, 26.244842388228189, 85.768869004450067, 280.29503021904566, 1065.9610995854582 ] assert_allclose(Nus_allRe, Nus_allRe_values) Nu_highPr = Nu_cylinder_Zukauskas(7992, 42.) assert_allclose(Nu_highPr, 219.24837219760443)
import matplotlib.pyplot as plt import numpy as np from thermo import * from fluids.numerics import logspace from math import log10 thing = PR(P=1e5, T=460, Tc=658.0, Pc=1820000.0, omega=0.9) Ts = logspace(log10(1000), log10(2000000), 100) Ps = [] for T in Ts: try: Ps.append(thing.to(T=T, V=thing.V_l).P) except ValueError: Ps.append(1e5) plt.loglog(Ts, Ps) plt.xlabel('Temperature [K]') plt.ylabel('Pressure [Pa]') plt.title("Constant-volume calculated pressure at V=%.8f m^3/mol" % (thing.V_l)) #plt.show()