From ada585552ba0c18805d85b007c0004bc4d7d4c00 Mon Sep 17 00:00:00 2001 From: Caleb Bell Date: Mon, 2 Dec 2024 18:23:17 -0700 Subject: [PATCH] Code cleanup --- ht/hx.py | 70 ++++++++++++++++++++++----------------------- ht/units.py | 6 +--- tests/test_hx.py | 10 +++---- tests/test_units.py | 7 ++++- 4 files changed, 47 insertions(+), 46 deletions(-) diff --git a/ht/hx.py b/ht/hx.py index 2aa0991..e80a69d 100644 --- a/ht/hx.py +++ b/ht/hx.py @@ -29,7 +29,7 @@ from fluids.piping import BWG_SI, BWG_integers __all__ = ['effectiveness_from_NTU', 'NTU_from_effectiveness', 'calc_Cmin', -'calc_Cmax', 'calc_Cr', 'Pp', 'Pc', +'calc_Cmax', 'calc_Cr', 'P_NTU_Pp', 'P_NTU_Pc', 'NTU_from_UA', 'UA_from_NTU', 'effectiveness_NTU_method', 'F_LMTD_Fakheri', 'temperature_effectiveness_basic', 'temperature_effectiveness_TEMA_J', 'temperature_effectiveness_TEMA_H', 'temperature_effectiveness_TEMA_G', @@ -37,7 +37,7 @@ 'temperature_effectiveness_air_cooler', 'P_NTU_method', 'NTU_from_P_basic', 'NTU_from_P_J', 'NTU_from_P_G', 'NTU_from_P_E', 'NTU_from_P_H', -'NTU_from_P_plate', 'check_tubing_TEMA', 'get_tube_TEMA', +'NTU_from_P_plate', 'DBundle_min', 'shell_clearance', 'baffle_thickness', 'D_baffle_holes', 'L_unsupported_max', 'Ntubes', 'size_bundle_from_tubecount', 'Ntubes_Perrys', 'Ntubes_VDI', 'Ntubes_Phadkeb', @@ -804,7 +804,7 @@ def UA_from_NTU(NTU, Cmin): return NTU*Cmin -def Pp(x, y): +def P_NTU_Pp(x, y): r'''Basic helper calculator which accepts a transformed R1 and NTU1 as inputs for a common term used in the calculation of the P-NTU method for plate exchangers. @@ -835,7 +835,7 @@ def Pp(x, y): Examples -------- - >>> Pp(5, .4) + >>> P_NTU_Pp(5, .4) 0.713634370024 References @@ -850,7 +850,7 @@ def Pp(x, y): return (1. - exp(-x*(1. + y)))/(1. + y) -def Pc(x, y): +def P_NTU_Pc(x, y): r'''Basic helper calculator which accepts a transformed R1 and NTU1 as inputs for a common term used in the calculation of the P-NTU method for plate exchangers. @@ -881,7 +881,7 @@ def Pc(x, y): Examples -------- - >>> Pc(5, .7) + >>> P_NTU_Pc(5, .7) 0.920670368605 References @@ -2434,45 +2434,45 @@ def temperature_effectiveness_plate(R1, NTU1, Np1, Np2, counterflow=True, 300-313. doi:10.1115/1.3250678. ''' if Np1 == 1 and Np2 == 1 and counterflow: - return Pc(NTU1, R1) + return P_NTU_Pc(NTU1, R1) elif Np1 == 1 and Np2 == 1 and not counterflow: - return Pp(NTU1, R1) + return P_NTU_Pp(NTU1, R1) elif Np1 == 1 and Np2 == 2: # There are four configurations but all have the same formula # They do behave different depending on the number of available plates # but this model assues infinity # There are four more arrangements that are equivalent as well - A = Pp(NTU1, 0.5*R1) - B = Pc(NTU1, 0.5*R1) + A = P_NTU_Pp(NTU1, 0.5*R1) + B = P_NTU_Pc(NTU1, 0.5*R1) return 0.5*(A + B - 0.5*A*B*R1) elif Np1 == 1 and Np2 == 3 and counterflow: # There are six configurations, two formulas # Each behaves differently though as a function of number of plates - A = Pp(NTU1, R1/3.) - B = Pc(NTU1, R1/3.) + A = P_NTU_Pp(NTU1, R1/3.) + B = P_NTU_Pc(NTU1, R1/3.) return 1/3.*(A + B*(1. - R1*A/3.)*(2. - R1*B/3.)) elif Np1 == 1 and Np2 == 3 and not counterflow: - A = Pp(NTU1, R1/3.) - B = Pc(NTU1, R1/3.) + A = P_NTU_Pp(NTU1, R1/3.) + B = P_NTU_Pc(NTU1, R1/3.) return 1/3.*(B + A*(1. - R1*B/3.)*(2. - R1*A/3.)) elif Np1 == 1 and Np2 == 4: # four configurations # Again a function of number of plates, but because expressions assume # infinity it gets ignored and they're the same - A = Pp(NTU1, 0.25*R1) - B = Pc(NTU1, 0.25*R1) + A = P_NTU_Pp(NTU1, 0.25*R1) + B = P_NTU_Pc(NTU1, 0.25*R1) t1 = (1. - 0.25*A*R1) t2 = (1. - 0.25*B*R1) t3 = t1*t2 # minor optimization return (1. - t3*t3)/R1 elif Np1 == 2 and Np2 == 2: if counterflow and passes_counterflow: - return Pc(NTU1, R1) + return P_NTU_Pc(NTU1, R1) elif counterflow and not passes_counterflow: - A = Pp(0.5*NTU1, R1) + A = P_NTU_Pp(0.5*NTU1, R1) return (2.*A - A*A*(1. + R1))/(1. - R1*A*A) elif not counterflow and passes_counterflow: - B = Pc(0.5*NTU1, R1) + B = P_NTU_Pc(0.5*NTU1, R1) return B*(2. - B*(1. + R1)) elif not counterflow and not passes_counterflow: return temperature_effectiveness_plate(R1, NTU1, Np1=1, Np2=1, @@ -2483,8 +2483,8 @@ def temperature_effectiveness_plate(R1, NTU1, Np1, Np2, counterflow=True, # One place says there are four configurations; no other discussion is # presented if counterflow: - H = Pp(0.5*NTU1, 2./3.*R1) - G = Pc(0.5*NTU1, 2./3.*R1) + H = P_NTU_Pp(0.5*NTU1, 2./3.*R1) + G = P_NTU_Pc(0.5*NTU1, 2./3.*R1) E = 1./(2./3.*R1*G) F = 1./(2./3.*R1*H) E2 = E*E @@ -2496,8 +2496,8 @@ def temperature_effectiveness_plate(R1, NTU1, Np1, Np2, counterflow=True, return (A + 0.5*B + 0.5*C + D)/R1 elif not counterflow: D = 2*R1/3. - A = Pp(NTU1/2, D) - B = Pc(NTU1/2, D) + A = P_NTU_Pp(NTU1/2, D) + B = P_NTU_Pc(NTU1/2, D) return (A + B - (2/9. + D/3.)*(A*A + B*B) -(5./9. + 4./3.*D)*A*B + D*(1. + D)*A*B*(A + B)/3. @@ -2505,13 +2505,13 @@ def temperature_effectiveness_plate(R1, NTU1, Np1, Np2, counterflow=True, elif Np1 == 2 and Np2 == 4: # Both cases are correct for passes_counterflow=True or False if counterflow: - A = Pp(0.5*NTU1, 0.5*R1) - B = Pc(0.5*NTU1, 0.5*R1) + A = P_NTU_Pp(0.5*NTU1, 0.5*R1) + B = P_NTU_Pc(0.5*NTU1, 0.5*R1) D = 0.5*(A + B - 0.5*A*B*R1) return (2.*D - (1. + R1)*D*D)/(1. - D*D*R1) elif not counterflow: - A = Pp(0.5*NTU1, 0.5*R1) - B = Pc(0.5*NTU1, 0.5*R1) + A = P_NTU_Pp(0.5*NTU1, 0.5*R1) + B = P_NTU_Pc(0.5*NTU1, 0.5*R1) D = 0.5*(A + B - 0.5*A*B*R1) return 2.*D - ((1. + R1)*D*D) if not reverse: @@ -3736,7 +3736,7 @@ def NTU_from_P_plate(P1, R1, Np1, Np2, counterflow=True, return log(-1./(P1*(R1 + 1.) - 1.))/(R1 + 1.) except: # raise ValueError("impossible") # numba: uncomment - raise ValueError(f'The maximum P1 obtainable at the specified R1 is {Pp(1E10, R1):f} at the limit of NTU1=inf.') # numba: delete + raise ValueError(f'The maximum P1 obtainable at the specified R1 is {P_NTU_Pp(1E10, R1):f} at the limit of NTU1=inf.') # numba: delete elif Np1 == 1 and Np2 == 2: NTU_max = 100. elif Np1 == 1 and Np2 == 3 and counterflow: @@ -4552,13 +4552,13 @@ def baffle_thickness(Dshell, L_unsupported, service='C'): -def D_baffle_holes(do, L_unsupported): +def D_baffle_holes(Do, L_unsupported): r'''Determines the diameter of holes in baffles for tubes according to TEMA [1]_. Applies for all geometries. Parameters ---------- - do : float + Do : float Tube outer diameter, [m] L_unsupported : float Distance between tube supports, [m] @@ -4573,11 +4573,11 @@ def D_baffle_holes(do, L_unsupported): Examples -------- - >>> D_baffle_holes(do=.0508, L_unsupported=0.75) + >>> D_baffle_holes(Do=.0508, L_unsupported=0.75) 0.0516 - >>> D_baffle_holes(do=0.01905, L_unsupported=0.3) + >>> D_baffle_holes(Do=0.01905, L_unsupported=0.3) 0.01985 - >>> D_baffle_holes(do=0.01905, L_unsupported=1.5) + >>> D_baffle_holes(Do=0.01905, L_unsupported=1.5) 0.019450000000000002 References @@ -4585,11 +4585,11 @@ def D_baffle_holes(do, L_unsupported): .. [1] Standards of the Tubular Exchanger Manufacturers Association, Ninth edition, 2007, TEMA, New York. ''' - if do > 0.0318 or L_unsupported <= 0.914: # 1-1/4 inches and 36 inches + if Do > 0.0318 or L_unsupported <= 0.914: # 1-1/4 inches and 36 inches extra = 0.0008 else: extra = 0.0004 - d = do + extra + d = Do + extra return d diff --git a/ht/units.py b/ht/units.py index e5765d9..8c9fc4c 100644 --- a/ht/units.py +++ b/ht/units.py @@ -36,10 +36,6 @@ 'https://github.com/hgrecco/pint') from fluids.units import wraps_numpydoc -""" -Functions which will need custom wrappers: -ht.get_tube_TEMA, ht.check_tubing_TEMA -""" __funcs = {} @@ -48,7 +44,7 @@ if name in ('__getattr__', '__test__'): continue obj = getattr(ht, name) - if isinstance(obj, types.FunctionType) and obj not in [ht.get_tube_TEMA, ht.check_tubing_TEMA]: + if isinstance(obj, types.FunctionType): obj = wraps_numpydoc(u)(obj) elif isinstance(obj, str): continue diff --git a/tests/test_hx.py b/tests/test_hx.py index a1825b4..e8e6ab6 100644 --- a/tests/test_hx.py +++ b/tests/test_hx.py @@ -963,18 +963,18 @@ def test_P_NTU_method_backwards(): def test_Pp(): - from ht.hx import Pc, Pp + from ht.hx import P_NTU_Pc, P_NTU_Pp # randomly chosen test value - ans = Pp(5, .4) + ans = P_NTU_Pp(5, .4) assert_close(ans, 0.713634370024604) # Test the limit works with a small difference - assert_close(Pp(2, -1), Pp(2, -1+1E-9)) + assert_close(P_NTU_Pp(2, -1), P_NTU_Pp(2, -1+1E-9)) # randomly chosen test value - assert_close(Pc(5, .7), 0.9206703686051108) + assert_close(P_NTU_Pc(5, .7), 0.9206703686051108) # Test the limit works with a small difference - assert_close(Pc(5, 1), Pc(5, 1-1E-8)) + assert_close(P_NTU_Pc(5, 1), P_NTU_Pc(5, 1-1E-8)) def test_temperature_effectiveness_plate(): diff --git a/tests/test_units.py b/tests/test_units.py index 66b0252..4c5ff28 100644 --- a/tests/test_units.py +++ b/tests/test_units.py @@ -27,6 +27,7 @@ import ht from ht.units import P_NTU_method, R_to_k, R_value_to_k, effectiveness_NTU_method, k_to_R_value, u +from fluids.units import check_module_docstring_parameters def assert_pint_allclose(value, magnitude, units, rtol=1e-7): @@ -104,5 +105,9 @@ def test_check_signatures(): if name in bad_names: continue obj = getattr(ht, name) - if isinstance(obj, types.FunctionType) and obj not in [ht.get_tube_TEMA, ht.check_tubing_TEMA]: + if isinstance(obj, types.FunctionType): check_args_order(obj) + +def test_parse_numpydoc_variables_units(): + import ht + check_module_docstring_parameters(ht)