Ejemplo n.º 1
0
def test_BVirial_Pitzer_Curl_calculus():
    from sympy import symbols, Rational, diff, lambdify, integrate
    # Derivatives check
    # Uses SymPy
    T, Tc, Pc, omega, R = symbols('T, Tc, Pc, omega, R')
    Tr = T / Tc
    B0 = Rational(1445, 10000) - Rational(33, 100) / Tr - Rational(
        1385, 10000) / Tr**2 - Rational(121, 10000) / Tr**3
    B1 = Rational(
        73, 1000) + Rational(46, 100) / Tr - Rational(1, 2) / Tr**2 - Rational(
            97, 1000) / Tr**3 - Rational(73, 10000) / Tr**8

    # Note: scipy.misc.derivative was attempted, but found to given too
    # incorrect of derivatives for higher orders, so there is no reasons to
    # implement it. Plus, no uses have yet been found for the higher
    # derivatives/integrals. For integrals, there is no way to get the
    # indefinite integral.

    # Test points in vector form for order 1, 2, and 3
    # Use lambdify for fast evaluation
    pts = 3  # points total = pts^4
    _Ts = linspace(5, 500, pts)
    _Tcs = linspace(501, 900, pts)
    _Pcs = linspace(2E5, 1E7, pts)
    _omegas = linspace(-1, 10, pts)

    _Ts, _Tcs, _Pcs, _omegas = np.meshgrid(_Ts, _Tcs, _Pcs, _omegas)
    _Ts, _Tcs, _Pcs, _omegas = _Ts.ravel(), _Tcs.ravel(), _Pcs.ravel(
    ), _omegas.ravel()
    for order in range(1, 4):
        B0c = diff(B0, T, order)
        B1c = diff(B1, T, order)
        Br = B0c + omega * B1c
        BVirial = (Br * R * Tc / Pc).subs(R, _R)
        f = lambdify((T, Tc, Pc, omega), BVirial, "numpy")

        Bcalcs = f(_Ts, _Tcs, _Pcs, _omegas)
        Bcalc2 = BVirial_Pitzer_Curl(_Ts, _Tcs, _Pcs, _omegas, order)
        assert_close1d(Bcalcs, Bcalc2)

    # Check integrals using SymPy:
    for order in range(-2, 0):
        B0c = B0
        B1c = B1
        for i in range(abs(order)):
            B0c = integrate(B0c, T)
            B1c = integrate(B1c, T)
        Br = B0c + omega * B1c
        BVirial = (Br * R * Tc / Pc).subs(R, _R)
        f = lambdify((T, Tc, Pc, omega), BVirial, "numpy")

        Bcalcs = f(_Ts, _Tcs, _Pcs, _omegas)
        Bcalc2 = [
            BVirial_Pitzer_Curl(T2, Tc2, Pc2, omega2, order)
            for T2, Tc2, Pc2, omega2 in zip(_Ts, _Tcs, _Pcs, _omegas)
        ]
        assert_close1d(Bcalcs, Bcalc2)
Ejemplo n.º 2
0
def test_sincos():
    N = 10**1
    for v in linspace(0.0, 2.0 * pi, N):
        a, b = sincos(v)
        assert_close(a, sin(v), rtol=1e-14)
        assert_close(b, cos(v), rtol=1e-14)
    for v in linspace(-100.0, 100.0, N):
        a, b = sincos(v)
        assert_close(a, sin(v), rtol=1e-14)
        assert_close(b, cos(v), rtol=1e-14)
Ejemplo n.º 3
0
def test_Smith():
    assert_close(Smith(.4, 800, 2.5), 0.959981235534199)
    
    # Quick test function, to ensure results are the same regardless of 
    # the form of the expression
    def Smith2(x, rhol, rhog):
        K = 0.4
        first = 1 + rhog/rhol*K*(1/x-1)
        second = rhog/rhol*(1-K)*(1/x-1)
        third = ((rhol/rhog + K*(1/x-1))/(1 + K*(1/x -1)))**0.5
        return (first + second*third)**-1

    alpha_1 = [Smith(i, 800, 2.5) for i in linspace(1E-9,.99)]
    alpha_2 = [Smith2(i, 800, 2.5) for i in linspace(1E-9, .99)]
    assert_close1d(alpha_1, alpha_2)
Ejemplo n.º 4
0
def test_NRTL_chemsep():
    from thermo.interaction_parameters import IPDB
    tausB = IPDB.get_ip_asymmetric_matrix('ChemSep NRTL',
                                          ['64-17-5', '7732-18-5'], 'bij')
    alphaC = IPDB.get_ip_asymmetric_matrix('ChemSep NRTL',
                                           ['64-17-5', '7732-18-5'], 'alphaij')
    N = 2
    T = 343.15
    xs = [0.252, 0.748]
    tausA = tausE = tausF = tausG = tausH = alphaD = [[0.0] * N
                                                      for i in range(N)]
    ABEFGHCD = (tausA, tausB, tausE, tausF, tausG, tausH, alphaC, alphaD)
    GE = NRTL(T=T, xs=xs, ABEFGHCD=ABEFGHCD)
    gammas = GE.gammas()
    assert_close1d(gammas, [1.985383485664009, 1.146380779201308], rtol=1e-7)

    # Table of values right from ChemSep
    gammas_ethanol = [
        5.66232, 4.283, 3.3749, 2.75365, 2.31475, 1.99622, 1.75984, 1.58121,
        1.44425, 1.33807, 1.25512, 1.19003, 1.13894, 1.09896, 1.06796, 1.0443,
        1.02671, 1.0142, 1.00598, 1.00142, 1
    ]
    gammas_water = [
        1, 1.00705, 1.02657, 1.05673, 1.09626, 1.14429, 1.20021, 1.26357,
        1.33405, 1.41139, 1.49541, 1.58593, 1.68281, 1.78591, 1.89509, 2.0102,
        2.1311, 2.25761, 2.38956, 2.52675, 2.66895
    ]
    pts = 21
    xs_ethanol = linspace(0, 1, 21)
    for i in range(pts):
        GE2 = GE.to_T_xs(T=T, xs=[xs_ethanol[i], 1 - xs_ethanol[i]])
        gammas = GE2.gammas()
        assert_close(gammas[0], gammas_ethanol[i], rtol=1e-5)
        assert_close(gammas[1], gammas_water[i], rtol=1e-5)
Ejemplo n.º 5
0
def test_polylog2():
    x = polylog2(0.5)
    assert_close(x, 0.5822405264516294)

    xs = linspace(0, 0.99999, 50)
    #    ys_act = [float(polylog(2, x)) for x in xs]
    #    from sympy import polylog
    ys_act = [
        0.0, 0.020513035768572635, 0.0412401364927588, 0.06218738124039796,
        0.08336114665629184, 0.10476812813791354, 0.12641536301777412,
        0.1483102559926201, 0.1704606070746889, 0.19287464238138674,
        0.21556104812821067, 0.23852900824703252, 0.261788246119884,
        0.2853490709994786, 0.309222429784819, 0.33341996493707843,
        0.3579540794622072, 0.38283801005840257, 0.4080859097363812,
        0.43371294147827794, 0.45973538481992837, 0.4861707576383267,
        0.5130379559237574, 0.540357414944675, 0.5681512960135646,
        0.596443704089335, 0.6252609427828415, 0.6546318150738004,
        0.6845879803506546, 0.7151643814663312, 0.7463997596771124,
        0.7783372810645774, 0.811025306032588, 0.8445183447984893,
        0.878878258148156, 0.9141757868110273, 0.9504925291123206,
        0.9879235426949309, 1.026580835488432, 1.0665981582977615,
        1.108137763432647, 1.1514002456979586, 1.1966394380910048,
        1.244186068718536, 1.2944877067946645, 1.3481819162579485,
        1.4062463083287482, 1.4703641942000052, 1.5441353484206717,
        1.644808936992927
    ]

    ys = [polylog2(x) for x in xs]
    assert_close1d(ys, ys_act, rtol=1E-7, atol=1E-10)
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
def test_turbulent_complicated():
    Nu1 = turbulent_Dittus_Boelter(1E5, 1.2, True, False)
    Nu2 = turbulent_Dittus_Boelter(Re=1E5,
                                   Pr=1.2,
                                   heating=False,
                                   revised=False)
    Nu3 = turbulent_Dittus_Boelter(Re=1E5, Pr=1.2, heating=False)
    Nu4 = turbulent_Dittus_Boelter(Re=1E5, Pr=1.2)
    Nu_values = [
        261.3838629346147, 279.89829163640354, 242.9305927410295,
        247.40036409449127
    ]
    assert_close1d([Nu1, Nu2, Nu3, Nu4], Nu_values)

    Nu1 = turbulent_Sieder_Tate(Re=1E5, Pr=1.2)
    Nu2 = turbulent_Sieder_Tate(1E5, 1.2, 0.01, 0.067)
    assert_close1d([Nu1, Nu2], [286.9178136793052, 219.84016455766044])

    Nus = [
        turbulent_entry_Hausen(1E5, 1.2, 0.154, i)
        for i in linspace(1e-3, 1, 11)
    ]
    Nus_values = [
        6464.503822124652, 505.67127136455525, 399.6147653094695,
        356.6182206114823, 332.39191624636305, 316.53483318707475,
        305.21220965431286, 296.6521831991236, 289.91358493027764,
        284.4463173972796, 279.90553997822707
    ]
    assert_close1d(Nus, Nus_values)
Ejemplo n.º 8
0
def test_liquid_jet_pump_ancillary_d_mixing():
    rhop=998.
    rhos=1098.
    Ks=0.11
    Kp=.04


    for rhos in [1098., 1100, 1200, 1600, 4000, 100]:
        for Ks in [1E-9, 1E-3, 0.11, .5, 1, 5, 10, 100, 1000]:
            for D_mult in linspace(0.1, 10, 100):
                solution_vars = {'P1': 426256.1597041593,
                 'P2': 133600,
                 'Qp': 0.01,
                 'd_mixing': 0.045,
                 'd_nozzle': 0.022382858811037732}
                solution_vars['d_mixing'] *= D_mult
                if solution_vars['d_mixing'] < solution_vars['d_nozzle']*1.43:
                    continue

                # Finish calculating good known values
                solution_vars['Qs'] = liquid_jet_pump_ancillary(rhop=rhop, rhos=rhos, Ks=Ks, Kp=Kp, **solution_vars)
                if solution_vars['Qs'].imag:
                    # Do not keep testing if obtained an imaginary flow rate
                    continue
                # Try each variable with the solver
                for key, value in solution_vars.items():
                    kwargs = dict(solution_vars)
                    del kwargs[key]
            #         print(solution_vars, key)

                    new_value = liquid_jet_pump_ancillary(rhop=rhop, rhos=rhos, Ks=Ks, Kp=Kp, **kwargs)
                    assert_close(new_value, value)
Ejemplo n.º 9
0
def test_VaporPressure_fitting2_dippr():
    pts = 10
    fit_issue_CASs = [
        '75-07-0',
        '107-02-8',
        '108-38-3',
        '7732-18-5',
        '85-44-9',
        '67-64-1',
        '78-87-5',
        '624-72-6',
        '118-96-7',
        '124-18-5',
        # '526-73-8'
    ]
    for CAS in fit_issue_CASs:
        obj = VaporPressure(CASRN=CAS)
        Ts = linspace(obj.Perrys2_8_Tmin, obj.Perrys2_8_Tmax, pts)
        props_calc = [obj.calculate(T, DIPPR_PERRY_8E) for T in Ts]
        res, stats = obj.fit_data_to_model(Ts=Ts,
                                           data=props_calc,
                                           model='DIPPR101',
                                           do_statistics=True,
                                           use_numba=False,
                                           multiple_tries=True,
                                           fit_method='lm')
        assert stats['MAE'] < 1e-5
Ejemplo n.º 10
0
def test_VaporPressure_fitting0():
    obj = VaporPressure(CASRN='13838-16-9')
    Tmin, Tmax = obj.WAGNER_POLING_Tmin, obj.WAGNER_POLING_Tmax
    Ts = linspace(Tmin, Tmax, 10)
    Ps = [obj(T) for T in Ts]
    Tc, Pc = obj.WAGNER_POLING_Tc, obj.WAGNER_POLING_Pc
    fitted = obj.fit_data_to_model(Ts=Ts,
                                   data=Ps,
                                   model='Wagner',
                                   use_numba=False,
                                   model_kwargs={
                                       'Tc': obj.WAGNER_POLING_Tc,
                                       'Pc': obj.WAGNER_POLING_Pc
                                   })
    res = fitted
    assert 'Tc' in res
    assert 'Pc' in res
    assert_close(res['a'], obj.WAGNER_POLING_coefs[0])
    assert_close(res['b'], obj.WAGNER_POLING_coefs[1])
    assert_close(res['c'], obj.WAGNER_POLING_coefs[2])
    assert_close(res['d'], obj.WAGNER_POLING_coefs[3])

    # Heavy compound fit
    Ts = linspace(179.15, 237.15, 5)
    props_calc = [Antoine(T, A=138., B=520200.0, C=3670.0) for T in Ts]
    res, stats = TDependentProperty.fit_data_to_model(
        Ts=Ts,
        data=props_calc,
        model='Antoine',
        do_statistics=True,
        use_numba=False,
        model_kwargs={'base': 10.0},
        fit_method='lm')
    assert stats['MAE'] < 1e-5

    # Fit with very low range and no C
    Ts = linspace(374, 377.0, 5)
    props_calc = [Antoine(T, A=12.852103, B=2942.98, C=0.0) for T in Ts]
    res, stats = TDependentProperty.fit_data_to_model(
        Ts=Ts,
        data=props_calc,
        model='Antoine',
        do_statistics=True,
        use_numba=False,
        model_kwargs={'base': 10.0},
        fit_method='lm')
    assert stats['MAE'] < 1e-5
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
def test_UNIQUAC_fitting_gamma_1_success_cases():
    pts = 10
    rs = [3.8254, 4.4998]
    qs = [3.316, 3.856]
    xs = [[xi, 1.0 - xi] for xi in linspace(1e-7, 1 - 1e-7, pts)]
    gammas = [[1, 1] for i in range(pts)]
    coeffs, stats = UNIQUAC.regress_binary_parameters(gammas, xs, rs, qs)
    assert stats['MAE'] < 1e-5
Ejemplo n.º 13
0
def test_fit_cheb_poly():
    eos = PR(Tc=507.6, Pc=3025000.0, omega=0.2975, T=400., P=1E6)

    coeffs_linear_short = fit_cheb_poly(eos.Psat, 350, 370, 10)
    for T in linspace(350, 370, 30):
        assert_close(eos.Psat(T), horner(coeffs_linear_short, T), rtol=1e-9)



    # Test transformation of the output only
    coeffs_log_wide = fit_cheb_poly(eos.Psat, 200, 400, 15, interpolation_property=lambda x: log(x),
                                            interpolation_property_inv=lambda x: exp(x))

    for T in linspace(200, 400, 30):
        assert_close(eos.Psat(T), exp(horner(coeffs_log_wide, T)), rtol=1e-9)

    # Test ability to have other arguments depend on it
    coeffs_linear_short_under_P = fit_cheb_poly(lambda T, P: eos.to(T=T, P=P).V_l, 350, 370, 7,
                                                arg_func=lambda T: (eos.Psat(T)*1.1,))

    for T in linspace(350, 370, 30):
        P = eos.Psat(T)*1.1
        assert_close(eos.to(T=T, P=P).V_l, horner(coeffs_linear_short_under_P, T), rtol=1e-9)

    # Test ability to have other arguments depend on it
    coeffs_log_short_above_P = fit_cheb_poly(lambda T, P: eos.to(T=T, P=P).V_g, 350, 370, 7,
                                             arg_func=lambda T: (eos.Psat(T)*.7,),
                                             interpolation_property=lambda x: log(x),
                                             interpolation_property_inv=lambda x: exp(x))
    for T in linspace(350, 370, 30):
        P = eos.Psat(T)*0.7
        assert_close(eos.to(T=T, P=P).V_g, exp(horner(coeffs_log_short_above_P, T)), rtol=1e-9)


    # test interpolation_x
    Tc = 750.0
    coeffs_linear_short_SMK_x_trans = fit_cheb_poly(lambda T: SMK(T, Tc=Tc, omega=0.04), 200, 748, 20,
                                            interpolation_x=lambda T: log(1. - T/Tc),
                                            interpolation_x_inv=lambda x: -(exp(x)-1.0)*Tc)
    for T in linspace(200, 748, 30):
        x =  log(1. - T/Tc)
        assert_close(SMK(T, Tc=Tc, omega=0.04), horner(coeffs_linear_short_SMK_x_trans, x), rtol=1e-7)

    # Case with one coefficient and no T bounds
    assert_close1d(fit_cheb_poly(func=lambda T: 102.5, low=298.15, high=298.15, n=1), [102.5])
Ejemplo n.º 14
0
def test_Laliberte_heat_capacity_w():
    from scipy.interpolate import interp1d
    _T_array = [-15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140]
    _Cp_array = [4294.03, 4256.88, 4233.58, 4219.44, 4204.95, 4195.45, 4189.1, 4184.8, 4181.9, 4180.02, 4178.95, 4178.86, 4178.77, 4179.56, 4180.89, 4182.77, 4185.17, 4188.1, 4191.55, 4195.52, 4200.01, 4205.02, 4210.57, 4216.64, 4223.23, 4230.36, 4238.07, 4246.37, 4255.28, 4264.84, 4275.08, 4286.04]
    Laliberte_heat_capacity_w_interp = interp1d(_T_array, _Cp_array, kind='cubic')
    for T in linspace(_T_array[0], 92.0, 1000):
        assert_close(Laliberte_heat_capacity_w_interp(T),
                     Laliberte_heat_capacity_w(T+273.15),
                     rtol=1e-5)
Ejemplo n.º 15
0
def test_VaporPressure_linear_extrapolation_non_negative():
    ethanol_psat = VaporPressure(Tb=351.39,
                                 Tc=514.0,
                                 Pc=6137000.0,
                                 omega=0.635,
                                 CASRN='64-17-5')

    # Make sure the constants are set to guard against future changes to defaults
    ethanol_psat.method = WAGNER_MCGARRY
    ethanol_psat.interpolation_T = (lambda T: 1 / T)
    ethanol_psat.interpolation_property = (lambda P: log(P))
    ethanol_psat.interpolation_property_inv = (lambda P: exp(P))
    ethanol_psat.extrapolation = 'linear'

    assert_close(ethanol_psat(700), 59005875.32878946, rtol=1e-4)
    assert_close(ethanol_psat(100), 1.0475828451230242e-11, rtol=1e-4)

    assert ethanol_psat.T_limits['WAGNER_MCGARRY'][
        0] == ethanol_psat.WAGNER_MCGARRY_Tmin
    assert ethanol_psat.T_limits['WAGNER_MCGARRY'][
        1] == ethanol_psat.WAGNER_MCGARRY_Tc

    assert_close(
        ethanol_psat.T_dependent_property(ethanol_psat.WAGNER_MCGARRY_Tc),
        ethanol_psat.T_dependent_property(ethanol_psat.WAGNER_MCGARRY_Tc -
                                          1e-6))
    assert_close(
        ethanol_psat.T_dependent_property(ethanol_psat.WAGNER_MCGARRY_Tc),
        ethanol_psat.T_dependent_property(ethanol_psat.WAGNER_MCGARRY_Tc +
                                          1e-6))

    assert_close(
        ethanol_psat.T_dependent_property(ethanol_psat.WAGNER_MCGARRY_Tmin),
        ethanol_psat.T_dependent_property(ethanol_psat.WAGNER_MCGARRY_Tmin -
                                          1e-6))
    assert_close(
        ethanol_psat.T_dependent_property(ethanol_psat.WAGNER_MCGARRY_Tmin),
        ethanol_psat.T_dependent_property(ethanol_psat.WAGNER_MCGARRY_Tmin +
                                          1e-6))

    Tmin = ethanol_psat.T_limits[ethanol_psat.method][0]
    Ts = linspace(0.7 * Tmin, Tmin * (1 - 1e-10), 10)
    Ps = [ethanol_psat(T) for T in Ts]

    # Confirms it's linear
    # plt.plot(1/np.array(Ts), np.log(Ps))
    # plt.show()
    #    def rsquared(x, y):
    #        import scipy.stats
    #        _, _, r_value, _, _ = scipy.stats.linregress(x, y)
    #        return r_value*r_value

    #    assert_close(rsquared(1/np.array(Ts), np.log(Ps)), 1, atol=1e-5)
    assert abs(np.polyfit(1 / np.array(Ts), np.log(Ps), 1,
                          full=True)[1][0]) < 1e-13
Ejemplo n.º 16
0
def test_VaporPressure_fitting9_Yaws_Psat():
    A, B, C, D, E, Tmin, Tmax = 53.93890302013294, -788.24, -22.734, 0.051225, 6.1896e-11, 68.15, 132.92
    Ts = linspace(Tmin, Tmax, 10)
    props_calc = [Yaws_Psat(T, A, B, C, D, E) for T in Ts]
    res, stats = VaporPressure.fit_data_to_model(Ts=Ts,
                                                 data=props_calc,
                                                 model='Yaws_Psat',
                                                 do_statistics=True,
                                                 use_numba=False,
                                                 fit_method='lm')
    assert stats['MAE'] < 1e-5
Ejemplo n.º 17
0
def test_ViscosityLiquid_fitting2():
    # yaws points
    A, B, C, D = -25.532, 3747.2, 0.0466, 0.0
    Ts = linspace(489.2, 619.65)
    props = [mu_Yaws(T, A, B, C, D) for T in Ts]
    res, stats = ViscosityLiquid.fit_data_to_model(Ts=Ts,
                                                   data=props,
                                                   model='mu_Yaws',
                                                   do_statistics=True,
                                                   use_numba=False,
                                                   fit_method='lm')
    assert stats['MAE'] < 1e-5
Ejemplo n.º 18
0
def test_SurfaceTension_fitting0_yaws():
    A, Tc, n = 117.684, 7326.47, 1.2222
    Ts = linspace(378.4, 7326.47, 10)
    props_calc = [EQ106(T, Tc, A, n, 0.0, 0.0, 0.0, 0) for T in Ts]
    res, stats = SurfaceTension.fit_data_to_model(Ts=Ts,
                                                  data=props_calc,
                                                  model='YawsSigma',
                                                  do_statistics=True,
                                                  use_numba=False,
                                                  model_kwargs={'Tc': Tc},
                                                  fit_method='lm')
    assert stats['MAE'] < 1e-5
Ejemplo n.º 19
0
def test_one_phase_dP_dz_acceleration_example():
    # This requires thermo!
    from thermo import Stream, Vm_to_rho
    from fluids import one_phase_dP, one_phase_dP_acceleration
    import numpy as np
    from scipy.integrate import odeint
    from fluids.numerics import assert_close

    P0 = 1E5
    s = Stream(['nitrogen', 'methane'], T=300, P=P0, zs=[0.5, 0.5], m=1)
    rho0 = s.rho
    D = 0.1

    def dP_dz(P, L, acc=False):
        s.flash(P=float(P), Hm=s.Hm)
        dPf = one_phase_dP(m=s.m,
                           rho=s.rhog,
                           mu=s.rhog,
                           D=D,
                           roughness=0,
                           L=1.0)

        if acc:
            G = 4.0 * s.m / (pi * D * D)
            der = s.VolumeGasMixture.property_derivative_P(P=s.P,
                                                           T=s.T,
                                                           zs=s.zs,
                                                           ws=s.ws)
            der = 1 / Vm_to_rho(der, s.MW)
            factor = G * G * der
            dP = dPf / (1.0 + factor)
            return -dP
        return -dPf

    ls = linspace(0, .01)

    dP_noacc = odeint(dP_dz, s.P, ls, args=(False, ))[-1]
    s.flash(P=float(P0), Hm=s.Hm)  # Reset the stream object
    profile = odeint(dP_dz, s.P, ls, args=(True, ))

    dP_acc = profile[-1]

    s.flash(P=dP_acc, Hm=s.Hm)
    rho1 = s.rho

    dP_acc_numerical = dP_noacc - dP_acc
    dP_acc_basic = one_phase_dP_acceleration(m=s.m,
                                             D=D,
                                             rho_o=rho1,
                                             rho_i=rho0)

    assert_close(dP_acc_basic, dP_acc_numerical, rtol=1E-4)
Ejemplo n.º 20
0
def test_linspace():
    from fluids.numerics import linspace
    calc = linspace(-3, 10, endpoint=True, num=8)
    expect = np.linspace(-3, 10, endpoint=True, num=8)
    assert_allclose(calc, expect)

    calc = linspace(-3, 10, endpoint=False, num=20)
    expect = np.linspace(-3, 10, endpoint=False, num=20)
    assert_allclose(calc, expect)

    calc = linspace(0, 1e-10, endpoint=False, num=3)
    expect = np.linspace(0, 1e-10, endpoint=False, num=3)
    assert_allclose(calc, expect)

    calc = linspace(0, 1e-10, endpoint=False, num=2)
    expect = np.linspace(0, 1e-10, endpoint=False, num=2)
    assert_allclose(calc, expect)

    calc = linspace(0, 1e-10, endpoint=False, num=1)
    expect = np.linspace(0, 1e-10, endpoint=False, num=1)
    assert_allclose(calc, expect)

    calc, calc_step = linspace(0, 1e-10, endpoint=False, num=2, retstep=True)
    expect, expect_step = np.linspace(0,
                                      1e-10,
                                      endpoint=False,
                                      num=2,
                                      retstep=True)
    assert_allclose(calc, expect)
    assert_allclose(calc_step, expect_step)

    calc, calc_step = linspace(0, 1e-10, endpoint=False, num=1, retstep=True)
    expect, expect_step = np.linspace(0,
                                      1e-10,
                                      endpoint=False,
                                      num=1,
                                      retstep=True)
    assert_allclose(calc, expect)
    assert isnan(calc_step)
    # Cannot compare against numpy expect_step - it did not use to give nan in older versions

    calc, calc_step = linspace(100, 1000, endpoint=False, num=21, retstep=True)
    expect, expect_step = np.linspace(100,
                                      1000,
                                      endpoint=False,
                                      num=21,
                                      retstep=True)
    assert_allclose(calc, expect)
    assert_allclose(calc_step, expect_step)
Ejemplo n.º 21
0
def test_ThermalConductivityLiquid_fitting1_dippr100():
    for i, CAS in enumerate(
            chemicals.thermal_conductivity.k_data_Perrys_8E_2_315.index):
        obj = ThermalConductivityLiquid(CASRN=CAS)
        Ts = linspace(obj.Perrys2_315_Tmin, obj.Perrys2_315_Tmax, 10)
        props_calc = [obj.calculate(T, DIPPR_PERRY_8E) for T in Ts]
        res, stats = obj.fit_data_to_model(Ts=Ts,
                                           data=props_calc,
                                           model='DIPPR100',
                                           do_statistics=True,
                                           use_numba=False,
                                           fit_method='lm')
        assert stats['MAE'] < 1e-8
Ejemplo n.º 22
0
    def _custom_set_poly_fit(self):
        try:
            Tmin, Tmax = self.poly_fit_Tmin, self.poly_fit_Tmax
            poly_fit_coeffs = self.poly_fit_coeffs
            v_Tmin = horner(poly_fit_coeffs, Tmin)
            for T_trans in linspace(Tmin, Tmax, 25):
                v, d1, d2 = horner_and_der2(poly_fit_coeffs, T_trans)
                Psat = exp(v)
                dPsat_dT = Psat * d1
                d2Psat_dT2 = Psat * (d1 * d1 + d2)

                A, B, C = Antoine_ABC = Antoine_coeffs_from_point(T_trans,
                                                                  Psat,
                                                                  dPsat_dT,
                                                                  d2Psat_dT2,
                                                                  base=e)
                self.poly_fit_AB = list(
                    Antoine_AB_coeffs_from_point(T_trans,
                                                 Psat,
                                                 dPsat_dT,
                                                 base=e))
                self.DIPPR101_ABC = list(
                    DIPPR101_ABC_coeffs_from_point(T_trans, Psat, dPsat_dT,
                                                   d2Psat_dT2))

                B_OK = B > 0.0  # B is negated in this implementation, so the requirement is reversed
                C_OK = -T_trans < C < 0.0
                if B_OK and C_OK:
                    self.poly_fit_Antoine = Antoine_ABC
                    break
                else:
                    continue

            # Calculate the extrapolation values
            v_Tmax = horner(poly_fit_coeffs, Tmax)
            v, d1, d2 = horner_and_der2(poly_fit_coeffs, Tmax)
            Psat = exp(v)
            dPsat_dT = Psat * d1
            d2Psat_dT2 = Psat * (d1 * d1 + d2)
            #                A, B, C = Antoine_ABC = Antoine_coeffs_from_point(T_trans, Psat, dPsat_dT, d2Psat_dT2, base=e)
            self.poly_fit_AB_high = list(
                Antoine_AB_coeffs_from_point(Tmax, Psat, dPsat_dT, base=e))
            self.poly_fit_AB_high_ABC_compat = [
                self.poly_fit_AB_high[0], -self.poly_fit_AB_high[1]
            ]
            self.DIPPR101_ABC_high = list(
                DIPPR101_ABC_coeffs_from_point(Tmax, Psat, dPsat_dT,
                                               d2Psat_dT2))

        except:
            pass
Ejemplo n.º 23
0
def test_linspace():
    from fluids.numerics import linspace
    calc = linspace(-3, 10, endpoint=True, num=8)
    expect = np.linspace(-3, 10, endpoint=True, num=8)
    assert_allclose(calc, expect)

    calc = linspace(-3, 10, endpoint=False, num=20)
    expect = np.linspace(-3, 10, endpoint=False, num=20)
    assert_allclose(calc, expect)

    calc = linspace(0, 1e-10, endpoint=False, num=3)
    expect = np.linspace(0, 1e-10, endpoint=False, num=3)
    assert_allclose(calc, expect)

    calc = linspace(0, 1e-10, endpoint=False, num=2)
    expect = np.linspace(0, 1e-10, endpoint=False, num=2)
    assert_allclose(calc, expect)

    calc = linspace(0, 1e-10, endpoint=False, num=1)
    expect = np.linspace(0, 1e-10, endpoint=False, num=1)
    assert_allclose(calc, expect)

    calc, calc_step = linspace(0, 1e-10, endpoint=False, num=2, retstep=True)
    expect, expect_step = np.linspace(0,
                                      1e-10,
                                      endpoint=False,
                                      num=2,
                                      retstep=True)
    assert_allclose(calc, expect)
    assert_allclose(calc_step, expect_step)

    calc, calc_step = linspace(0, 1e-10, endpoint=False, num=1, retstep=True)
    expect, expect_step = np.linspace(0,
                                      1e-10,
                                      endpoint=False,
                                      num=1,
                                      retstep=True)
    assert_allclose(calc, expect)
    assert_allclose(calc_step, expect_step)

    calc, calc_step = linspace(100, 1000, endpoint=False, num=21, retstep=True)
    expect, expect_step = np.linspace(100,
                                      1000,
                                      endpoint=False,
                                      num=21,
                                      retstep=True)
    assert_allclose(calc, expect)
    assert_allclose(calc_step, expect_step)
Ejemplo n.º 24
0
def test_EnthalpyVaporization_fitting3_dippr_106_full():
    for CAS in chemicals.phase_change.phase_change_data_Perrys2_150.index:
        obj = EnthalpyVaporization(CASRN=CAS)
        Ts = linspace(obj.Perrys2_150_Tmin, obj.Perrys2_150_Tmax, 8)
        props_calc = [obj.calculate(T, DIPPR_PERRY_8E) for T in Ts]
        res, stats = obj.fit_data_to_model(
            Ts=Ts,
            data=props_calc,
            model='DIPPR106',
            do_statistics=True,
            use_numba=False,
            fit_method='lm',
            model_kwargs={'Tc': obj.Perrys2_150_coeffs[0]})
        assert stats['MAE'] < 1e-7
Ejemplo n.º 25
0
def test_Permittivity_class():
    # Test some cases
    water = PermittivityLiquid(CASRN='7732-18-5')
    assert (False, False) == (water.test_property_validity(2000),
                              water.test_property_validity(-10))

    with pytest.raises(Exception):
        water.test_method_validity(300, 'BADMETHOD')

    epsilon = water.T_dependent_property(298.15)
    assert_close(epsilon, 78.35530812232503)
    assert PermittivityLiquid(CASRN='7732-18-5').all_methods == set(
        ['CRC', 'CRC_CONSTANT'])

    assert PermittivityLiquid(
        CASRN='132451235-2151234-1234123').all_methods == set()
    assert PermittivityLiquid(
        CASRN='132451235-2151234-1234123').T_dependent_property(300) is None

    assert False == PermittivityLiquid(CASRN='7732-18-5').test_method_validity(
        228.15, 'CRC_CONSTANT')
    assert False == PermittivityLiquid(CASRN='7732-18-5').test_method_validity(
        228.15, 'CRC')

    # Tabular data
    w = PermittivityLiquid(CASRN='7732-18-5')
    Ts = linspace(273, 372, 10)
    permittivities = [
        87.75556413000001, 83.530500320000016, 79.48208925000003,
        75.610330919999996, 71.915225330000013, 68.396772480000024,
        65.05497237000003, 61.889825000000044, 58.901330369999997, 56.08948848
    ]
    w.add_tabular_data(Ts=Ts, properties=permittivities)
    assert_close(w.T_dependent_property(305.), 75.95500925000006)
    w.extrapolation = 'interp1d'
    assert_close(w.T_dependent_property(200.), 115.79462395999997)

    w.extrapolation_coeffs.clear()
    assert w.T_dependent_property(800.0) is not None

    w.extrapolation = None
    assert w.T_dependent_property(200.) is None

    assert PermittivityLiquid.from_json(w.as_json()) == w

    # Case where a nan was stored
    obj = PermittivityLiquid(CASRN='57-10-3')
    assert not isnan(obj.CRC_Tmin)
    assert not isnan(obj.CRC_Tmax)
Ejemplo n.º 26
0
def test_VaporPressure_fitting5_AntoinePoling():
    for i, CAS in enumerate(
            chemicals.vapor_pressure.Psat_data_AntoinePoling.index):
        obj = VaporPressure(CASRN=CAS)
        Ts = linspace(obj.ANTOINE_POLING_Tmin, obj.ANTOINE_POLING_Tmax, 10)
        props_calc = [obj.calculate(T, ANTOINE_POLING) for T in Ts]
        res, stats = obj.fit_data_to_model(Ts=Ts,
                                           data=props_calc,
                                           model='Antoine',
                                           do_statistics=True,
                                           use_numba=False,
                                           multiple_tries=True,
                                           model_kwargs={'base': 10.0},
                                           fit_method='lm')
        assert stats['MAE'] < 1e-7
Ejemplo n.º 27
0
def test_EnthalpyVaporization_fitting2_dippr_106():

    for i, CAS in enumerate(
            chemicals.phase_change.phase_change_data_VDI_PPDS_4.index):
        obj = EnthalpyVaporization(CASRN=CAS)
        Ts = linspace(obj.T_limits[VDI_PPDS][0], obj.T_limits[VDI_PPDS][1], 8)
        props_calc = [obj.calculate(T, VDI_PPDS) for T in Ts]
        res, stats = obj.fit_data_to_model(
            Ts=Ts,
            data=props_calc,
            model='PPDS12',
            do_statistics=True,
            use_numba=False,
            fit_method='lm',
            model_kwargs={'Tc': obj.VDI_PPDS_Tc})
        assert stats['MAE'] < 1e-7
Ejemplo n.º 28
0
def test_VaporPressure_fitting6_VDI_PPDS():
    for i, CAS in enumerate(
            chemicals.vapor_pressure.Psat_data_VDI_PPDS_3.index):
        obj = VaporPressure(CASRN=CAS)
        Ts = linspace(obj.VDI_PPDS_Tm, obj.VDI_PPDS_Tc, 10)
        props_calc = [obj.calculate(T, VDI_PPDS) for T in Ts]
        res, stats = obj.fit_data_to_model(Ts=Ts,
                                           data=props_calc,
                                           model='Wagner',
                                           do_statistics=True,
                                           use_numba=False,
                                           fit_method='lm',
                                           model_kwargs={
                                               'Tc': obj.VDI_PPDS_Tc,
                                               'Pc': obj.VDI_PPDS_Pc
                                           })
        assert stats['MAE'] < 1e-7
Ejemplo n.º 29
0
def test_VaporPressure_fitting3_WagnerMcGarry():
    for i, CAS in enumerate(
            chemicals.vapor_pressure.Psat_data_WagnerMcGarry.index):
        obj = VaporPressure(CASRN=CAS)
        Ts = linspace(obj.WAGNER_MCGARRY_Tmin, obj.WAGNER_MCGARRY_Tc, 10)
        props_calc = [obj.calculate(T, WAGNER_MCGARRY) for T in Ts]
        res, stats = obj.fit_data_to_model(Ts=Ts,
                                           data=props_calc,
                                           model='Wagner_original',
                                           do_statistics=True,
                                           use_numba=False,
                                           fit_method='lm',
                                           model_kwargs={
                                               'Tc': obj.WAGNER_MCGARRY_Tc,
                                               'Pc': obj.WAGNER_MCGARRY_Pc
                                           })
        assert stats['MAE'] < 1e-7
Ejemplo n.º 30
0
def test_VolumeLiquid_fitting1_dippr():
    fit_check_CASs = [
        '124-38-9', '74-98-6', '1333-74-0', '630-08-0', '100-21-0', '624-92-0',
        '624-72-6', '74-86-2', '115-07-1', '64-18-6'
    ]
    for CAS in fit_check_CASs:
        obj = VolumeLiquid(CASRN=CAS)
        Ts = linspace(obj.DIPPR_Tmin, obj.DIPPR_Tmax, 8)
        props_calc = [1.0 / obj.calculate(T, DIPPR_PERRY_8E) for T in Ts]

        res, stats = obj.fit_data_to_model(Ts=Ts,
                                           data=props_calc,
                                           model='DIPPR105',
                                           do_statistics=True,
                                           use_numba=False,
                                           fit_method='lm')
        assert stats['MAE'] < 1e-5