def test_VolumeLiquidMixture(): from thermo.chemical import Mixture from thermo.volume import LALIBERTE, COSTALD_MIXTURE_FIT, RACKETT_PARAMETERS, COSTALD_MIXTURE, SIMPLE, RACKETT m = Mixture(['benzene', 'toluene'], zs=[.5, .5], T=298.15, P=101325.) VolumeLiquids = [i.VolumeLiquid for i in m.Chemicals] obj = VolumeLiquidMixture(MWs=m.MWs, Tcs=m.Tcs, Pcs=m.Pcs, Vcs=m.Vcs, Zcs=m.Zcs, omegas=m.omegas, CASs=m.CASs, VolumeLiquids=VolumeLiquids) Vm = obj.mixture_property(m.T, m.P, m.zs, m.ws) assert_allclose(Vm, 9.814092676573469e-05) Vms = [obj.calculate(m.T, m.P, m.zs, m.ws, method) for method in obj.all_methods] Vms_expect = [9.814092676573469e-05, 9.737758899339708e-05, 9.8109833265793461e-05, 9.8154006097783393e-05, 9.858773618507426e-05] assert_allclose(sorted(Vms), sorted(Vms_expect)) # Test Laliberte m = Mixture(['water', 'sulfuric acid'], zs=[0.01, 0.99], T=298.15) VolumeLiquids = [i.VolumeLiquid for i in m.Chemicals] obj = VolumeLiquidMixture(MWs=m.MWs, Tcs=m.Tcs, Pcs=m.Pcs, Vcs=m.Vcs, Zcs=m.Zcs, omegas=m.omegas, CASs=m.CASs, VolumeLiquids=VolumeLiquids) Vm = obj.mixture_property(m.T, m.P, m.zs, m.ws) assert_allclose(Vm, 4.824170609370422e-05) # Unhappy paths with pytest.raises(Exception): obj.calculate(m.T, m.P, m.zs, m.ws, 'BADMETHOD') with pytest.raises(Exception): obj.test_method_validity(m.T, m.P, m.zs, m.ws, 'BADMETHOD')
def test_UNIFAC_PP_fuzz(): m = Mixture(['ethanol', 'water'], zs=[0.5, 0.5], P=5000, T=298.15) vodka = UNIFAC_PP(m.UNIFAC_groups, m.VaporPressures, m.Tms, m.Tcs, m.Pcs) for i in range(500): zs = [uniform(0, 1) for i in range(2)] zs = [i / sum(zs) for i in zs] T_known = uniform(274, 513) V_over_F_known = uniform(0, 1) vodka.flash(T=T_known, VF=V_over_F_known, zs=zs) P_known = vodka.P xs_known = vodka.xs ys_known = vodka.ys phase_known = vodka.phase # test TP flash gives the same as TVF vodka.flash(T=T_known, P=P_known, zs=zs) assert_allclose(V_over_F_known, vodka.V_over_F, rtol=1E-5) assert_allclose(xs_known, vodka.xs, rtol=1E-5) assert_allclose(ys_known, vodka.ys, rtol=1E-5) assert vodka.phase == phase_known # Test PVF flash gives same as well vodka.flash(VF=V_over_F_known, P=P_known, zs=zs) assert_allclose(xs_known, vodka.xs) assert_allclose(ys_known, vodka.ys) assert_allclose(xs_known, vodka.xs) assert_allclose(T_known, vodka.T) assert vodka.phase == phase_known
def test_SurfaceTensionMixture(): from thermo.chemical import Mixture from thermo.interface import SurfaceTensionMixture, DIGUILIOTEJA, SIMPLE, WINTERFELDSCRIVENDAVIS m = Mixture(['pentane', 'dichloromethane'], zs=[.1606, .8394], T=298.15) SurfaceTensions = [i.SurfaceTension for i in m.Chemicals] VolumeLiquids = [i.VolumeLiquid for i in m.Chemicals] a = SurfaceTensionMixture(MWs=m.MWs, Tbs=m.Tbs, Tcs=m.Tcs, CASs=m.CASs, SurfaceTensions=SurfaceTensions, VolumeLiquids=VolumeLiquids) sigma = a.mixture_property(m.T, m.P, m.zs, m.ws) assert_allclose(sigma, 0.023886472131054423) sigma = a.calculate(m.T, m.P, m.zs, m.ws, SIMPLE) assert_allclose(sigma, 0.025331490604571537) sigmas = [ a.calculate(m.T, m.P, m.zs, m.ws, i) for i in [DIGUILIOTEJA, SIMPLE, WINTERFELDSCRIVENDAVIS] ] assert_allclose( sigmas, [0.025257338967448677, 0.025331490604571537, 0.023886472131054423]) with pytest.raises(Exception): a.test_method_validity(m.T, m.P, m.zs, m.ws, 'BADMETHOD') with pytest.raises(Exception): a.calculate(m.T, m.P, m.zs, m.ws, 'BADMETHOD')
def main(csvFile,inputFile): [species, ws, t0, tEnd, VClearance, VCrev, R, T0, stroke] = readInput(inputFile) mixture = Mixture(species, ws=ws) RStar = mixture.R_specific # Ideal gas constant [J/(kg K)] try: [time, position, pressure] = readCSV(csvFile, t0, tEnd, stroke) except ValueError as err: print(err.message) p0 = pressure[0] # Initial pressure pTDC = pressure[-1] # Pressure at top dead center V0 = VClearance + VCrev + stroke*math.pi*R**2 VTDC = VClearance + VCrev #V chamber without crevices H = VClearance/(math.pi*R**2) + stroke # Clearance height at BDC nPoly = np.log(pTDC/p0)/np.log((V0)/VTDC) VChamber = V0 - VCrev - position*math.pi*R**2 TChamber = T0*(VChamber[0]/VChamber)**(nPoly - 1.0) nu = np.zeros(len(TChamber)) rhoChamber = np.zeros(len(TChamber)) for Ti in range(len(TChamber)): mixture.calculate(T=TChamber[Ti],P=pressure[Ti]) rhoChamber[Ti] = pressure[Ti]/(RStar*TChamber[Ti]) nu[Ti] = mixture.mug/rhoChamber[Ti] vPiston = np.gradient(position, time) mDotStar = np.zeros(len(time)) for ti in range(2,len(time)): nut = integrate.simps(nu[0:ti], time[0:ti]) deltaOmega = A*np.sqrt(4.0*nut)*np.sqrt((H - position[ti])/H) mDotStar[ti] = rhoChamber[ti]*math.pi*(R**2 - (R - deltaOmega)**2)*vPiston[ti] mStar = integrate.simps(mDotStar, time) mChamberTDC = rhoChamber[-1]*VChamber[-1] mTot = p0*V0/(RStar*T0) mCrevTDC = mTot - mChamberTDC TCrevTDC = pTDC*VCrev/(mCrevTDC*RStar) rhoCrevTDC = pTDC/(RStar*TCrevTDC) VStar = mStar/(rhoCrevTDC - rhoChamber[0]) print("\n The ratio of the volume of the crevices to the volume needed to absorb the vortex is :") print(" %f" % (VCrev/VStar)) if (VCrev/VStar) > 1.0: print("\n Your crevices seem to be big enough.")
def test_ThermalConductivityGasMixture(): from thermo.chemical import Mixture from thermo.thermal_conductivity import ThermalConductivityGasMixture, LINDSAY_BROMLEY, SIMPLE m2 = Mixture(['nitrogen', 'argon', 'oxygen'], ws=[0.7557, 0.0127, 0.2316]) ThermalConductivityGases = [i.ThermalConductivityGas for i in m2.Chemicals] ViscosityGases = [i.ViscosityGas for i in m2.Chemicals] kg_mix = ThermalConductivityGasMixture( MWs=m2.MWs, Tbs=m2.Tbs, CASs=m2.CASs, ThermalConductivityGases=ThermalConductivityGases, ViscosityGases=ViscosityGases) k = kg_mix.mixture_property(m2.T, m2.P, m2.zs, m2.ws) assert_allclose( k, 0.025864474514829254) # test LINDSAY_BROMLEY and mixture property # Do it twice to test the stored method k = kg_mix.mixture_property(m2.T, m2.P, m2.zs, m2.ws) assert_allclose( k, 0.025864474514829254) # test LINDSAY_BROMLEY and mixture property k = kg_mix.calculate(m2.T, m2.P, m2.zs, m2.ws, SIMPLE) # Test calculate, and simple assert_allclose(k, 0.02586655464213776) dT1 = kg_mix.calculate_derivative_T(m2.T, m2.P, m2.zs, m2.ws, LINDSAY_BROMLEY) dT2 = kg_mix.property_derivative_T(m2.T, m2.P, m2.zs, m2.ws) assert_allclose([dT1, dT2], [7.3391064059347144e-05] * 2) dP1 = kg_mix.calculate_derivative_P(m2.P, m2.T, m2.zs, m2.ws, LINDSAY_BROMLEY) dP2 = kg_mix.property_derivative_P(m2.T, m2.P, m2.zs, m2.ws) assert_allclose([dP1, dP2], [3.5325319058809868e-10] * 2, rtol=1E-4) # Test other methods assert kg_mix.user_methods == [] assert kg_mix.all_methods == {LINDSAY_BROMLEY, SIMPLE} assert kg_mix.ranked_methods == [LINDSAY_BROMLEY, SIMPLE] # set a method kg_mix.set_user_method([SIMPLE]) assert None == kg_mix.method k = kg_mix.mixture_property(m2.T, m2.P, m2.zs, m2.ws) assert_allclose(k, 0.02586655464213776) # Unhappy paths with pytest.raises(Exception): kg_mix.calculate(m2.T, m2.P, m2.zs, m2.ws, 'BADMETHOD') with pytest.raises(Exception): kg_mix.test_method_validity(m2.T, m2.P, m2.zs, m2.ws, 'BADMETHOD')
def test_UNIFAC_Dortmund_PP(): m = Mixture(['ethanol', 'water'], zs=[0.5, 0.5], P=6500, T=298.15) vodka = UNIFAC_Dortmund_PP(UNIFAC_groups=m.UNIFAC_Dortmund_groups, VaporPressures=m.VaporPressures, Tms=m.Tms, Tcs=m.Tcs, Pcs=m.Pcs) # Low pressure ethanol-water ideal TP flash phase, xs, ys, V_over_F = vodka.flash_TP_zs(m.T, m.P, m.zs) V_over_F_expect = 0.721802969194136 xs_expect = [0.26331608196660095, 0.736683918033399] ys_expect = [0.5912226272910779, 0.408777372708922] assert phase == 'l/g' assert_allclose(xs, xs_expect) assert_allclose(ys, ys_expect) assert_allclose(V_over_F, V_over_F_expect) # Same flash with T-VF spec phase, xs, ys, V_over_F, P = vodka.flash_TVF_zs(m.T, V_over_F_expect, m.zs) assert phase == 'l/g' assert_allclose(xs, xs_expect, rtol=1E-5) assert_allclose(ys, ys_expect, rtol=1E-5) assert_allclose(V_over_F, V_over_F_expect, rtol=1E-5) # Same flash with P-VF spec phase, xs, ys, V_over_F, T = vodka.flash_PVF_zs(m.P, V_over_F_expect, m.zs) assert phase == 'l/g' assert_allclose(xs, xs_expect, rtol=1E-5) assert_allclose(ys, ys_expect, rtol=1E-5) assert_allclose(V_over_F, V_over_F_expect, rtol=1E-5) # Test the flash interface directly T_known = m.T V_over_F_known = V_over_F_expect zs = m.zs vodka.flash(T=T_known, VF=V_over_F_known, zs=zs) P_known = vodka.P xs_known = vodka.xs ys_known = vodka.ys phase_known = vodka.phase # test TP flash gives the same as TVF vodka.flash(T=T_known, P=P_known, zs=zs) assert_allclose(V_over_F_known, vodka.V_over_F) assert_allclose(xs_known, vodka.xs) assert_allclose(ys_known, vodka.ys) assert vodka.phase == phase_known # Test PVF flash gives same as well vodka.flash(VF=V_over_F_known, P=P_known, zs=zs) assert_allclose(xs_known, vodka.xs) assert_allclose(ys_known, vodka.ys) assert_allclose(xs_known, vodka.xs) assert_allclose(T_known, vodka.T) assert vodka.phase == phase_known
def test_Ideal_PP(): m = Mixture(['ethanol', 'water'], zs=[0.5, 0.5], P=5000, T=298.15) vodka = Ideal_PP(m.VaporPressures, m.Tms, m.Tcs, m.Pcs) # Low pressure ethanol-water ideal TP flash phase, xs, ys, V_over_F = vodka.flash_TP_zs(m.T, m.P, m.zs) V_over_F_expect = 0.49376976949268025 xs_expect = [0.38951827297213176, 0.6104817270278682] ys_expect = [0.6132697738819218, 0.3867302261180783] assert phase == 'l/g' assert_allclose(xs, xs_expect) assert_allclose(ys, ys_expect) assert_allclose(V_over_F, V_over_F_expect) # Same flash with T-VF spec phase, xs, ys, V_over_F, P = vodka.flash_TVF_zs(m.T, V_over_F_expect, m.zs) assert phase == 'l/g' assert_allclose(xs, xs_expect) assert_allclose(ys, ys_expect) assert_allclose(V_over_F, V_over_F_expect) # Same flash with P-VF spec phase, xs, ys, V_over_F, T = vodka.flash_PVF_zs(m.P, V_over_F_expect, m.zs) assert phase == 'l/g' assert_allclose(xs, xs_expect) assert_allclose(ys, ys_expect) assert_allclose(V_over_F, V_over_F_expect) # Test the flash interface directly T_known = m.T V_over_F_known = V_over_F_expect zs = m.zs vodka.flash(T=T_known, VF=V_over_F_known, zs=zs) P_known = vodka.P xs_known = vodka.xs ys_known = vodka.ys phase_known = vodka.phase # test TP flash gives the same as TVF vodka.flash(T=T_known, P=P_known, zs=zs) assert_allclose(V_over_F_known, vodka.V_over_F) assert_allclose(xs_known, vodka.xs) assert_allclose(ys_known, vodka.ys) assert vodka.phase == phase_known # Test PVF flash gives same as well vodka.flash(VF=V_over_F_known, P=P_known, zs=zs) assert_allclose(xs_known, vodka.xs) assert_allclose(ys_known, vodka.ys) assert_allclose(xs_known, vodka.xs) assert_allclose(T_known, vodka.T) assert vodka.phase == phase_known
def test_UNIFAC_PP(): m = Mixture(['ethanol', 'water'], zs=[0.5, 0.5], P=6500, T=298.15) vodka = UNIFAC_PP(m.UNIFAC_groups, m.VaporPressures, m.Tms, m.Tcs, m.Pcs) # Low pressure ethanol-water ideal TP flash phase, xs, ys, V_over_F = vodka.flash_TP_zs(m.T, m.P, m.zs) V_over_F_expect = 0.7522885045317019 xs_expect = [0.2761473052710751, 0.7238526947289249] ys_expect = [0.5737096013588943, 0.42629039864110585] assert phase == 'l/g' assert_allclose(xs, xs_expect) assert_allclose(ys, ys_expect) assert_allclose(V_over_F, V_over_F_expect) # Same flash with T-VF spec phase, xs, ys, V_over_F, P = vodka.flash_TVF_zs(m.T, V_over_F_expect, m.zs) assert phase == 'l/g' assert_allclose(xs, xs_expect, rtol=1E-5) assert_allclose(ys, ys_expect, rtol=1E-5) assert_allclose(V_over_F, V_over_F_expect, rtol=1E-5) # Same flash with P-VF spec phase, xs, ys, V_over_F, T = vodka.flash_PVF_zs(m.P, V_over_F_expect, m.zs) assert phase == 'l/g' assert_allclose(xs, xs_expect, rtol=1E-5) assert_allclose(ys, ys_expect, rtol=1E-5) assert_allclose(V_over_F, V_over_F_expect, rtol=1E-5) # Test the flash interface directly T_known = m.T V_over_F_known = V_over_F_expect zs = m.zs vodka.flash(T=T_known, VF=V_over_F_known, zs=zs) P_known = vodka.P xs_known = vodka.xs ys_known = vodka.ys phase_known = vodka.phase # test TP flash gives the same as TVF vodka.flash(T=T_known, P=P_known, zs=zs) assert_allclose(V_over_F_known, vodka.V_over_F) assert_allclose(xs_known, vodka.xs) assert_allclose(ys_known, vodka.ys) assert vodka.phase == phase_known # Test PVF flash gives same as well vodka.flash(VF=V_over_F_known, P=P_known, zs=zs) assert_allclose(xs_known, vodka.xs) assert_allclose(ys_known, vodka.ys) assert_allclose(xs_known, vodka.xs) assert_allclose(T_known, vodka.T) assert vodka.phase == phase_known
def test_ThermalConductivityLiquidMixture(): from thermo.thermal_conductivity import MAGOMEDOV, DIPPR_9H, FILIPPOV, SIMPLE, ThermalConductivityLiquidMixture m = Mixture(['ethanol', 'pentanol'], ws=[0.258, 0.742], T=298.15) ThermalConductivityLiquids = [ i.ThermalConductivityLiquid for i in m.Chemicals ] kl_mix = ThermalConductivityLiquidMixture( CASs=m.CASs, ThermalConductivityLiquids=ThermalConductivityLiquids) k = kl_mix.mixture_property(m.T, m.P, m.zs, m.ws) assert_allclose(k, 0.15300152782218343) k = kl_mix.calculate(m.T, m.P, m.zs, m.ws, FILIPPOV) assert_allclose(k, 0.15522139770330717) k = kl_mix.calculate(m.T, m.P, m.zs, m.ws, SIMPLE) assert_allclose(k, 0.1552717795028546) # Test electrolytes m = Mixture(['water', 'sulfuric acid'], ws=[.5, .5], T=298.15) ThermalConductivityLiquids = [ i.ThermalConductivityLiquid for i in m.Chemicals ] kl_mix = ThermalConductivityLiquidMixture( CASs=m.CASs, ThermalConductivityLiquids=ThermalConductivityLiquids) k = kl_mix.mixture_property(m.T, m.P, m.zs, m.ws) assert_allclose(k, 0.4677453168207703) assert kl_mix.sorted_valid_methods == [MAGOMEDOV] # Unhappy paths with pytest.raises(Exception): kl_mix.calculate(m.T, m.P, m.zs, m.ws, 'BADMETHOD') with pytest.raises(Exception): kl_mix.test_method_validity(m.T, m.P, m.zs, m.ws, 'BADMETHOD')
def test_bubble_at_P_with_ideal_mixing(): '''Check to see if the bubble pressure calculated from the temperature matches the temperature calculated by the test function''' test_mix = Mixture(['ethylene oxide', 'tetrahydrofuran', 'beta-propiolactone'], ws=[6021, 111569.76, 30711.21, ], T=273.15 + 80, P=101325 + 1.5e5) bubble_temp = bubble_at_P(test_mix.Pbubble, test_mix.zs, test_mix.VaporPressures) assert_allclose(test_mix.T, bubble_temp)
def test_VolumeGasMixture(): from thermo.chemical import Mixture from thermo.volume import VolumeGasMixture, EOS, SIMPLE, IDEAL m = Mixture(['oxygen', 'nitrogen'], zs=[.5, .5], T=298.15, P=1E6) obj = VolumeGasMixture(CASs=m.CASs, VolumeGases=m.VolumeGases, eos=m.eos_in_a_box) assert_allclose(obj.mixture_property(m.T, m.P, m.zs, m.ws), 0.0024628053244477232) assert_allclose(obj.calculate(m.T, m.P, m.zs, m.ws, SIMPLE), 0.002468989614515616) assert_allclose(obj.calculate(m.T, m.P, m.zs, m.ws, IDEAL), 0.0024789561893699998) # Unhappy paths with pytest.raises(Exception): obj.calculate(m.T, m.P, m.zs, m.ws, 'BADMETHOD') with pytest.raises(Exception): obj.test_method_validity(m.T, m.P, m.zs, m.ws, 'BADMETHOD')
def test_IdealPP_fuzz_TP_VF(): m = Mixture(['ethanol', 'water'], zs=[0.5, 0.5], P=5000, T=298.15) vodka = Ideal_PP(m.VaporPressures, m.Tms, m.Tcs, m.Pcs) for i in range(500): # May fail right now on the transition between vapor pressure # function boundaries; there are multiple solutions for that case # Especially near T = 513.9263246740085 or T = 273.15728497179936 # Failure is only for PVF flashes # There may also be failures for extrapolated vapor pressures, but # those are not tested for here. zs = [uniform(0, 1) for i in range(2)] zs = [i / sum(zs) for i in zs] T_known = uniform(200, 513) V_over_F_known = uniform(0, 1) if 273.14 < T_known < 274.15 or 513.85 < T_known < 514.: continue vodka.flash(T=T_known, VF=V_over_F_known, zs=zs) P_known = vodka.P xs_known = vodka.xs ys_known = vodka.ys phase_known = vodka.phase # test TP flash gives the same as TVF vodka.flash(T=T_known, P=P_known, zs=zs) assert_allclose(V_over_F_known, vodka.V_over_F) assert_allclose(xs_known, vodka.xs) assert_allclose(ys_known, vodka.ys) assert vodka.phase == phase_known # Test PVF flash gives same as well vodka.flash(VF=V_over_F_known, P=P_known, zs=zs) assert_allclose(xs_known, vodka.xs) assert_allclose(ys_known, vodka.ys) assert_allclose(xs_known, vodka.xs) assert_allclose(T_known, vodka.T) assert vodka.phase == phase_known names = [ 'hexane', '2-methylpentane', '3-methylpentane', '2,3-dimethylbutane', '2,2-dimethylbutane' ] m = Mixture(names, zs=[.2, .2, .2, .2, .2], P=1E5, T=300) test_pkg = IdealPP(m.VaporPressures, m.Tms, m.Tcs, m.Pcs) for i in range(500): zs = [uniform(0, 1) for i in range(5)] zs = [i / sum(zs) for i in zs] T_known = uniform(200, 488.0) V_over_F_known = uniform(0, 1) test_pkg.flash(T=T_known, VF=V_over_F_known, zs=zs) P_known = test_pkg.P xs_known = test_pkg.xs ys_known = test_pkg.ys phase_known = test_pkg.phase # test TP flash gives the same as TVF test_pkg.flash(T=T_known, P=P_known, zs=zs) assert_allclose(V_over_F_known, test_pkg.V_over_F) assert_allclose(xs_known, test_pkg.xs) assert_allclose(ys_known, test_pkg.ys) assert test_pkg.phase == phase_known # Test PVF flash gives same as well test_pkg.flash(VF=V_over_F_known, P=P_known, zs=zs) assert_allclose(xs_known, test_pkg.xs) assert_allclose(ys_known, test_pkg.ys) assert_allclose(xs_known, test_pkg.xs) assert_allclose(T_known, test_pkg.T) assert test_pkg.phase == phase_known