1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
'''Chemical Engineering Design Library (ChEDL). Utilities for process modeling.
Copyright (C) 2016, 2017 Caleb Bell <Caleb.Andrew.Bell@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
from math import log, pi
import pytest
from fluids.compressible import (
IGT,
Fritzsche,
Muller,
Oliphant,
P_critical_flow,
P_isothermal_critical_flow,
P_stagnation,
Panhandle_A,
Panhandle_B,
Spitzglass_high,
Spitzglass_low,
T_critical_flow,
T_stagnation,
T_stagnation_ideal,
Weymouth,
is_critical_flow,
isentropic_efficiency,
isentropic_T_rise_compression,
isentropic_work_compression,
isothermal_gas,
isothermal_work_compression,
polytropic_exponent,
stagnation_energy,
)
from fluids.constants import day, foot, inch, psi
from fluids.core import F2K
from fluids.numerics import assert_close
def test_isothermal_work_compression():
assert_close(isothermal_work_compression(1E5, 1E6, 300.0), 5743.425357533477, rtol=1e-05)
def test_isentropic_work_compression():
dH = isentropic_work_compression(P1=1E5, P2=1E6, T1=300.0, k=1.4, eta=1)
assert_close(dH, 8125.161295388634, rtol=1e-05)
dH = isentropic_work_compression(P1=1E5, P2=1E6, T1=300, k=1.4, eta=0.78)
assert_close(dH, 10416.873455626454, rtol=1e-05)
dH = isentropic_work_compression(P1=1E5, P2=1E6, T1=300, k=1.4, eta=0.78, Z=0.9)
assert_close(dH, 9375.186110063809, rtol=1e-05)
# Other solutions - P1, P2, and eta
P1 = isentropic_work_compression(W=9375.186110063809, P2=1E6, T1=300., k=1.4, eta=0.78, Z=0.9)
assert_close(P1, 1E5, rtol=1E-5)
P2 = isentropic_work_compression(W=9375.186110063809, P1=1E5, T1=300., k=1.4, eta=0.78, Z=0.9)
assert_close(P2, 1E6, rtol=1E-5)
eta = isentropic_work_compression(W=9375.186110063809, P1=1E5, P2=1E6, T1=300, k=1.4, Z=0.9, eta=None)
assert_close(eta, 0.78, rtol=1E-5)
with pytest.raises(Exception):
isentropic_work_compression(P1=1E5, P2=1E6, k=1.4, T1=None)
def test_isentropic_T_rise_compression():
T2 = isentropic_T_rise_compression(286.8, 54050.0, 432400., 1.4)
assert_close(T2, 519.5230938217768, rtol=1e-05)
T2 = isentropic_T_rise_compression(286.8, 54050, 432400, 1.4, eta=0.78)
assert_close(T2, 585.1629407971498, rtol=1e-05)
# Test against the simpler formula for eta=1:
# T2 = T2*(P2/P1)^((k-1)/k)
T2_ideal = 286.8*((432400/54050)**((1.4-1)/1.4))
assert_close(T2_ideal, 519.5230938217768, rtol=1e-05)
def test_isentropic_efficiency():
eta_s = isentropic_efficiency(1E5, 1E6, 1.4, eta_p=0.78)
assert_close(eta_s, 0.7027614191263858)
eta_p = isentropic_efficiency(1E5, 1E6, 1.4, eta_s=0.7027614191263858)
assert_close(eta_p, 0.78)
with pytest.raises(Exception):
isentropic_efficiency(1E5, 1E6, 1.4)
# Example 7.6 of the reference:
eta_s = isentropic_efficiency(1E5, 3E5, 1.4, eta_p=0.75)
assert_close(eta_s, 0.7095085923615653)
eta_p = isentropic_efficiency(1E5, 3E5, 1.4, eta_s=eta_s)
assert_close(eta_p, 0.75)
def test_polytropic_exponent():
assert_close(polytropic_exponent(1.4, eta_p=0.78), 1.5780346820809246)
assert_close(polytropic_exponent(1.4, n=1.5780346820809246), 0.78)
with pytest.raises(Exception):
polytropic_exponent(1.4)
def test_compressible():
T = T_critical_flow(473., 1.289)
assert_close(T, 413.2809086937528)
P = P_critical_flow(1400000., 1.289)
assert_close(P, 766812.9022792266)
assert not is_critical_flow(670E3, 532E3, 1.11)
assert is_critical_flow(670E3, 101E3, 1.11)
SE = stagnation_energy(125.)
assert_close(SE, 7812.5)
PST = P_stagnation(54050., 255.7, 286.8, 1.4)
assert_close(PST, 80772.80495900588)
Tst = T_stagnation(286.8, 54050., 54050*8., 1.4)
assert_close(Tst, 519.5230938217768)
Tstid = T_stagnation_ideal(255.7, 250., 1005.)
assert_close(Tstid, 286.79452736318405)
def test_Panhandle_A():
# Example 7-18 Gas of Crane TP 410M
D = 0.340
P1 = 90E5
P2 = 20E5
L = 160E3
SG=0.693
Tavg = 277.15
Q = 42.56082051195928
# Test all combinations of relevant missing inputs
assert_close(Panhandle_A(D=D, P1=P1, P2=P2, L=L, SG=SG, Tavg=Tavg), Q)
assert_close(Panhandle_A(D=D, Q=Q, P2=P2, L=L, SG=SG, Tavg=Tavg), P1)
assert_close(Panhandle_A(D=D, Q=Q, P1=P1, L=L, SG=SG, Tavg=Tavg), P2)
assert_close(Panhandle_A(D=D, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), L)
assert_close(Panhandle_A(L=L, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), D)
with pytest.raises(Exception):
Panhandle_A(D=0.340, P1=90E5, L=160E3, SG=0.693, Tavg=277.15)
# Sample problem from "Natural Gas Pipeline Flow Calculations" by "Harlan H. Bengtson"
Q_panhandle = Panhandle_A(SG=0.65, Tavg=F2K(80), Ts=F2K(60), Ps=14.7*psi, L=500*foot, D=12*inch, P1=510*psi,
P2=490*psi, Zavg=0.919, E=0.92)
mmscfd = Q_panhandle*day/foot**3/1e6
assert_close(mmscfd, 401.3019451856126, rtol=1e-12)
def test_Panhandle_B():
# Example 7-18 Gas of Crane TP 410M
D = 0.340
P1 = 90E5
P2 = 20E5
L = 160E3
SG=0.693
Tavg = 277.15
Q = 42.35366178004172
# Test all combinations of relevant missing inputs
assert_close(Panhandle_B(D=D, P1=P1, P2=P2, L=L, SG=SG, Tavg=Tavg), Q)
assert_close(Panhandle_B(D=D, Q=Q, P2=P2, L=L, SG=SG, Tavg=Tavg), P1)
assert_close(Panhandle_B(D=D, Q=Q, P1=P1, L=L, SG=SG, Tavg=Tavg), P2)
assert_close(Panhandle_B(D=D, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), L)
assert_close(Panhandle_B(L=L, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), D)
with pytest.raises(Exception):
Panhandle_B(D=0.340, P1=90E5, L=160E3, SG=0.693, Tavg=277.15)
def test_Weymouth():
D = 0.340
P1 = 90E5
P2 = 20E5
L = 160E3
SG=0.693
Tavg = 277.15
Q = 32.07729055913029
assert_close(Weymouth(D=D, P1=P1, P2=P2, L=L, SG=SG, Tavg=Tavg), Q)
assert_close(Weymouth(D=D, Q=Q, P2=P2, L=L, SG=SG, Tavg=Tavg), P1)
assert_close(Weymouth(D=D, Q=Q, P1=P1, L=L, SG=SG, Tavg=Tavg), P2)
assert_close(Weymouth(D=D, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), L)
assert_close(Weymouth(L=L, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), D)
with pytest.raises(Exception):
Weymouth(D=0.340, P1=90E5, L=160E3, SG=0.693, Tavg=277.15)
Q_Weymouth = Weymouth(SG=0.65, Tavg=F2K(80), Ts=F2K(60), Ps=14.7*psi, L=500*foot, D=12*inch, P1=510*psi,
P2=490*psi, Zavg=0.919, E=0.92)
mmscfd = Q_Weymouth*day/foot**3/1e6
assert_close(mmscfd, 272.5879686092862, rtol=1e-12)
def test_Spitzglass_high():
D = 0.340
P1 = 90E5
P2 = 20E5
L = 160E3
SG=0.693
Tavg = 277.15
Q = 29.42670246281681
assert_close(Spitzglass_high(D=D, P1=P1, P2=P2, L=L, SG=SG, Tavg=Tavg), Q)
assert_close(Spitzglass_high(D=D, Q=Q, P2=P2, L=L, SG=SG, Tavg=Tavg), P1)
assert_close(Spitzglass_high(D=D, Q=Q, P1=P1, L=L, SG=SG, Tavg=Tavg), P2)
assert_close(Spitzglass_high(D=D, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), L)
assert_close(Spitzglass_high(L=L, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), D)
with pytest.raises(Exception):
Spitzglass_high(D=0.340, P1=90E5, L=160E3, SG=0.693, Tavg=277.15)
def test_Spitzglass_low():
D = 0.154051
P1 = 6720.3199
P2 = 0.0
L = 54.864
SG = 0.6
Tavg = 288.7
Q = 0.9488775242530617
assert_close(Spitzglass_low(D=D, P1=P1, P2=P2, L=L, SG=SG, Tavg=Tavg), Q)
assert_close(Spitzglass_low(D=D, Q=Q, P2=P2, L=L, SG=SG, Tavg=Tavg), P1)
assert_close(Spitzglass_low(D=D, Q=Q, P1=P1, L=L, SG=SG, Tavg=Tavg), P2, atol=1E-10)
assert_close(Spitzglass_low(D=D, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), L)
assert_close(Spitzglass_low(L=L, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), D)
with pytest.raises(Exception):
Spitzglass_low(D=0.340, P1=90E5, L=160E3, SG=0.693, Tavg=277.15)
def test_Oliphant():
D = 0.340
P1 = 90E5
P2 = 20E5
L = 160E3
SG=0.693
Tavg = 277.15
Q = 28.851535408143057
assert_close(Oliphant(D=D, P1=P1, P2=P2, L=L, SG=SG, Tavg=Tavg), Q)
assert_close(Oliphant(D=D, Q=Q, P2=P2, L=L, SG=SG, Tavg=Tavg), P1)
assert_close(Oliphant(D=D, Q=Q, P1=P1, L=L, SG=SG, Tavg=Tavg), P2)
assert_close(Oliphant(D=D, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), L)
assert_close(Oliphant(L=L, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), D)
with pytest.raises(Exception):
Oliphant(D=0.340, P1=90E5, L=160E3, SG=0.693, Tavg=277.15)
def test_Fritzsche():
D = 0.340
P1 = 90E5
P2 = 20E5
L = 160E3
SG=0.693
Tavg = 277.15
Q = 39.421535157535565
assert_close(Fritzsche(D=D, P1=P1, P2=P2, L=L, SG=SG, Tavg=Tavg), Q)
assert_close(Fritzsche(D=D, Q=Q, P2=P2, L=L, SG=SG, Tavg=Tavg), P1)
assert_close(Fritzsche(D=D, Q=Q, P1=P1, L=L, SG=SG, Tavg=Tavg), P2)
assert_close(Fritzsche(D=D, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), L)
assert_close(Fritzsche(L=L, Q=Q, P1=P1, P2=P2, SG=SG, Tavg=Tavg), D)
with pytest.raises(Exception):
Fritzsche(D=0.340, P1=90E5, L=160E3, SG=0.693, Tavg=277.15)
def test_Muller():
D = 0.340
mu = 1E-5
P1 = 90E5
P2 = 20E5
L = 160E3
SG=0.693
Tavg = 277.15
Q = 60.45796698148663
assert_close(Muller(D=D, P1=P1, P2=P2, L=L, SG=SG, mu=mu, Tavg=Tavg), Q)
assert_close(Muller(D=D, Q=Q, P2=P2, L=L, SG=SG, mu=mu, Tavg=Tavg), P1)
assert_close(Muller(D=D, Q=Q, P1=P1, L=L, SG=SG, mu=mu, Tavg=Tavg), P2)
assert_close(Muller(D=D, Q=Q, P1=P1, P2=P2, SG=SG, mu=mu, Tavg=Tavg), L)
assert_close(Muller(L=L, Q=Q, P1=P1, P2=P2, SG=SG, mu=mu, Tavg=Tavg), D)
with pytest.raises(Exception):
Muller(D=D, P2=P2, L=L, SG=SG, mu=mu, Tavg=Tavg)
def test_IGT():
D = 0.340
mu = 1E-5
P1 = 90E5
P2 = 20E5
L = 160E3
SG=0.693
Tavg = 277.15
Q = 48.92351786788815
assert_close(IGT(D=D, P1=P1, P2=P2, L=L, SG=SG, mu=mu, Tavg=Tavg), Q)
assert_close(IGT(D=D, Q=Q, P2=P2, L=L, SG=SG, mu=mu, Tavg=Tavg), P1)
assert_close(IGT(D=D, Q=Q, P1=P1, L=L, SG=SG, mu=mu, Tavg=Tavg), P2)
assert_close(IGT(D=D, Q=Q, P1=P1, P2=P2, SG=SG, mu=mu, Tavg=Tavg), L)
assert_close(IGT(L=L, Q=Q, P1=P1, P2=P2, SG=SG, mu=mu, Tavg=Tavg), D)
with pytest.raises(Exception):
IGT(D=D, P2=P2, L=L, SG=SG, mu=mu, Tavg=Tavg)
def test_isothermal_gas():
mcalc = isothermal_gas(11.3, 0.00185, P1=1E6, P2=9E5, L=1000., D=0.5)
assert_close(mcalc, 145.484757264)
assert_close(isothermal_gas(11.3, 0.00185, P1=1E6, P2=9E5, m=145.484757264, D=0.5), 1000)
assert_close(isothermal_gas(11.3, 0.00185, P2=9E5, m=145.484757264, L=1000., D=0.5), 1E6)
assert_close(isothermal_gas(11.3, 0.00185, P1=1E6, m=145.484757264, L=1000., D=0.5), 9E5)
assert_close(isothermal_gas(11.3, 0.00185, P1=1E6, P2=9E5, m=145.484757264, L=1000.), 0.5)
with pytest.raises(Exception):
isothermal_gas(11.3, 0.00185, P1=1E6, P2=9E5, L=1000.)
with pytest.raises(Exception):
isothermal_gas(rho=11.3, fd=0.00185, P1=1E6, P2=1E5, L=1000., D=0.5)
with pytest.raises(Exception):
isothermal_gas(rho=11.3, fd=0.00185, P2=1E6, P1=9E5, L=1000., D=0.5)
# Newton can't converge, need a bounded solver
P1 = isothermal_gas(rho=11.3, fd=0.00185, m=390., P2=9E5, L=1000., D=0.5)
assert_close(P1, 2298973.786533209)
# Case where the desired flow is greater than the choked flow's rate
with pytest.raises(Exception):
isothermal_gas(rho=11.3, fd=0.00185, m=400, P2=9E5, L=1000., D=0.5)
# test the case where the ideal gas assumption is baked in:
rho = 10.75342009105268 # Chemical('nitrogen', P=(1E6+9E5)/2).rho
m1 = isothermal_gas(rho=rho, fd=0.00185, P1=1E6, P2=9E5, L=1000., D=0.5)
assert_close(m1, 141.92260633059334)
# They are fairly similar
fd = 0.00185
P1 = 1E6
P2 = 9E5
L = 1000
D = 0.5
T = 298.15
# from scipy.constants import R
# from thermo import property_molar_to_mass, Chemical, pi, log
R = 296.8029514446658 # property_molar_to_mass(R, Chemical('nitrogen').MW)
m2 = (pi**2/16*D**4/(R*T*(fd*L/D + 2*log(P1/P2)))*(P1**2-P2**2))**0.5
assert_close(m2, 145.48786057477403)
def test_P_isothermal_critical_flow():
P2_max = P_isothermal_critical_flow(P=1E6, fd=0.00185, L=1000., D=0.5)
assert_close(P2_max, 389699.7317645518)
|