Example #1
0
def test_atom_matrix():
    atomss = [{
        'C': 1,
        'H': 4
    }, {
        'C': 2,
        'H': 6
    }, {
        'N': 2
    }, {
        'O': 2
    }, {
        'H': 2,
        'O': 1
    }, {
        'C': 1,
        'O': 2
    }]
    OCH_expect = [[0.0, 1, 4], [0.0, 2, 6], [0.0, 0.0, 0.0], [2, 0.0, 0.0],
                  [1, 0.0, 2], [2, 1, 0.0]]

    default_expect = [[4, 1, 0.0, 0.0], [6, 2, 0.0, 0.0], [0.0, 0.0, 2, 0.0],
                      [0.0, 0.0, 0.0, 2], [2, 0.0, 0.0, 1], [0.0, 1, 0.0, 2]]
    default = atom_matrix(atomss)

    assert_close2d(default, default_expect, rtol=1e-12)

    OCH = atom_matrix(atomss, ['O', 'C', 'H'])
    assert_close2d(OCH, OCH_expect, rtol=1e-12)
Example #2
0
def test_d2ns_to_dn2_partials():
    d2ns = [[0.152, 0.08, 0.547], [0.08, 0.674, 0.729], [0.547, 0.729, 0.131]]
    res = d2ns_to_dn2_partials(d2ns, [20.0, .124, 900.52])
    res_expect = [[40.152, 20.203999999999997, 921.067],
                  [20.204, 0.922, 901.3729999999999],
                  [921.067, 901.373, 1801.1709999999998]]
    assert_close2d(res, res_expect)
Example #3
0
def test_regular_solution_gammas_binaries_jac():
    kwargs = dict(xs=[.1, .9, 0.3, 0.7, .85, .15],
                  Vs=[7.421e-05, 8.068e-05],
                  SPs=[19570.2, 18864.7],
                  Ts=[300.0, 400.0, 500.0],
                  lambda12=0.1759,
                  lambda21=0.7991)

    res = regular_solution_gammas_binaries_jac(**kwargs)
    res_expect = [[61651.76714001304, 61651.76714001304],
                  [0.1134949564269304, 0.1134949564269304],
                  [265.5654802072593, 265.5654802072593],
                  [1.4404516377618741, 1.4404516377618741],
                  [0.20175156664075444, 0.20175156664075444],
                  [694.1461546179326, 694.1461546179326]]
    assert_close2d(res, res_expect, rtol=1e-13)

    for i in range(len(res)):
        for j in range(len(res[i])):
            res[i][j] = -1e100
    jac = res
    res = regular_solution_gammas_binaries_jac(jac=res, **kwargs)
    assert res is jac
    assert_close2d(res, res_expect, rtol=1e-13)
    '''
Example #4
0
def test_air_cooler_Ft():
    Ft_1 = Ft_aircooler(Thi=93, Tho=52, Tci=35, Tco=54.59, Ntp=2, rows=4)
    assert_close(Ft_1, 0.9570456123827129)

    # Example 2 as in [1]_; author rounds to obtain a slightly different result.
    Ft_2 = Ft_aircooler(Thi=125., Tho=45., Tci=25., Tco=95., Ntp=1, rows=4)
    assert_close(Ft_2, 0.5505093604092708)
    Ft_many = [[
        Ft_aircooler(Thi=125., Tho=80., Tci=25., Tco=95., Ntp=i, rows=j)
        for i in range(1, 6)
    ] for j in range(1, 6)]
    Ft_values = [[
        0.6349871996666123, 0.9392743008890244, 0.9392743008890244,
        0.9392743008890244, 0.9392743008890244
    ],
                 [
                     0.7993839562360742, 0.9184594715750571,
                     0.9392743008890244, 0.9392743008890244, 0.9392743008890244
                 ],
                 [
                     0.8201055328279105, 0.9392743008890244,
                     0.9784008071402877, 0.9392743008890244, 0.9392743008890244
                 ],
                 [
                     0.8276966706732202, 0.9392743008890244,
                     0.9392743008890244, 0.9828365967034366, 0.9392743008890244
                 ],
                 [
                     0.8276966706732202, 0.9392743008890244,
                     0.9392743008890244, 0.9392743008890244, 0.9828365967034366
                 ]]
    assert_close2d(Ft_many, Ft_values)
Example #5
0
def test_ViscosityGas():
    EtOH = ViscosityGas(MW=46.06844,
                        Tc=514.0,
                        Pc=6137000.0,
                        Zc=0.2412,
                        dipole=1.44,
                        Vmg=0.02357,
                        CASRN='64-17-5')

    EtOH.method = VDI_PPDS
    assert_close(EtOH.T_dependent_property(305), 9.13079342291875e-06)
    EtOH.method = YOON_THODOS
    assert_close(EtOH.T_dependent_property(305), 7.584977640806432e-06)
    EtOH.method = STIEL_THODOS
    assert_close(EtOH.T_dependent_property(305), 7.699272212598415e-06)
    EtOH.method = GHARAGHEIZI
    assert_close(EtOH.T_dependent_property(305), 8.12922040122556e-06)
    EtOH.method = LUCAS_GAS
    assert_close(EtOH.T_dependent_property(305), 9.008160864895763e-06)
    EtOH.method = VDI_TABULAR
    assert_close(EtOH.T_dependent_property(305), 8.761390047621239e-06)
    EtOH.method = DIPPR_PERRY_8E
    assert_close(EtOH.T_dependent_property(305), 9.129674918795814e-06)

    # Test that methods return None
    EtOH.extrapolation = None
    for i in EtOH.all_methods:
        EtOH.method = i
        assert EtOH.T_dependent_property(6000) is None

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

    s = EtOH.as_json()
    EtOH2 = ViscosityGas.from_json(s)
    # Do hash checks before interpolation object exists
    assert hash(EtOH) == hash(EtOH2)
    assert EtOH == EtOH2
    assert id(EtOH) != id(EtOH2)

    # Ethanol data, calculated from CoolProp
    Ts = [400, 500, 550]
    Ps = [1E3, 1E4, 1E5]
    TP_data = [[
        1.18634700291489e-05, 1.4762189560203758e-05, 1.6162732753470533e-05
    ], [
        1.1862505513959454e-05, 1.4762728590964208e-05, 1.6163602669178767e-05
    ], [1.1853229260926176e-05, 1.4768417536555742e-05, 1.617257402798515e-05]]
    EtOH.add_tabular_data_P(Ts, Ps, TP_data, name='CPdata')
    recalc_pts = [[EtOH.TP_dependent_property(T, P) for T in Ts] for P in Ps]
    assert_close2d(TP_data, recalc_pts)

    EtOH.tabular_extrapolation_permitted = False
    assert None == EtOH.TP_dependent_property(300, 9E4)
    EtOH.tabular_extrapolation_permitted = True
    assert_close(EtOH.TP_dependent_property(300, 9E4), 1.1854259955707653e-05)

    with pytest.raises(Exception):
        EtOH.test_method_validity_P(300, 1E5, 'BADMETHOD')
Example #6
0
def assert_pint_allclose2d(value, magnitude, units, rtol=1e-7, atol=0):
    assert_close2d(value.to_base_units().magnitude,
                   magnitude,
                   rtol=rtol,
                   atol=atol)
    if type(units) != dict:
        units = dict(units.dimensionality)
    assert dict(value.dimensionality) == units
Example #7
0
def test_Nu_vertical_cylinder_Carne_Morgan():
    Nu_all = [[
        Nu_vertical_cylinder_Carne_Morgan(i, 2E8, j)
        for i in (0.999999, 1.000001)
    ] for j in (True, False, None)]
    Nu_all_values = [[216.88764905616722, 216.88781389084312],
                     [225.77302655456344, 225.77315298749372],
                     [225.77302655456344, 216.88781389084312]]
    assert_close2d(Nu_all, Nu_all_values)
Example #8
0
def test_Nu_vertical_cylinder_Jakob_Linke_Morgan():
    Nu_all = [[
        Nu_vertical_cylinder_Jakob_Linke_Morgan(i, 1E8, j)
        for i in (0.999999, 1.000001)
    ] for j in (True, False, None)]
    Nu_all_values = [[59.87647599476619, 59.87651591243016],
                     [55.499986124994805, 55.5000138749948],
                     [55.499986124994805, 59.87651591243016]]
    assert_close2d(Nu_all, Nu_all_values)
Example #9
0
def test_Nu_vertical_cylinder_Griffiths_Davis_Morgan():
    Nu_all = [[
        Nu_vertical_cylinder_Griffiths_Davis_Morgan(i, 1E9, j)
        for i in (0.999999, 1.000001)
    ] for j in (True, False, None)]
    Nu_all_values = [[127.7046167347578, 127.7047079158867],
                     [119.14469068641654, 119.14475025877677],
                     [119.14469068641654, 127.7047079158867]]
    assert_close2d(Nu_all, Nu_all_values)
Example #10
0
def test_stoichiometric_matrix():
    res = stoichiometric_matrix([{
        'Mg': 1,
        'O': 1
    }, {
        'Mg': 1
    }, {
        'O': 2
    }], [True, False, False])
    assert_close2d([[1, -1, 0], [1, 0, -2]], res)
Example #11
0
def test_no_interactions():
    GE = RegularSolution(T=325.0,
                         xs=[.25, .75],
                         Vs=[7.421e-05, 8.068e-05],
                         SPs=[19570.2, 18864.7])
    assert_close2d(GE.lambda_coeffs, [[0, 0], [0, 0]], atol=0, rtol=0)

    GE = RegularSolution(T=325.0,
                         xs=np.array([.25, .75]),
                         Vs=np.array([7.421e-05, 8.068e-05]),
                         SPs=np.array([19570.2, 18864.7]))
    assert_close2d(GE.lambda_coeffs, [[0, 0], [0, 0]], atol=0, rtol=0)
    assert type(GE.lambda_coeffs) is np.ndarray
Example #12
0
def test_a_alpha_aijs_composition_independent():
    kijs = [[0, .083], [0.083, 0]]
    a_alphas = [0.2491099357671155, 0.6486495863528039]
    a_alpha_ijs, a_alpha_roots, a_alpha_ij_roots_inv = a_alpha_aijs_composition_independent(
        a_alphas, kijs)

    assert_close2d(a_alpha_ijs, [[0.2491099357671155, 0.3686123937424334],
                                 [0.3686123937424334, 0.6486495863528038]],
                   rtol=1e-13)
    assert_close1d(a_alpha_roots, [0.4991091421393877, 0.8053878484015039],
                   rtol=1e-13)
    assert_close1d(a_alpha_ij_roots_inv,
                   [[4.014291910599931, 2.4877079977965977],
                    [2.4877079977965977, 1.5416644379945614]],
                   rtol=1e-13)
Example #13
0
def check_numba_np_output_activity(model, modelnp, modelnp2):
    # model is flat, scalar, list-based model
    # modelnp is numba model
    # modelnp2 is created from the numba model with to_T_xs at a different composition
    vec_attrs = [
        'dGE_dxs', 'gammas', '_gammas_dGE_dxs', 'd2GE_dTdxs', 'dHE_dxs',
        'gammas_infinite_dilution', 'dHE_dns', 'dnHE_dns', 'dSE_dxs',
        'dSE_dns', 'dnSE_dns', 'dGE_dns', 'dnGE_dns', 'd2GE_dTdns',
        'd2nGE_dTdns', 'dgammas_dT'
    ]

    for attr in vec_attrs:
        assert_close1d(getattr(model, attr)(),
                       getattr(modelnp, attr)(),
                       rtol=1e-13)
        assert_close1d(getattr(modelnp2, attr)(),
                       getattr(modelnp, attr)(),
                       rtol=1e-13)
        assert type(getattr(model, attr)()) is list
        assert type(getattr(modelnp, attr)()) is np.ndarray
        assert type(getattr(modelnp2, attr)()) is np.ndarray

    mat_attrs = ['d2GE_dxixjs', 'd2nGE_dninjs', 'dgammas_dns']
    for attr in mat_attrs:
        assert_close2d(getattr(model, attr)(),
                       getattr(modelnp, attr)(),
                       rtol=1e-13)
        assert_close2d(getattr(modelnp2, attr)(),
                       getattr(modelnp, attr)(),
                       rtol=1e-13)
        assert type(getattr(model, attr)()) is list
        assert type(getattr(modelnp, attr)()) is np.ndarray
        assert type(getattr(modelnp2, attr)()) is np.ndarray

    attrs_3d = ['d3GE_dxixjxks']
    for attr in attrs_3d:
        if hasattr(model, attr):
            # some models do not have this implemented
            assert_close3d(getattr(model, attr)(),
                           getattr(modelnp, attr)(),
                           rtol=1e-13)
            assert_close3d(getattr(modelnp2, attr)(),
                           getattr(modelnp, attr)(),
                           rtol=1e-13)
            assert type(getattr(model, attr)()) is list
            assert type(getattr(modelnp, attr)()) is np.ndarray
            assert type(getattr(modelnp2, attr)()) is np.ndarray
Example #14
0
def test_K_separator_Watkins():
    calc = [[
        K_separator_Watkins(0.88, 985.4, 1.3, horizontal, method)
        for method in ['spline', 'branan', 'blackwell']
    ] for horizontal in [False, True]]

    expect = [[0.06361290880381038, 0.06108986837654085, 0.06994527471072351],
              [0.07951613600476297, 0.07636233547067607, 0.0874315933884044]]

    assert_close2d(calc, expect, rtol=1e-4)

    with pytest.raises(Exception):
        K_separator_Watkins(0.88,
                            985.4,
                            1.3,
                            horizontal=True,
                            method='BADMETHOD')
def test_PSDLognormal_cdf_orders():
    # Test cdf of different orders a bunch
    disc = PSDLognormal(s=0.5, d_characteristic=5E-6)
    # 16 x 4 = 64 points
    # had 1e-7 here too as a diameter but too many numerical issues, too sensitive to rounding
    # errors
    ds = [
        2E-6, 3E-6, 4E-6, 5E-6, 6E-6, 7E-6, 1E-5, 2E-5, 3E-5, 5E-5, 7E-5, 1E-4,
        2E-4, 4E-4, 1E-3
    ]
    ans_expect = [
        [
            0.36972511868508068, 0.68379899882263917, 0.8539928088656249,
            0.93319279873114203, 0.96888427729983861, 0.98510775165387254,
            0.99805096305713792, 0.9999903391682271, 0.99999981474719135,
            0.99999999948654394, 0.99999999999391231, 0.99999999999996592, 1.0,
            1.0, 1.0
        ],
        [
            0.20254040832522924, 0.49136307673913149, 0.71011232639847854,
            0.84134474606854293, 0.91381737643345484, 0.95283088619207579,
            0.99149043874391107, 0.99991921875653167, 0.99999771392273817,
            0.99999998959747816, 0.99999999982864851, 0.99999999999863987, 1.0,
            1.0, 1.0000000000000002
        ],
        [
            0.091334595732478097, 0.30095658738958564, 0.52141804648990697,
            0.69146246127401301, 0.80638264936531323, 0.87959096325267294,
            0.9703723506333426, 0.999467162897961, 0.99997782059383122,
            0.99999983475152954, 0.99999999622288382, 0.99999999995749711,
            0.99999999999999833, 1.0000000000000002, 1.0000000000000002
        ],
        [
            0.033432418408916864, 0.15347299656473007, 0.3276949357115424, 0.5,
            0.64231108623683952, 0.74950869138681098, 0.91717148099830148,
            0.99721938213769046, 0.99983050191355338, 0.99999793935660408,
            0.99999993474010451, 0.99999999896020164, 0.99999999999991951, 1.0,
            1.0
        ]
    ]

    calc = []
    for n in range(0, 4):
        calc.append([disc.cdf(i, n=n) for i in ds])

    assert_close2d(ans_expect, calc, rtol=1E-9)
Example #16
0
def test_a_alpha_aijs_composition_independent():
    # TODO: a_alpha_aijs_composition_independent is being overwritten in thermo.numba somehow!

    kijs = np.array([[0, .083], [0.083, 0]])
    a_alphas = np.array([0.2491099357671155, 0.6486495863528039])
    a0, a1, a2 = thermo.numba.eos_mix_methods.a_alpha_aijs_composition_independent(
        a_alphas, kijs)
    assert type(a0) is np.ndarray
    assert type(a1) is np.ndarray
    assert type(a2) is np.ndarray

    b0, b1, b2 = thermo.eos_mix_methods.a_alpha_aijs_composition_independent(
        a_alphas, kijs)
    assert_close1d(a1, b1, rtol=1e-13)
    assert_close2d(a0, b0, rtol=1e-13)
    assert_close2d(a2, b2, rtol=1e-13)

    assert thermo.numba.eos_mix_methods.a_alpha_aijs_composition_independent is not thermo.eos_mix_methods.a_alpha_aijs_composition_independent
Example #17
0
def test_Rachford_Rice_solutionN():
    ns = [
        0.204322076984, 0.070970999150, 0.267194323384, 0.296291964579,
        0.067046080882, 0.062489248292, 0.031685306730
    ]
    Ks_y = [
        1.23466988745, 0.89727701141, 2.29525708098, 1.58954899888,
        0.23349348597, 0.02038108640, 1.40715641002
    ]
    Ks_z = [
        1.52713341421, 0.02456487977, 1.46348240453, 1.16090546194,
        0.24166289908, 0.14815282572, 14.3128010831
    ]
    ns2, Ks2, betas2 = np.array(ns), np.array([Ks_y, Ks_z]), np.array([.1, .6])
    betas_new, zs_new = chemicals.numba.Rachford_Rice_solutionN(
        ns2, Ks2, betas2)
    betas, zs = Rachford_Rice_solutionN(ns, [Ks_y, Ks_z], [.1, .6])
    assert_close1d(betas, betas_new, rtol=1e-14)
    assert_close2d(zs, zs_new, rtol=1e-14)
Example #18
0
def test_wedge_meter_expansibility():
    data = [[1.0000, 0.9871, 0.9741, 0.9610, 0.9478, 0.9345, 0.9007, 0.8662, 0.8308],
            [1.0000, 0.9863, 0.9726, 0.9588, 0.9449, 0.9310, 0.8957, 0.8599, 0.8234],
            [1.0000, 0.9848, 0.9696, 0.9544, 0.9393, 0.9241, 0.8860, 0.8479, 0.8094],
            [1.0000, 0.9820, 0.9643, 0.9467, 0.9292, 0.9119, 0.8692, 0.8272, 0.7857],
            [1.0000, 0.9771, 0.9547, 0.9329, 0.9117, 0.8909, 0.8408, 0.7930, 0.7472]]

    h_ds = [0.2, 0.3, 0.4, 0.5, 0.6]
    pressure_ratios = [1.0, 0.98, 0.96, 0.94, 0.92, 0.9, 0.85, 0.8, 0.75]
    calculated = []
    for i, h_d in enumerate(h_ds):
        row = []
        beta = diameter_ratio_wedge_meter(D=1, H=h_d)
        for j, p_ratio in enumerate(pressure_ratios):

            ans = nozzle_expansibility(D=1, Do=h_d, P1=1E5, P2=1E5*p_ratio, k=1.2, beta=beta)
            row.append(ans)
        calculated.append(row)

    assert_close2d(data, calculated, rtol=1e-4)
Example #19
0
def test_NRTL_gammas_binaries_jac():
    expect = [[
        1.1789916307339643, -0.26111310616433153, -3.3538119188199227,
        -6.741171640572135
    ],
              [
                  0.028442736322554146, 0.12389510256475463,
                  -0.4669633093241108, -0.24619435634886436
              ],
              [
                  0.6880803492773255, -0.1604580514041915, -1.2421051941611734,
                  -3.0460013493889275
              ],
              [
                  0.07924102114006676, 0.14519919300123013,
                  -0.9799779542489997, -0.9023280256735463
              ]]
    jac_calc = NRTL_gammas_binaries_jac([.3, .7, .4, .6], 2, 3, .2, .4)
    assert_close2d(expect, jac_calc, rtol=1e-12)

    # The optimized code was generated as follows with sympy
    '''
Example #20
0
def test_d2xs_to_d2xsn1():
    test_data = [[
        -4622.597669746761, 1098.2168754455354, -1014.6525577464832,
        210.01951660524486, 974.1035993187835
    ],
                 [
                     1098.2168754455347, -2340.178897128616,
                     -791.9786180652402, 384.16062898478407, 2502.1075882633986
                 ],
                 [
                     -1014.6525577464832, -791.9786180652403,
                     -6071.991240755533, 3359.1726518884193, 1823.0297151891225
                 ],
                 [
                     210.01951660524486, 384.16062898478407,
                     3359.1726518884193, -2723.623727200983, 319.21808436722444
                 ],
                 [
                     974.1035993187835, 2502.1075882633972, 1823.0297151891225,
                     319.21808436722444, -5223.4014263476565
                 ]]
    d2xsn1_expect = [[
        -11794.206294731985, -7601.395738484303, -9035.187298602046,
        -6306.7035934284195
    ],
                     [
                         -7601.395738484303, -12567.79550000307,
                         -10340.517347865418, -7660.566469993495
                     ],
                     [
                         -9035.187298602046, -10340.517347865418,
                         -14941.452097481435, -4006.4765740155844
                     ],
                     [
                         -6306.7035934284195, -7660.566469993495,
                         -4006.4765740155844, -8585.461322283089
                     ]]
    assert_close2d(d2xs_to_d2xsn1(test_data), d2xsn1_expect, rtol=1e-12)
def test_basic_chemsep_PR():
    kij = IPDB.get_ip_specific('ChemSep PR', ['124-38-9', '67-56-1'], 'kij')
    assert_close(kij, 0.0583)

    kij_auto = IPDB.get_ip_automatic(['124-38-9', '67-56-1'], 'PR kij', 'kij')
    assert_close(kij, kij_auto)

    kij_missing = IPDB.get_ip_specific('ChemSep PR', ['1249-38-9', '67-56-1'], 'kij')
    assert kij_missing == 0
    assert False == IPDB.has_ip_specific('ChemSep PR', ['1249-38-9', '67-56-1'], 'kij')
    assert True == IPDB.has_ip_specific('ChemSep PR', ['124-38-9', '67-56-1'], 'kij')

    assert IPDB.get_tables_with_type('PR kij') == ['ChemSep PR']

    # interaction parameter matrix
    kij_C1C4 = IPDB.get_ip_symmetric_matrix('ChemSep PR', ['74-82-8', '74-84-0', '74-98-6', '106-97-8'], 'kij')
    kij_C1C4_known = [[0.0, -0.0059, 0.0119, 0.0185],
                     [-0.0059, 0.0, 0.0011, 0.0089],
                     [0.0119, 0.0011, 0.0, 0.0033],
                     [0.0185, 0.0089, 0.0033, 0.0]]
    assert_close2d(kij_C1C4, kij_C1C4_known)
    # Test for asymetric works the same since the model is asymmetric
    kij_C1C4 = IPDB.get_ip_symmetric_matrix('ChemSep PR', ['74-82-8', '74-84-0', '74-98-6', '106-97-8'], 'kij')
    assert_close2d(kij_C1C4, kij_C1C4_known)
def test_basic_chemsep_NRTL():
    # ethanol water, converted to metric, simple T dependence
    bijs = IPDB.get_ip_asymmetric_matrix('ChemSep NRTL', ['64-17-5', '7732-18-5'], 'bij')
    assert_close2d(bijs, [[0.0, -29.166654483541816], [624.8676222389441, 0.0]], rtol=1e-7)
    alphas_known = [[0.0, 0.2937, 0.3009], [0.2937, 0.0, 0.2999], [0.3009, 0.2999, 0.0]]
    # Test is works both symmetric and asymmetric
    alphas = IPDB.get_ip_asymmetric_matrix('ChemSep NRTL', ['64-17-5', '7732-18-5', '67-56-1'], 'alphaij')
    assert_close2d(alphas, alphas_known)
    alphas = IPDB.get_ip_symmetric_matrix('ChemSep NRTL', ['64-17-5', '7732-18-5', '67-56-1'], 'alphaij')
    assert_close2d(alphas, alphas_known)
Example #23
0
def test_multicomnent_madeup():
    T = 273.15 + 70
    xs = [1 / 7.0] * 7
    #    m = Mixture(['water', 'ethanol', 'methanol', '1-pentanol', '2-pentanol', '3-pentanol',
    #                 '1-decanol'],
    #                 P=1e5, zs=xs, T=T)

    # Main coefficients with temperature inverse dependency
    lambdasB = [[0.0, -35.3, 40.0, -139.0, -129.0, -128.0, -242.0],
                [-557.0, 0.0, -200.0, 83.2, 84.6, 80.2, 140.0],
                [-280.0, 95.5, 0.0, 88.2, 85.3, 89.1, 119.0],
                [-1260.0, -128.0, -220.0, 0.0, -94.4, -85.5, 59.7],
                [-1280.0, -121.0, -236.0, 80.3, 0.0, -88.8, 61.4],
                [-1370.0, -121.0, -238.0, 75.7, 78.2, 0.0, 63.1],
                [-2670.0, -304.0, -403.0, -93.4, -91.1, -86.6, 0.0]]

    # Add in some random noise for numerical stuff
    lambdasA = [
        [0.0092, 0.00976, 0.00915, 0.00918, 0.00974, 0.00925, 0.00908],
        [0.00954, 0.00927, 0.00902, 0.00948, 0.00934, 0.009, 0.00995],
        [0.00955, 0.00921, 0.0098, 0.00926, 0.00952, 0.00912, 0.00995],
        [0.00924, 0.00938, 0.00941, 0.00992, 0.00935, 0.00996, 0.0092],
        [0.00992, 0.00946, 0.00935, 0.00917, 0.00998, 0.00903, 0.00924],
        [0.00937, 0.00973, 0.00924, 0.00991, 0.00997, 0.00968, 0.00975],
        [0.00983, 0.00934, 0.00921, 0.00977, 0.00944, 0.00902, 0.00916]
    ]

    lambdasC = [
        [0.000956, 0.000958, 0.000993, 0.000949, 0.000913, 0.000947, 0.000949],
        [0.000945, 0.000928, 0.000935, 0.000999, 0.000986, 0.000959, 0.000924],
        [0.000957, 0.000935, 0.00097, 0.000906, 0.00098, 0.000952, 0.000939],
        [0.000956, 0.000948, 0.0009, 0.000903, 0.000967, 0.000972, 0.000969],
        [0.000917, 0.000949, 0.000973, 0.000922, 0.000978, 0.000944, 0.000905],
        [0.000947, 0.000996, 0.000961, 0.00091, 0.00096, 0.000982, 0.000998],
        [0.000934, 0.000929, 0.000955, 0.000975, 0.000924, 0.000979, 0.001]
    ]

    lambdasD = [
        [3.78e-05, 3.86e-05, 3.62e-05, 3.83e-05, 3.95e-05, 3.94e-05, 3.92e-05],
        [3.88e-05, 3.88e-05, 3.75e-05, 3.82e-05, 3.8e-05, 3.76e-05, 3.71e-05],
        [3.93e-05, 3.67e-05, 4e-05, 4e-05, 3.67e-05, 3.72e-05, 3.82e-05],
        [3.95e-05, 3.67e-05, 3.64e-05, 3.62e-05, 3.62e-05, 3.63e-05, 3.97e-05],
        [3.83e-05, 3.68e-05, 3.73e-05, 3.78e-05, 3.9e-05, 3.79e-05, 3.94e-05],
        [3.67e-05, 3.82e-05, 3.76e-05, 3.61e-05, 3.67e-05, 3.88e-05, 3.64e-05],
        [3.7e-05, 3.7e-05, 3.82e-05, 3.91e-05, 3.73e-05, 3.93e-05, 3.89e-05]
    ]

    lambdasE = [[493.0, 474.0, 481.0, 468.0, 467.0, 474.0, 460.0],
                [478.0, 454.0, 460.0, 488.0, 469.0, 479.0, 483.0],
                [469.0, 493.0, 470.0, 476.0, 466.0, 451.0, 478.0],
                [481.0, 470.0, 467.0, 455.0, 473.0, 465.0, 465.0],
                [470.0, 487.0, 472.0, 460.0, 467.0, 468.0, 500.0],
                [480.0, 464.0, 475.0, 469.0, 462.0, 476.0, 469.0],
                [492.0, 460.0, 458.0, 494.0, 465.0, 461.0, 496.0]]

    lambdasF = [
        [8.25e-08, 8.27e-08, 8.78e-08, 8.41e-08, 8.4e-08, 8.93e-08, 8.98e-08],
        [8.28e-08, 8.35e-08, 8.7e-08, 8.96e-08, 8.15e-08, 8.46e-08, 8.53e-08],
        [8.51e-08, 8.65e-08, 8.24e-08, 8.89e-08, 8.86e-08, 8.71e-08, 8.21e-08],
        [8.75e-08, 8.89e-08, 8.6e-08, 8.42e-08, 8.83e-08, 8.52e-08, 8.53e-08],
        [8.24e-08, 8.27e-08, 8.43e-08, 8.19e-08, 8.74e-08, 8.3e-08, 8.35e-08],
        [8.79e-08, 8.84e-08, 8.31e-08, 8.15e-08, 8.68e-08, 8.55e-08, 8.2e-08],
        [8.63e-08, 8.76e-08, 8.52e-08, 8.46e-08, 8.67e-08, 8.9e-08, 8.38e-08]
    ]

    GE = Wilson(T=T,
                xs=xs,
                ABCDEF=(lambdasA, lambdasB, lambdasC, lambdasD, lambdasE,
                        lambdasF))

    dT = T * 4e-8
    dlambdas_dT_numerical = ((np.array(GE.to_T_xs(T=T + dT, xs=xs).lambdas()) -
                              np.array(GE.lambdas())) / dT)
    dlambdas_dT_analytical = GE.dlambdas_dT()
    dlambdas_dT_expect = [[
        7.590031904561817e-05, 0.00035248336206849656, -0.0003094799547918734,
        0.0008734144675894445, 0.0008398355729456691, 0.0008388484248830437,
        0.0011010388136697634
    ],
                          [
                              0.0009887057582300238, 7.958864330150294e-05,
                              0.0010333738209314847, -0.0008357318798642386,
                              -0.0008603154287867674, -0.000798471407040265,
                              -0.0017482651322616297
                          ],
                          [
                              0.0011329768794643754, -0.0010143841833093821,
                              7.944935494096504e-05, -0.0009028095210965592,
                              -0.0008655087807908184, -0.0009179733398931945,
                              -0.0013825890320730938
                          ],
                          [
                              0.00028607514753594, 0.0008361255545973125,
                              0.001066623152184149, 7.722830239720729e-05,
                              0.0006952398032090327, 0.0006509200113968528,
                              -0.0005326090692151773
                          ],
                          [
                              0.00027384494917044615, 0.0008057486827319439,
                              0.001089620949611226, -0.000800537779412656,
                              8.220344880607542e-05, 0.0006666235375135453,
                              -0.0005573837108687875
                          ],
                          [
                              0.00022537157737850808, 0.0008117920594539006,
                              0.0010915605157597842, -0.0007424034499493812,
                              -0.0007702925324798186, 8.014036862597122e-05,
                              -0.0005807961723267018
                          ],
                          [
                              9.912129903248681e-06, 0.001142970658771038,
                              0.00112743688620555, 0.0006908346953600135,
                              0.0006797401776554298, 0.000661487700579681,
                              7.80163901524144e-05
                          ]]
    assert_close2d(dlambdas_dT_analytical, dlambdas_dT_expect, rtol=1e-13)
    assert_close2d(GE.dlambdas_dT(), dlambdas_dT_numerical, rtol=4e-7)

    d2lambdas_dT2_expect = [[
        3.9148862097941074e-07, -1.1715575536585707e-06, 2.841056708836699e-06,
        -3.4348594220311783e-06, -3.3305413029034426e-06,
        -3.30503660953987e-06, -3.6315706582334585e-06
    ],
                            [
                                -8.471983417828227e-07, 3.7679068485264176e-07,
                                -3.7137380864269715e-06, 6.512351301871582e-06,
                                6.618281668596983e-06, 6.196655250267148e-06,
                                1.3401585927323754e-05
                            ],
                            [
                                -3.445457199491784e-06, 7.777520320436478e-06,
                                3.8176318059273734e-07, 7.003390983275993e-06,
                                6.702713977066079e-06, 7.0719354763776695e-06,
                                1.0522581265135932e-05
                            ],
                            [
                                1.4383544948858643e-06,
                                -3.3086241806901887e-06,
                                -3.7030893425862076e-06, 3.784586533299158e-07,
                                -2.794563476850137e-06, -2.623796179953121e-06,
                                4.348980938675828e-06
                            ],
                            [
                                1.4208967586173933e-06, -3.2319878656258e-06,
                                -3.6664691274893067e-06, 6.187296499210411e-06,
                                3.913709343027832e-07, -2.6938291442926234e-06,
                                4.508630881673789e-06
                            ],
                            [
                                1.341457154534956e-06, -3.2254094007592747e-06,
                                -3.6611570052888056e-06, 5.757845600966917e-06,
                                6.003019989380841e-06, 3.909269296593175e-07,
                                4.632022286150103e-06
                            ],
                            [
                                1.6807740689825766e-07, -3.272186668663736e-06,
                                -2.3655959455488764e-06,
                                -2.778154773804601e-06,
                                -2.7342339993722627e-06,
                                -2.6363752589934457e-06, 3.957722151854727e-07
                            ]]

    d2lambdas_dT2_analytical = GE.d2lambdas_dT2()
    d2lambdas_dT2_numerical = (
        (np.array(GE.to_T_xs(T=T + dT, xs=xs).dlambdas_dT()) -
         np.array(GE.dlambdas_dT())) / dT)
    assert_close2d(d2lambdas_dT2_analytical, d2lambdas_dT2_expect, rtol=1e-13)
    assert_close2d(d2lambdas_dT2_numerical,
                   d2lambdas_dT2_analytical,
                   rtol=1e-7)

    d3lambdas_dT3_expect = [[
        -2.458523153557734e-09, 1.075750588231061e-08, -2.5272393065722066e-08,
        2.4517104581274395e-08, 2.431790858429151e-08, 2.427274202635229e-08,
        1.9490204463227542e-08
    ],
                            [
                                -8.757802083086752e-09,
                                -2.2542994182376097e-09, 2.274425631180586e-08,
                                -6.272451551020672e-08, -6.401893152558163e-08,
                                -5.9505339021928453e-08, -1.3924656024261e-07
                            ],
                            [
                                1.580721350387629e-08, -7.635453830620122e-08,
                                -2.338277642369151e-09, -6.793661773544476e-08,
                                -6.473586412797368e-08, -6.868136383836727e-08,
                                -1.0662459524200453e-07
                            ],
                            [
                                -5.556692877523925e-09, 2.4287244013271755e-08,
                                2.1341219536976808e-08, -2.262235043655526e-09,
                                2.2067296896708128e-08, 2.1072746826311852e-08,
                                -4.033020732041565e-08
                            ],
                            [
                                -5.1501733514971684e-09,
                                2.3982486346807567e-08, 2.0021555356784153e-08,
                                -5.943967388445255e-08,
                                -2.3179928186947205e-09, 2.144234022725535e-08,
                                -4.2054010358506494e-08
                            ],
                            [
                                -3.323181667271306e-09, 2.404402632258137e-08,
                                1.9839516484420424e-08,
                                -5.4961048857581214e-08,
                                -5.7383559610513795e-08,
                                -2.366287903394162e-09, -4.330885396751575e-08
                            ],
                            [
                                1.7135848473209763e-09, 1.331477284270524e-08,
                                3.0148603800370024e-09, 2.1913464581918025e-08,
                                2.1725189171286703e-08, 2.120416180853968e-08,
                                -2.4707966753243437e-09
                            ]]

    d3lambdas_dT3_analytical = GE.d3lambdas_dT3()
    d3lambdas_dT3_numerical = (
        (np.array(GE.to_T_xs(T=T + dT, xs=xs).d2lambdas_dT2()) -
         np.array(GE.d2lambdas_dT2())) / dT)
    assert_close2d(d3lambdas_dT3_analytical, d3lambdas_dT3_expect, rtol=1e-13)
    assert_close2d(d3lambdas_dT3_numerical,
                   d3lambdas_dT3_analytical,
                   rtol=2e-7)
def test_ThermalConductivityGas():
    EtOH = ThermalConductivityGas(MW=46.06844,
                                  Tb=351.39,
                                  Tc=514.0,
                                  Pc=6137000.0,
                                  Vc=0.000168,
                                  Zc=0.2412,
                                  omega=0.635,
                                  dipole=1.44,
                                  Vmg=0.02357,
                                  Cpgm=56.98 + R,
                                  mug=7.903e-6,
                                  CASRN='64-17-5')
    all_methods = list(EtOH.all_methods)

    EtOH.method = EUCKEN_MOD
    assert_close(EtOH.T_dependent_property(305), 0.015427445804245578)
    EtOH.method = EUCKEN
    assert_close(EtOH.T_dependent_property(305), 0.012984130473277289)
    EtOH.method = VDI_PPDS
    assert_close(EtOH.T_dependent_property(305), 0.015661846372995)
    EtOH.method = BAHADORI_G
    assert_close(EtOH.T_dependent_property(305), 0.018297587287579457)
    EtOH.method = GHARAGHEIZI_G
    assert_close(EtOH.T_dependent_property(305), 0.016862968023145547)
    EtOH.method = DIPPR_9B
    assert_close(EtOH.T_dependent_property(305), 0.014372770946906635)
    EtOH.method = ELI_HANLEY
    assert_close(EtOH.T_dependent_property(305), 0.011684946002735508)
    EtOH.method = VDI_TABULAR
    assert_close(EtOH.T_dependent_property(305), 0.015509857659914554)
    EtOH.method = CHUNG
    assert_close(EtOH.T_dependent_property(305), 0.011710616856383785)
    EtOH.method = DIPPR_PERRY_8E
    assert_close(EtOH.T_dependent_property(305), 0.015836254853225484)

    EtOH.extrapolation = None

    for i in (DIPPR_PERRY_8E, VDI_TABULAR, GHARAGHEIZI_G, ELI_HANLEY,
              BAHADORI_G, VDI_PPDS):
        EtOH.method = i
        assert EtOH.T_dependent_property(5E20) is None

    # Test tabular limits/extrapolation
    EtOH.method = VDI_TABULAR
    EtOH.extrapolation = 'interp1d'
    assert_close(EtOH.T_dependent_property(600.), 0.05755089974293061)

    EtOH.extrapolation = None
    assert None == EtOH.T_dependent_property(600.)

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

    # Ethanol compressed

    assert [True, False] == [
        EtOH.test_method_validity_P(300, P, ELI_HANLEY_DENSE)
        for P in (1E5, -1E5)
    ]
    assert [True, False] == [
        EtOH.test_method_validity_P(300, P, CHUNG_DENSE) for P in (1E5, -1E5)
    ]
    assert [True, False] == [
        EtOH.test_method_validity_P(300, P, STIEL_THODOS_DENSE)
        for P in (1E5, -1E5)
    ]

    assert ThermalConductivityGas.from_json(EtOH.as_json()) == EtOH

    EtOH = ThermalConductivityGas(MW=46.06844,
                                  Tb=351.39,
                                  Tc=514.0,
                                  Pc=6137000.0,
                                  Vc=0.000168,
                                  Zc=0.2412,
                                  omega=0.635,
                                  dipole=1.44,
                                  Vmg=0.02357,
                                  Cpgm=56.98 + R,
                                  mug=7.903e-6,
                                  CASRN='64-17-5')
    assert_close(EtOH.calculate_P(298.15, 1E6, ELI_HANLEY_DENSE),
                 0.011210125242396791)
    assert_close(EtOH.calculate_P(298.15, 1E6, CHUNG_DENSE),
                 0.011770368783141446)
    EtOH.method = DIPPR_PERRY_8E
    assert_close(EtOH.calculate_P(298.15, 1E6, STIEL_THODOS_DENSE),
                 0.015422777479549062)

    # Ethanol data, calculated from CoolProp
    Ts = [400, 500, 600]
    Ps = [1E4, 1E5, 2E5]
    TP_data = [
        [0.025825794817543015, 0.037905383602635095, 0.05080124980338535],
        [0.02601702567554805, 0.03806794452306919, 0.050946301396380594],
        [0.026243171168075605, 0.03825284803978187, 0.05110925652065333]
    ]
    EtOH.add_tabular_data_P(Ts, Ps, TP_data, name='CPdata')
    recalc_pts = [[EtOH.TP_dependent_property(T, P) for T in Ts] for P in Ps]
    assert_close2d(TP_data, recalc_pts)

    EtOH.tabular_extrapolation_permitted = True
    assert_close(EtOH.TP_dependent_property(399, 9E3), 0.025825794817543015)
    EtOH.tabular_extrapolation_permitted = False
    assert None == EtOH.TP_dependent_property(399, 9E3)

    with pytest.raises(Exception):
        EtOH.test_method_validity_P(300, 1E5, 'BADMETHOD')

    assert ThermalConductivityGas.from_json(EtOH.as_json()) == EtOH
Example #25
0
def test_VolumeGas():
    eos = [PR(T=300, P=1E5, Tc=430.8, Pc=7884098.25, omega=0.251)]
    SO2 = VolumeGas(CASRN='7446-09-5',
                    MW=64.0638,
                    Tc=430.8,
                    Pc=7884098.25,
                    omega=0.251,
                    dipole=1.63,
                    eos=eos)

    SO2.method_P = ABBOTT
    assert_close(SO2.TP_dependent_property(305, 1E5),
                 0.024995877483424613,
                 rtol=1e-13)

    SO2.method_P = PITZER_CURL
    assert_close(SO2.TP_dependent_property(305, 1E5),
                 0.024996281566986505,
                 rtol=1e-13)

    SO2.method_P = EOS
    assert_close(SO2.TP_dependent_property(305, 1E5),
                 0.02502431104578071,
                 rtol=1e-13)

    SO2.method_P = TSONOPOULOS_EXTENDED
    assert_close(SO2.TP_dependent_property(305, 1E5),
                 0.02499979467057479,
                 rtol=1e-13)

    SO2.method_P = IDEAL
    assert_close(SO2.TP_dependent_property(305, 1E5),
                 0.02535911098536738,
                 rtol=1e-13)

    # Test that methods return None
    for i in SO2.all_methods_P:
        SO2.method_P = i
        assert SO2.TP_dependent_property(-100, 1E5) is None
        assert SO2.TP_dependent_property(100, -1E5) is None

    with pytest.raises(Exception):
        SO2.test_method_validity_P(300, 1E5, 'BADMETHOD')

    # Ethanol data, calculated from CoolProp
    EtOH = VolumeGas(MW=46.06844,
                     Tc=514.0,
                     Pc=6137000.0,
                     omega=0.635,
                     dipole=1.44,
                     CASRN='64-17-5')
    Ts = [400, 500, 600]
    Ps = [5E3, 1E4, 5E4]

    TP_data = [[0.6646136629870959, 0.8312205608372635, 0.9976236498685168],
               [0.33203434186506636, 0.4154969056659669, 0.49875532241189763],
               [0.06596765735649, 0.08291758227639608, 0.09966060658661156]]
    EtOH.add_tabular_data_P(Ts, Ps, TP_data, name='CPdata')
    recalc_pts = [[EtOH.TP_dependent_property(T, P) for T in Ts] for P in Ps]
    assert_close2d(TP_data, recalc_pts)

    assert_close(EtOH.TP_dependent_property(300, 9E4), 0.06596765735649)
    EtOH.tabular_extrapolation_permitted = False
    assert None == EtOH.TP_dependent_property(300, 9E4)

    # Test CRC Virial data
    H2 = VolumeGas(CASRN='1333-74-0')
    H2.method_P = CRC_VIRIAL
    assert_close(H2.TP_dependent_property(300, 1E5), 0.024958843346854165)
def test_basic_chemsep_UNIQUAC():
    tausB = IPDB.get_ip_asymmetric_matrix(name='ChemSep UNIQUAC', CASs=['64-17-5', '7732-18-5'], ip='bij')
    assert_close2d(tausB, [[0.0, -87.46005814161899], [-55.288075960115854, 0.0]], rtol=1e-5)
Example #27
0
def test_ViscosityLiquid():
    EtOH = ViscosityLiquid(MW=46.06844, Tm=159.05, Tc=514.0, Pc=6137000.0, Vc=0.000168, omega=0.635, Psat=7872.16, Vml=5.8676e-5, CASRN='64-17-5')

    # Test json export
    EtOH2 = ViscosityLiquid.from_JSON(EtOH.as_JSON())
    assert EtOH.__dict__ == EtOH2.__dict__

    # Test json export with interpolator objects
    EtOH.method = VDI_TABULAR
    EtOH.T_dependent_property(315)
    s = EtOH.as_JSON()
    EtOH2 = ViscosityLiquid.from_JSON(s)
    # Do hash checks before interpolation object exists
    assert hash(EtOH) == hash(EtOH2)
    assert EtOH == EtOH2
    assert id(EtOH) != id(EtOH2)

    assert EtOH.T_dependent_property(315) == EtOH2.T_dependent_property(315)

    EtOH.method = (COOLPROP)
    assert_close(EtOH.T_dependent_property(298.15), 0.0010823506202025659, rtol=1e-9)
    EtOH.method = (DIPPR_PERRY_8E)
    assert_close(EtOH.T_dependent_property(298.15), 0.0010774308462863267, rtol=1e-9)
    EtOH.method = (VDI_PPDS)
    assert_close(EtOH.T_dependent_property(298.15), 0.0010623746999654108, rtol=1e-9)
    EtOH.method = (DUTT_PRASAD)
    assert_close(EtOH.T_dependent_property(298.15), 0.0010720812586059744, rtol=1e-9)
    EtOH.method = (VISWANATH_NATARAJAN_3)
    assert_close(EtOH.T_dependent_property(298.15), 0.0031157679801337825, rtol=1e-9)
    EtOH.method = (VDI_TABULAR)
    assert_close(EtOH.T_dependent_property(310), 0.0008726933038017184, rtol=1e-9)
    EtOH.method = (LETSOU_STIEL)
    assert_close(EtOH.T_dependent_property(298.15), 0.0004191198228004421, rtol=1e-9)
    EtOH.method = (PRZEDZIECKI_SRIDHAR)
    assert EtOH.T_dependent_property(298.15) is None
    assert_close(EtOH.T_dependent_property(400.0), 0.00039598337518386806, rtol=1e-9)


    EtOH.extrapolation = None
    for i in EtOH.all_methods:
        EtOH.method = i
        assert EtOH.T_dependent_property(600) is None


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

    # Acetic acid to test Viswanath_Natarajan_2_exponential
    acetic_acid = ViscosityLiquid(CASRN='64-19-7', Tc=590.7, method=VISWANATH_NATARAJAN_2E)
    assert_close(acetic_acid.T_dependent_property(350.0), 0.000587027903931889, rtol=1e-12)

    acetic_acid.extrapolation = None
    for i in acetic_acid.all_methods:
        acetic_acid.method = i
        assert acetic_acid.T_dependent_property(650.0) is None

    # Test Viswanath_Natarajan_2 with boron trichloride
    mu = ViscosityLiquid(CASRN='10294-34-5').T_dependent_property(250)
    assert_close(mu, 0.0003389255178814321)
    assert None == ViscosityLiquid(CASRN='10294-34-5', extrapolation=None).T_dependent_property(350)


    # Ethanol compressed
    EtOH = ViscosityLiquid(MW=46.06844, Tm=159.05, Tc=514.0, Pc=6137000.0, Vc=0.000168, omega=0.635, Psat=7872.16, Vml=5.8676e-5, CASRN='64-17-5')

    assert [False, True] == [EtOH.test_method_validity_P(300, P, COOLPROP) for P in (1E3, 1E5)]
    assert [True, True] == [EtOH.test_method_validity_P(300, P, LUCAS) for P in (1E3, 1E5)]

    assert_close(EtOH.calculate_P(298.15, 1E6, LUCAS), 0.0010880229239312313)
    assert_close(EtOH.calculate_P(298.15, 1E6, COOLPROP), 0.0010885493279015608)

    EtOH = ViscosityLiquid(MW=46.06844, Tm=159.05, Tc=514.0, Pc=6137000.0, Vc=0.000168, omega=0.635, Psat=7872.16, Vml=5.8676e-5, CASRN='64-17-5')
    # Ethanol data, calculated from CoolProp
    Ts = [275, 300, 350]
    Ps = [1E5, 5E5, 1E6]
    TP_data = [[0.0017455993713216815, 0.0010445175985089377, 0.00045053170256051774], [0.0017495149679815605, 0.0010472128172002075, 0.000452108003076486], [0.0017543973013034444, 0.0010505716944451827, 0.00045406921275411145]]
    EtOH.add_tabular_data_P(Ts, Ps, TP_data, name='CPdata')
    recalc_pts = [[EtOH.TP_dependent_property(T, P) for T in Ts] for P in Ps]
    assert_close2d(TP_data, recalc_pts)

    # TEst thta we can export to json, create a new instance with P interpolation objects
    EtOH2 = ViscosityLiquid.from_JSON( EtOH.as_JSON())
    recalc_pts2 = [[EtOH2.TP_dependent_property(T, P) for T in Ts] for P in Ps]
    assert_close2d(recalc_pts, recalc_pts2, atol=0, rtol=0)


    EtOH.tabular_extrapolation_permitted = False
    EtOH.forced_P = True
    assert None == EtOH.TP_dependent_property(300, 9E4)
    EtOH.tabular_extrapolation_permitted = True
    assert_close(EtOH.TP_dependent_property(300, 9E4), 0.0010445175985089377)

    with pytest.raises(Exception):
        EtOH.test_method_validity_P(300, 1E5, 'BADMETHOD')
Example #28
0
def test_UNIQUAC_madeup_ternary():
    N = 3
    T = 331.42
    xs = [0.229, 0.175, 0.596]
    rs = [2.5735, 2.87, 1.4311]
    qs = [2.336, 2.41, 1.432]

    # madeup numbers to match Wilson example roughly
    tausA = [[0.0, -1.05e-4, -2.5e-4], [3.9e-4, 0.0, 1.6e-4],
             [-1.123e-4, 6.5e-4, 0]]
    tausB = [[0.0, 235.0, -169.0], [-160, 0.0, -715.0], [11.2, 144.0, 0.0]]
    tausC = [[0.0, -4.23e-4, 2.9e-4], [6.1e-4, 0.0, 8.2e-5],
             [-7.8e-4, 1.11e-4, 0]]
    tausD = [[0.0, -3.94e-5, 2.22e-5], [8.5e-5, 0.0, 4.4e-5],
             [-7.9e-5, 3.22e-5, 0]]
    tausE = [[0.0, -4.2e2, 8.32e2], [2.7e2, 0.0, 6.8e2], [3.7e2, 7.43e2, 0]]
    tausF = [[0.0, 9.64e-8, 8.94e-8], [1.53e-7, 0.0, 1.11e-7],
             [7.9e-8, 2.276e-8, 0]]
    ABCDEF = (tausA, tausB, tausC, tausD, tausE, tausF)
    GE = UNIQUAC(T=T, xs=xs, rs=rs, qs=qs, ABCDEF=ABCDEF)
    assert eval(str(GE)).GE() == GE.GE()

    with pytest.raises(ValueError):
        UNIQUAC(T=T, xs=xs, rs=rs, qs=qs, ABCDEF=(tausA, None, None, []))
    with pytest.raises(ValueError):
        UNIQUAC(T=T, xs=xs, rs=rs, qs=qs, ABCDEF=(tausA, None, None, [1, 1]))
    with pytest.raises(ValueError):
        UNIQUAC(T=T,
                xs=xs,
                rs=rs,
                qs=qs,
                ABCDEF=(tausA, None, None, [1, 1, 1]))
    with pytest.raises(ValueError):
        UNIQUAC(T=T,
                xs=xs,
                rs=rs,
                qs=qs,
                ABCDEF=(tausA, None, None, [[0.0, 9.64e-8],
                                            [1.53e-7, 0.0, 1.11e-7],
                                            [7.9e-8, 2.276e-8, 0]]))
    with pytest.raises(ValueError):
        UNIQUAC(T=T,
                xs=xs,
                rs=rs,
                qs=qs,
                ABCDEF=(tausA, None, None, [[0.0, 9.64e-8, 8.94e-8],
                                            [1.53e-7, 0.0, 1.11e-7]]))

    GE2 = UNIQUAC.from_json(GE.as_json())
    assert GE2.__dict__ == GE.__dict__

    # GE
    GE_expect = 415.5805110962149
    GE_analytical = GE.GE()
    assert_close(GE_expect, GE_analytical, rtol=1e-13)
    gammas = UNIQUAC_gammas(taus=GE.taus(), rs=rs, qs=qs, xs=xs)
    GE_identity = R * T * sum(xi * log(gamma) for xi, gamma in zip(xs, gammas))
    assert_close(GE_identity, GE_analytical, rtol=1e-12)

    # dGE_dT
    dGE_dT_expect = 0.9907140284750982
    dGE_dT_analytical = GE.dGE_dT()
    dGE_dT_numerical = derivative(lambda T: GE.to_T_xs(T, xs).GE(),
                                  T,
                                  order=7,
                                  dx=T * 1e-3)
    assert_close(dGE_dT_analytical, dGE_dT_numerical, rtol=1e-12)
    assert_close(dGE_dT_expect, dGE_dT_analytical, rtol=1e-13)

    # d2GE_dT2
    d2GE_dT2_expect = -0.007148011229475758
    d2GE_dT2_analytical = GE.d2GE_dT2()
    d2GE_dT2_numerical = derivative(lambda T: GE.to_T_xs(T, xs).dGE_dT(),
                                    T,
                                    order=7,
                                    dx=T * 1e-3)
    assert_close(d2GE_dT2_expect, d2GE_dT2_analytical, rtol=1e-12)
    assert_close(d2GE_dT2_analytical, d2GE_dT2_numerical, rtol=1e-12)

    # d3GE_dT3
    d3GE_dT3_expect = 2.4882477326368877e-05
    d3GE_dT3_analytical = GE.d3GE_dT3()
    assert_close(d3GE_dT3_expect, d3GE_dT3_analytical, rtol=1e-13)
    d3GE_dT3_numerical = derivative(lambda T: GE.to_T_xs(T, xs).d2GE_dT2(),
                                    T,
                                    order=11,
                                    dx=T * 1e-2)
    assert_close(d3GE_dT3_analytical, d3GE_dT3_numerical, rtol=1e-12)

    # dphis_dxs
    dphis_dxs_analytical = GE.dphis_dxs()
    dphis_dxs_expect = [
        [0.9223577846000854, -0.4473196931643269, -0.2230519905531248],
        [-0.3418381934661886, 1.094722540086528, -0.19009311780433752],
        [-0.5805195911338968, -0.6474028469222008, 0.41314510835746243]
    ]
    assert_close2d(dphis_dxs_expect, dphis_dxs_analytical, rtol=1e-12)
    dphis_dxs_numerical = jacobian(lambda xs: GE.to_T_xs(T, xs).phis(),
                                   xs,
                                   scalar=False,
                                   perturbation=2e-8)
    assert_close2d(dphis_dxs_numerical, dphis_dxs_analytical, rtol=3e-8)

    # d2phis_dxixjs - checked to the last decimal with sympy
    d2phis_dxixjs_expect = [
        [[-2.441416183656415, 0.9048216556030662, 1.536594528053349],
         [-0.7693373390462084, -0.9442924629794809, 1.7136298020256895],
         [-0.3836232285397313, 0.5031631130108988, -0.11953988447116741]],
        [[-0.7693373390462084, -0.9442924629794809, 1.7136298020256895],
         [1.3204383950972896, -3.231500191022578, 1.9110617959252876],
         [0.658424873597119, -0.5251124708645561, -0.13331240273256284]],
        [[-0.3836232285397313, 0.5031631130108987, -0.11953988447116741],
         [0.6584248735971189, -0.5251124708645561, -0.13331240273256284],
         [0.32831771310273056, 0.27980444182238084, -0.6081221549251116]]
    ]

    d2phis_dxixjs_analytical = GE.d2phis_dxixjs()
    assert_close3d(d2phis_dxixjs_analytical, d2phis_dxixjs_expect, rtol=1e-12)
    d2phis_dxixjs_numerical = hessian(lambda xs: GE.to_T_xs(T, xs).phis(),
                                      xs,
                                      scalar=False,
                                      perturbation=1e-5)
    assert_close3d(d2phis_dxixjs_numerical,
                   d2phis_dxixjs_analytical,
                   rtol=8e-5)

    d2thetas_dxixjs_expect = [
        [[-2.346422740416712, 0.7760247163009644, 1.5703980241157476],
         [-0.7026345706138027, -0.9175106511836936, 1.6201452217974965],
         [-0.4174990477672056, 0.47571378156805694, -0.05821473380085118]],
        [[-0.7026345706138027, -0.9175106511836936, 1.6201452217974965],
         [1.0476523499983839, -2.7191206652946023, 1.6714683152962189],
         [0.6225054627376287, -0.5624465978146614, -0.06005886492296719]],
        [[-0.4174990477672056, 0.47571378156805694, -0.05821473380085118],
         [0.6225054627376287, -0.5624465978146614, -0.06005886492296719],
         [0.3698870633362176, 0.2916190647283637, -0.6615061280645813]]
    ]
    d2thetas_dxixjs_analytical = GE.d2thetas_dxixjs()
    assert_close3d(d2thetas_dxixjs_analytical,
                   d2thetas_dxixjs_expect,
                   rtol=1e-12)
    d2thetas_dxixjs_numerical = hessian(lambda xs: GE.to_T_xs(T, xs).thetas(),
                                        xs,
                                        scalar=False,
                                        perturbation=2e-5)
    assert_close3d(d2thetas_dxixjs_numerical,
                   d2thetas_dxixjs_analytical,
                   rtol=1e-4)

    def to_jac(xs):
        return GE.to_T_xs(T, xs).GE()

    # Obtained 12 decimals of precision with numdifftools
    dGE_dxs_analytical = GE.dGE_dxs()
    dGE_dxs_expect = [
        -2651.3181821109024, -2085.574403592012, -2295.0860830203587
    ]
    assert_close1d(dGE_dxs_analytical, dGE_dxs_expect, rtol=1e-12)
    dGE_dxs_numerical = jacobian(to_jac, xs, perturbation=1e-8)
    assert_close1d(dGE_dxs_numerical, dGE_dxs_analytical, rtol=1e-6)

    # d2GE_dTdxs
    def to_jac(xs):
        return GE.to_T_xs(T, xs).dGE_dT()

    d2GE_dTdxs_expect = [
        -9.940433543371945, -3.545963210296949, -7.427593534302016
    ]
    d2GE_dTdxs = GE.d2GE_dTdxs()
    d2GE_dTdxs_numerical = jacobian(to_jac, xs, perturbation=1e-8)
    assert_close1d(d2GE_dTdxs_numerical, d2GE_dTdxs, rtol=1e-6)
    assert_close1d(d2GE_dTdxs, d2GE_dTdxs_expect, rtol=1e-12)

    # d2GE_dxixjs

    def to_hess(xs):
        return GE.to_T_xs(T, xs).GE()

    d2GE_dxixjs_numerical = hessian(to_hess, xs, perturbation=1e-4)
    d2GE_dxixjs_sympy = [
        [-2890.4327598108343, -6687.099054095988, -1549.3754436994557],
        [-6687.099054095988, -2811.283290487096, -1228.622385377738],
        [-1549.3754436994557, -1228.622385377738, -3667.3880987585053]
    ]
    d2GE_dxixjs_analytical = GE.d2GE_dxixjs()
    assert_close2d(d2GE_dxixjs_numerical, d2GE_dxixjs_analytical, rtol=1e-4)
    assert_close2d(d2GE_dxixjs_analytical, d2GE_dxixjs_sympy, rtol=1e-12)

    # Check json storage again, with some results
    GE2 = UNIQUAC.from_json(GE.as_json())
    assert GE2.__dict__ == GE.__dict__
Example #29
0
def test_4_components():
    #    m = Mixture(['acetone', 'chloroform', 'methanol', 'water'], zs=xs, T=300)
    xs = [.4, .3, .2, .1]
    SPs = [19570.2, 18864.7, 29261.4, 47863.5]
    Vs = [7.421e-05, 8.068e-05, 4.083e-05, 1.808e-05]
    N = 4
    T = 300.0
    # Made up asymmetric parameters
    lambda_coeffs = [[0.0, 0.01811, 0.01736, 0.02111],
                     [0.00662, 0.0, 0.00774, 0.01966],
                     [0.01601, 0.01022, 0.0, 0.00698],
                     [0.0152, 0.00544, 0.02579, 0.0]]

    GE = RegularSolution(T, xs, Vs, SPs, lambda_coeffs)
    assert eval(str(GE)).GE() == GE.GE()

    dT = 1e-7 * T
    gammas_expect = [
        1.1928784349228994, 1.3043087978251762, 3.2795596493820955,
        197.92137114651274
    ]
    assert_close1d(GE.gammas(), gammas_expect, rtol=1e-12)
    assert_close1d(GibbsExcess.gammas(GE), gammas_expect)

    # Gammas
    assert_close(GE.GE(), 2286.257263714889, rtol=1e-12)
    gammas = GE.gammas()
    GE_from_gammas = R * T * sum(xi * log(gamma)
                                 for xi, gamma in zip(xs, gammas))
    assert_close(GE_from_gammas, GE.GE(), rtol=1e-12)

    # dGE dT
    dGE_dT_numerical = (
        (np.array(GE.to_T_xs(T + dT, xs).GE()) - np.array(GE.GE())) / dT)
    dGE_dT_analytical = GE.dGE_dT()
    assert_close(dGE_dT_analytical, 0, rtol=1e-12, atol=1e-9)
    assert_close(dGE_dT_numerical, dGE_dT_analytical)

    # d2GE dT2
    d2GE_dT2_numerical = (
        (np.array(GE.to_T_xs(T + dT, xs).dGE_dT()) - np.array(GE.dGE_dT())) /
        dT)
    d2GE_dT2_analytical = GE.d2GE_dT2()
    assert_close(d2GE_dT2_analytical, 0, rtol=1e-12, atol=1e-9)
    assert_close(d2GE_dT2_analytical, d2GE_dT2_numerical, rtol=1e-8)

    # d3GE dT3
    d3GE_dT3_numerical = ((np.array(GE.to_T_xs(T + dT, xs).d2GE_dT2()) -
                           np.array(GE.d2GE_dT2())) / dT)
    d3GE_dT3_analytical = GE.d3GE_dT3()
    assert_close(d3GE_dT3_analytical, 0, rtol=1e-12, atol=1e-9)
    assert_close(d3GE_dT3_numerical, d3GE_dT3_analytical, rtol=1e-7)

    # d2GE_dTdxs
    def dGE_dT_diff(xs):
        return GE.to_T_xs(T, xs).dGE_dT()

    d2GE_dTdxs_numerical = jacobian(dGE_dT_diff, xs, perturbation=1e-7)
    d2GE_dTdxs_analytical = GE.d2GE_dTdxs()
    d2GE_dTdxs_expect = [0] * 4
    assert_close1d(d2GE_dTdxs_analytical, d2GE_dTdxs_expect, rtol=1e-12)
    assert_close1d(d2GE_dTdxs_numerical, d2GE_dTdxs_analytical, rtol=1e-7)

    # dGE_dxs
    def dGE_dx_diff(xs):
        return GE.to_T_xs(T, xs).GE()

    dGE_dxs_numerical = jacobian(dGE_dx_diff, xs, perturbation=1e-7)
    dGE_dxs_analytical = GE.dGE_dxs()
    dGE_dxs_expect = [
        439.92463410596037, 662.6790758115604, 2962.5490239819123,
        13189.738825326536
    ]
    assert_close1d(dGE_dxs_analytical, dGE_dxs_expect, rtol=1e-12)
    assert_close1d(dGE_dxs_analytical, dGE_dxs_numerical, rtol=1e-7)

    # d2GE_dxixjs
    d2GE_dxixjs_numerical = hessian(dGE_dx_diff, xs, perturbation=1e-5)
    d2GE_dxixjs_analytical = GE.d2GE_dxixjs()
    d2GE_dxixjs_expect = [[
        -1022.4173091041094, -423.20895951381453, 1638.9017092099375,
        2081.4926965380164
    ],
                          [
                              -423.20895951381453, -1674.3900233778054,
                              1920.6043029143648, 2874.797302359955
                          ],
                          [
                              1638.901709209937, 1920.6043029143648,
                              -3788.1956922483323, -4741.028361086175
                          ],
                          [
                              2081.4926965380164, 2874.797302359955,
                              -4741.028361086175, -7468.305971059591
                          ]]
    d2GE_dxixjs_sympy = [[
        -1022.4173091041112, -423.208959513817, 1638.9017092099352,
        2081.492696538016
    ],
                         [
                             -423.208959513817, -1674.3900233778083,
                             1920.6043029143652, 2874.7973023599534
                         ],
                         [
                             1638.9017092099352, 1920.6043029143652,
                             -3788.1956922483323, -4741.028361086176
                         ],
                         [
                             2081.492696538016, 2874.7973023599534,
                             -4741.028361086176, -7468.305971059591
                         ]]
    assert_close2d(d2GE_dxixjs_analytical, d2GE_dxixjs_sympy, rtol=1e-12)
    assert_close2d(d2GE_dxixjs_analytical, d2GE_dxixjs_expect, rtol=1e-12)
    assert_close2d(d2GE_dxixjs_analytical, d2GE_dxixjs_numerical, rtol=2.5e-4)

    d3GE_dxixjxks_analytical = GE.d3GE_dxixjxks()
    d3GE_dxixjxks_sympy = [[[
        3564.2598967437325, 2275.2388316927168, -3155.248707372427,
        -4548.085576267108
    ],
                            [
                                2275.2388316927168, 3015.024292098843,
                                -4031.740524903445, -5850.4575581223535
                            ],
                            [
                                -3155.248707372427, -4031.740524903445,
                                2306.3682432066844, 3714.462825687298
                            ],
                            [
                                -4548.085576267108, -5850.4575581223535,
                                3714.462825687298, 7499.862362680743
                            ]],
                           [[
                               2275.2388316927168, 3015.024292098843,
                               -4031.740524903445, -5850.4575581223535
                           ],
                            [
                                3015.024292098843, 6346.017369615182,
                                -3782.270609497761, -6789.70782446731
                            ],
                            [
                                -4031.740524903445, -3782.270609497761,
                                2329.947090204009, 3607.836718555389
                            ],
                            [
                                -5850.4575581223535, -6789.70782446731,
                                3607.836718555389, 7807.307245181044
                            ]],
                           [[
                               -3155.248707372427, -4031.740524903445,
                               2306.3682432066844, 3714.462825687298
                           ],
                            [
                                -4031.740524903445, -3782.270609497761,
                                2329.947090204009, 3607.836718555389
                            ],
                            [
                                2306.3682432066844, 2329.947090204009,
                                7265.918548487337, 7134.805582069884
                            ],
                            [
                                3714.462825687298, 3607.836718555389,
                                7134.805582069884, 7459.310988306651
                            ]],
                           [[
                               -4548.085576267108, -5850.4575581223535,
                               3714.462825687298, 7499.862362680743
                           ],
                            [
                                -5850.4575581223535, -6789.70782446731,
                                3607.836718555389, 7807.307245181044
                            ],
                            [
                                3714.462825687298, 3607.836718555389,
                                7134.805582069884, 7459.310988306651
                            ],
                            [
                                7499.862362680743, 7807.307245181044,
                                7459.310988306651, 6343.066547716518
                            ]]]
    assert_close3d(d3GE_dxixjxks_analytical, d3GE_dxixjxks_sympy, rtol=1e-12)
Example #30
0
def test_DDBST_example():
    # One good numerical example - acetone, chloroform, methanol
    T = 331.42
    N = 3
    Vs_ddbst = [74.04, 80.67, 40.73]
    as_ddbst = [[0, 375.2835, 31.1208], [-1722.58, 0, -1140.79],
                [747.217, 3596.17, 0.0]]
    bs_ddbst = [[0, -3.78434, -0.67704], [6.405502, 0, 2.59359],
                [-0.256645, -6.2234, 0]]
    cs_ddbst = [[0.0, 7.91073e-3, 8.68371e-4], [-7.47788e-3, 0.0, 3.1e-5],
                [-1.24796e-3, 3e-5, 0.0]]

    dis = eis = fis = [[0.0] * N for _ in range(N)]

    params = Wilson.from_DDBST_as_matrix(Vs=Vs_ddbst,
                                         ais=as_ddbst,
                                         bis=bs_ddbst,
                                         cis=cs_ddbst,
                                         dis=dis,
                                         eis=eis,
                                         fis=fis,
                                         unit_conversion=False)

    A_expect = [[0.0, 3.870101271243586, 0.07939943395502425],
                [-6.491263271243587, 0.0, -3.276991837288562],
                [0.8542855660449756, 6.906801837288562, 0.0]]
    B_expect = [[0.0, -375.2835, -31.1208], [1722.58, 0.0, 1140.79],
                [-747.217, -3596.17, -0.0]]
    D_expect = [[-0.0, -0.00791073, -0.000868371],
                [0.00747788, -0.0, -3.1e-05], [0.00124796, -3e-05, -0.0]]

    C_expect = E_expect = F_expect = [[0.0] * N for _ in range(N)]

    assert_close2d(params[0], A_expect, rtol=1e-12, atol=0)
    assert_close2d(params[1], B_expect, rtol=1e-12, atol=0)
    assert_close2d(params[2], C_expect, rtol=1e-12, atol=0)
    assert_close2d(params[3], D_expect, rtol=1e-12, atol=0)
    assert_close2d(params[4], E_expect, rtol=1e-12, atol=0)
    assert_close2d(params[5], F_expect, rtol=1e-12, atol=0)

    xs = [0.229, 0.175, 0.596]

    GE = Wilson(T=T, xs=xs, ABCDEF=params)

    # Test __repr__ contains the needed information
    assert eval(str(GE)).GE() == GE.GE()

    GE2 = Wilson.from_JSON(GE.as_JSON())
    assert GE2.__dict__ == GE.__dict__

    gammas_expect = [1.223393433488855, 1.1009459024701462, 1.2052899281172034]
    assert_close1d(GE.gammas(), gammas_expect, rtol=1e-12)
    assert_close1d(GibbsExcess.gammas(GE), gammas_expect)

    lambdas = GE.lambdas()
    lambdas_expect = [[1.0, 1.1229699812593041, 0.7391181616283594],
                      [3.2694762162029805, 1.0, 1.1674967844769508],
                      [0.37280197780931773, 0.019179096486191153, 1.0]]
    assert_close2d(lambdas, lambdas_expect, rtol=1e-12)

    dlambdas_dT = GE.dlambdas_dT()
    dlambdas_dT_expect = [[0.0, -0.005046703220379676, -0.0004324140595259853],
                          [-0.026825598419319092, 0.0, -0.012161812924715213],
                          [0.003001348681882189, 0.0006273541924400231, 0.0]]
    assert_close2d(dlambdas_dT, dlambdas_dT_expect)

    dT = T * 1e-8
    dlambdas_dT_numerical = (np.array(GE.to_T_xs(T + dT, xs).lambdas()) -
                             GE.to_T_xs(T, xs).lambdas()) / dT
    assert_close2d(dlambdas_dT, dlambdas_dT_numerical, rtol=1e-7)

    d2lambdas_dT2 = GE.d2lambdas_dT2()
    d2lambdas_dT2_expect = [
        [0.0, -4.73530781420922e-07, -1.0107624477842068e-06],
        [0.000529522489227112, 0.0, 0.0001998633344112975],
        [8.85872572550323e-06, 1.6731622007033546e-05, 0.0]
    ]
    assert_close2d(d2lambdas_dT2, d2lambdas_dT2_expect, rtol=1e-12)

    d2lambdas_dT2_numerical = (np.array(GE.to_T_xs(T + dT, xs).dlambdas_dT()) -
                               GE.to_T_xs(T, xs).dlambdas_dT()) / dT
    assert_close2d(d2lambdas_dT2, d2lambdas_dT2_numerical, rtol=2e-5)

    d3lambdas_dT3 = GE.d3lambdas_dT3()
    d3lambdas_dT3_expect = [
        [0.0, 4.1982403087995867e-07, 1.3509359183777608e-08],
        [-1.2223067176509094e-05, 0.0, -4.268843384910971e-06],
        [-3.6571009680721684e-08, 3.3369718709496133e-07, 0.0]
    ]
    assert_close2d(d3lambdas_dT3, d3lambdas_dT3_expect, rtol=1e-12)

    d3lambdas_dT3_numerical = (
        np.array(GE.to_T_xs(T + dT, xs).d2lambdas_dT2()) -
        GE.to_T_xs(T, xs).d2lambdas_dT2()) / dT
    assert_close2d(d3lambdas_dT3, d3lambdas_dT3_numerical, rtol=1e-7)

    # Gammas
    assert_allclose(GE.GE(), 480.2639266306882, rtol=1e-12)
    gammas = GE.gammas()
    GE_from_gammas = R * T * sum(xi * log(gamma)
                                 for xi, gamma in zip(xs, gammas))
    assert_close(GE_from_gammas, GE.GE(), rtol=1e-12)

    # dGE dT
    dGE_dT_numerical = (
        (np.array(GE.to_T_xs(T + dT, xs).GE()) - np.array(GE.GE())) / dT)
    dGE_dT_analytical = GE.dGE_dT()
    assert_close(dGE_dT_analytical, 4.355962766232997, rtol=1e-12)
    assert_close(dGE_dT_numerical, dGE_dT_analytical)

    # d2GE dT2
    d2GE_dT2_numerical = (
        (np.array(GE.to_T_xs(T + dT, xs).dGE_dT()) - np.array(GE.dGE_dT())) /
        dT)
    d2GE_dT2_analytical = GE.d2GE_dT2()
    assert_close(d2GE_dT2_analytical, -0.02913038452501723, rtol=1e-12)
    assert_close(d2GE_dT2_analytical, d2GE_dT2_numerical, rtol=1e-8)

    # d3GE dT3
    d3GE_dT3_numerical = ((np.array(GE.to_T_xs(T + dT, xs).d2GE_dT2()) -
                           np.array(GE.d2GE_dT2())) / dT)
    d3GE_dT3_analytical = GE.d3GE_dT3()
    assert_close(d3GE_dT3_analytical, -0.00019988744724590656, rtol=1e-12)
    assert_close(d3GE_dT3_numerical, d3GE_dT3_analytical, rtol=1e-7)

    # d2GE_dTdxs
    def dGE_dT_diff(xs):
        return GE.to_T_xs(T, xs).dGE_dT()

    d2GE_dTdxs_numerical = jacobian(dGE_dT_diff, xs, perturbation=1e-7)
    d2GE_dTdxs_analytical = GE.d2GE_dTdxs()
    d2GE_dTdxs_expect = [
        -10.187806161151178, 13.956324059647034, -6.825249918548414
    ]
    assert_close1d(d2GE_dTdxs_analytical, d2GE_dTdxs_expect, rtol=1e-12)
    assert_close1d(d2GE_dTdxs_numerical, d2GE_dTdxs_analytical, rtol=1e-7)

    # dGE_dxs
    def dGE_dx_diff(xs):
        return GE.to_T_xs(T, xs).GE()

    dGE_dxs_numerical = jacobian(dGE_dx_diff, xs, perturbation=1e-7)
    dGE_dxs_analytical = GE.dGE_dxs()
    dGE_dxs_expect = [
        -2199.97589893946, -2490.5759162306463, -2241.05706053718
    ]
    assert_close1d(dGE_dxs_analytical, dGE_dxs_expect, rtol=1e-12)
    assert_close1d(dGE_dxs_analytical, dGE_dxs_numerical, rtol=1e-7)

    # d2GE_dxixjs
    d2GE_dxixjs_numerical = hessian(dGE_dx_diff, xs, perturbation=1e-5)
    d2GE_dxixjs_analytical = GE.d2GE_dxixjs()
    d2GE_dxixjs_expect = [
        [-3070.205333938506, -7565.029777297412, -1222.5200812237945],
        [-7565.029777297412, -2156.7810946064815, -1083.4743126696396],
        [-1222.5200812237945, -1083.4743126696396, -3835.5941234746824]
    ]
    assert_close2d(d2GE_dxixjs_analytical, d2GE_dxixjs_expect, rtol=1e-12)
    assert_close2d(d2GE_dxixjs_analytical, d2GE_dxixjs_numerical, rtol=1e-4)

    # d3GE_dxixjxks - very limited accuracy.
    def d2GE_dxixj_diff(xs):
        return GE.to_T_xs(T, xs).dGE_dxs()

    d3GE_dxixjxks_numerical = hessian(d2GE_dxixj_diff,
                                      xs,
                                      perturbation=2e-5,
                                      scalar=False)

    d3GE_dxixjxks_analytical = GE.d3GE_dxixjxks()
    d3GE_dxixjxks_expect = [
        [[614.0681113650099, 14845.66663517824, 556.3625424156468],
         [14845.66663517824, 8308.935636377626, 4549.175136703878],
         [556.3625424156469, 4549.175136703878, 501.6902853815983]],
        [[14845.66663517824, 8308.935636377626, 4549.175136703878],
         [8308.935636377626, 173.08338078843053, 375.4114802651511],
         [4549.175136703877, 375.411480265151, -40.24127966770044]],
        [[556.3625424156469, 4549.175136703877, 501.6902853815977],
         [4549.175136703877, 375.411480265151, -40.241279667700574],
         [501.6902853815964, -40.24127966770071, 6254.612872590844]]
    ]
    assert_close3d(d3GE_dxixjxks_analytical, d3GE_dxixjxks_expect, rtol=1e-12)
    assert_close3d(d3GE_dxixjxks_numerical,
                   d3GE_dxixjxks_analytical,
                   rtol=1e-3)

    ### TEST WHICH ARE COMMON TO ALL GibbsExcess classes
    HE_expected = -963.3892533542517
    HE_analytical = GE.HE()
    assert_allclose(HE_expected, HE_analytical, rtol=1e-12)

    def diff_for_HE(T):
        return GE.to_T_xs(T, xs).GE() / T

    HE_numerical = -derivative(diff_for_HE, T, order=13) * T**2
    assert_close(HE_analytical, HE_numerical, rtol=1e-12)

    SE_expected = -4.355962766232997
    SE_analytical = GE.SE()
    assert_allclose(SE_expected, SE_analytical, rtol=1e-12)
    SE_check = (GE.HE() - GE.GE()) / T
    assert_close(SE_analytical, SE_check, rtol=1e-12)

    def diff_for_Cp(T):
        return GE.to_T_xs(T, xs).HE()

    Cp_expected = 9.65439203928121
    Cp_analytical = GE.CpE()
    assert_close(Cp_expected, Cp_analytical, rtol=1e-12)
    Cp_numerical = derivative(diff_for_Cp, T, order=13)
    assert_close(Cp_numerical, Cp_analytical, rtol=1e-12)

    def diff_for_dS_dT(T):
        return GE.to_T_xs(T, xs).SE()

    dS_dT_expected = 0.02913038452501723
    dS_dT_analytical = GE.dSE_dT()
    assert_close(dS_dT_expected, dS_dT_analytical, rtol=1e-12)
    dS_dT_numerical = derivative(diff_for_dS_dT, T, order=9)
    assert_close(dS_dT_analytical, dS_dT_numerical, rtol=1e-12)

    def diff_for_dHE_dx(xs):
        return GE.to_T_xs(T, xs).HE()

    dHE_dx_expected = [
        1176.4668189892636, -7115.980836078867, 20.96726746813556
    ]
    dHE_dx_analytical = GE.dHE_dxs()
    assert_close1d(dHE_dx_expected, dHE_dx_analytical, rtol=1e-12)
    dHE_dx_numerical = jacobian(diff_for_dHE_dx, xs, perturbation=5e-7)
    assert_close1d(dHE_dx_expected, dHE_dx_numerical, rtol=4e-6)

    def diff_for_dHE_dn(xs):
        xs = normalize(xs)
        return GE.to_T_xs(T, xs).HE()

    dHE_dn_expected = [
        2139.856072343515, -6152.591582724615, 984.3565208223869
    ]
    dHE_dn_analytical = GE.dHE_dns()
    assert_close1d(dHE_dn_expected, dHE_dn_analytical, rtol=1e-12)

    dHE_dn_numerical = jacobian(diff_for_dHE_dn, xs, perturbation=5e-7)
    assert_close1d(dHE_dn_expected, dHE_dn_numerical, rtol=1e-6)

    def diff_for_dnHE_dn(xs):
        nt = sum(xs)
        xs = normalize(xs)
        return nt * GE.to_T_xs(T, xs).HE()

    dnHE_dn_expected = [
        1176.4668189892634, -7115.980836078867, 20.967267468135258
    ]
    dnHE_dn_analytical = GE.dnHE_dns()
    assert_close1d(dnHE_dn_expected, dnHE_dn_analytical, rtol=1e-12)

    dnHE_dn_numerical = jacobian(diff_for_dnHE_dn, xs, perturbation=5e-7)
    assert_close1d(dnHE_dn_analytical, dnHE_dn_numerical, rtol=2e-6)

    def diff_for_dSE_dx(xs):
        return GE.to_T_xs(T, xs).SE()

    dSE_dx_expected = [
        10.187806161151178, -13.956324059647036, 6.825249918548415
    ]
    dSE_dx_analytical = GE.dSE_dxs()
    assert_close1d(dSE_dx_expected, dSE_dx_analytical, rtol=1e-12)
    dSE_dx_numerical = jacobian(diff_for_dSE_dx, xs, perturbation=5e-7)
    assert_close1d(dSE_dx_expected, dSE_dx_numerical, rtol=4e-6)

    def diff_for_dSE_dns(xs):
        xs = normalize(xs)
        return GE.to_T_xs(T, xs).SE()

    dSE_dns_expected = [
        6.2293063092309335, -17.91482391156728, 2.8667500666281707
    ]
    dSE_dns_analytical = GE.dSE_dns()
    assert_close1d(dSE_dns_expected, dSE_dns_analytical, rtol=1e-12)

    dSE_dns_numerical = jacobian(diff_for_dSE_dns, xs, perturbation=5e-7)
    assert_close1d(dSE_dns_expected, dSE_dns_numerical, rtol=1e-6)

    def diff_for_dnSE_dn(xs):
        nt = sum(xs)
        xs = normalize(xs)
        return nt * GE.to_T_xs(T, xs).SE()

    dnSE_dn_expected = [
        1.8733435429979384, -22.270786677800274, -1.489212699604825
    ]
    dnSE_dn_analytical = GE.dnSE_dns()
    assert_close1d(dnSE_dn_expected, dnSE_dn_analytical, rtol=1e-12)

    dnSE_dn_numerical = jacobian(diff_for_dnSE_dn, xs, perturbation=5e-7)
    assert_close1d(dnSE_dn_analytical, dnSE_dn_numerical, rtol=2e-6)

    def diff_for_dGE_dn(xs):
        xs = normalize(xs)
        return GE.to_T_xs(T, xs).GE()

    dGE_dn_expected = [75.3393753381988, -215.2606419529875, 34.25821374047882]
    dGE_dn_analytical = GE.dGE_dns()
    assert_close1d(dGE_dn_expected, dGE_dn_analytical, rtol=1e-12)

    dGE_dn_numerical = jacobian(diff_for_dGE_dn, xs, perturbation=5e-7)
    assert_close1d(dGE_dn_expected, dGE_dn_numerical, rtol=1e-5)

    def diff_for_dnGE_dn(xs):
        nt = sum(xs)
        xs = normalize(xs)
        return nt * GE.to_T_xs(T, xs).GE()

    dnGE_dn_expected = [
        555.6033019688871, 265.0032846777008, 514.5221403711671
    ]
    dnGE_dn_analytical = GE.dnGE_dns()
    assert_close1d(dnGE_dn_expected, dnGE_dn_analytical, rtol=1e-12)

    dnGE_dn_numerical = jacobian(diff_for_dnGE_dn, xs, perturbation=5e-7)
    assert_close1d(dnGE_dn_analytical, dnGE_dn_numerical, rtol=2e-6)

    lambdas = GE.lambdas()

    def gammas_to_diff(xs):
        xs = normalize(xs)
        return np.array(Wilson_gammas(xs, lambdas))

    dgammas_dns_analytical = GE.dgammas_dns()
    dgammas_dn_numerical = jacobian(gammas_to_diff, xs, scalar=False)
    dgammas_dn_expect = [
        [-0.13968444275751782, -2.135249914756224, 0.6806316652245148],
        [-1.9215360979146614, 0.23923983797040177, 0.668061736204089],
        [0.6705598284218852, 0.7313784266789759, -0.47239836472723573]
    ]

    assert_close2d(dgammas_dns_analytical, dgammas_dn_numerical, rtol=1e-5)
    assert_close2d(dgammas_dns_analytical, dgammas_dn_expect, rtol=1e-11)
    '''# Using numdifftools, the result was confirmed to the four last decimal places (rtol=12-13).
    from numdifftools import Jacobian
    (Jacobian(gammas_to_diff, step=1e-6, order=37)(xs)/dgammas_dns_analytical).tolist()
    '''

    dgammas_dT_numerical = (
        (np.array(GE.to_T_xs(T + dT, xs).gammas()) - np.array(GE.gammas())) /
        dT)
    dgammas_dT_analytical = GE.dgammas_dT()
    dgammas_dT_expect = [
        -0.001575992756074107, 0.008578456201039092, -2.7672076632932624e-05
    ]
    assert_close1d(dgammas_dT_analytical, dgammas_dT_expect, rtol=1e-12)
    assert_close1d(dgammas_dT_numerical, dgammas_dT_analytical, rtol=2e-6)

    d2GE_dTdns_expect = [
        -6.229306309230934, 17.91482391156728, -2.8667500666281702
    ]
    d2GE_dTdns_analytical = GE.d2GE_dTdns()
    d2GE_dTdns_numerical = (
        (np.array(GE.to_T_xs(T + dT, xs).dGE_dns()) - np.array(GE.dGE_dns())) /
        dT)
    assert_close1d(d2GE_dTdns_expect, d2GE_dTdns_analytical, rtol=1e-12)
    assert_close1d(d2GE_dTdns_analytical, d2GE_dTdns_numerical, rtol=1e-7)

    d2nGE_dTdns_expect = [
        -1.8733435429979375, 22.270786677800274, 1.4892126996048267
    ]
    d2nGE_dTdns_analytical = GE.d2nGE_dTdns()
    d2nGE_dTdns_numerical = ((np.array(GE.to_T_xs(T + dT, xs).dnGE_dns()) -
                              np.array(GE.dnGE_dns())) / dT)
    assert_close1d(d2nGE_dTdns_expect, d2nGE_dTdns_analytical, rtol=1e-12)
    assert_close1d(d2nGE_dTdns_analytical, d2nGE_dTdns_numerical, rtol=1e-6)

    def to_diff_dnGE2_dninj(ns):
        nt = sum(ns)
        xs = normalize(ns)
        return nt * GE.to_T_xs(T, xs).GE()

    d2nGE_dninjs_numerical = hessian(to_diff_dnGE2_dninj,
                                     xs,
                                     perturbation=4e-5)
    d2nGE_dninjs_analytical = GE.d2nGE_dninjs()
    d2nGE_dninjs_expect = [
        [-314.62613303015996, -4809.450576389065, 1533.0591196845521],
        [-4809.450576389066, 598.7981063018656, 1672.104888238707],
        [1533.0591196845517, 1672.1048882387074, -1080.0149225663358]
    ]

    assert_close2d(d2nGE_dninjs_analytical, d2nGE_dninjs_expect, rtol=1e-12)
    assert_close2d(d2nGE_dninjs_numerical, d2nGE_dninjs_analytical, rtol=1e-4)

    # Test with some results stored
    GE2 = Wilson.from_JSON(GE.as_JSON())
    assert GE2.__dict__ == GE.__dict__