Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebBell committed Dec 3, 2024
1 parent 426a360 commit 872b09f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
22 changes: 11 additions & 11 deletions fluids/fittings.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,21 +847,21 @@ def entrance_beveled(Di, l, angle, method='Rennels'):
raise ValueError(entrance_beveled_methods_unknown_msg)


def entrance_beveled_orifice(Di, do, l, angle):
def entrance_beveled_orifice(Di, Do, l, angle):
r'''Returns loss coefficient for a beveled or chamfered orifice entrance to
a pipe flush with the wall of a reservoir, as shown in [1]_.
.. math::
K = 0.0696\left(1 - C_b\frac{l}{d_o}\right)\lambda^2 + \left(\lambda
-\left(\frac{d_o}{D_i}\right)^2\right)^2
K = 0.0696\left(1 - C_b\frac{l}{D_o}\right)\lambda^2 + \left(\lambda
-\left(\frac{D_o}{D_i}\right)^2\right)^2
.. math::
\lambda = 1 + 0.622\left[1-C_b\left(\frac{l}{d_o}\right)^{\frac{1-
(l/d_o)^{0.25}}{2}}\right]
\lambda = 1 + 0.622\left[1-C_b\left(\frac{l}{D_o}\right)^{\frac{1-
(l/D_o)^{0.25}}{2}}\right]
.. math::
C_b = \left(1 - \frac{\Psi}{90}\right)\left(\frac{\Psi}{90}
\right)^{\frac{1}{1+l/d_o}}
\right)^{\frac{1}{1+l/D_o}}
.. figure:: fittings/flush_mounted_beveled_orifice_entrance.png
:scale: 30 %
Expand All @@ -871,7 +871,7 @@ def entrance_beveled_orifice(Di, do, l, angle):
----------
Di : float
Inside diameter of pipe, [m]
do : float
Do : float
Inside diameter of orifice, [m]
l : float
Length of bevel measured parallel to the pipe length, [m]
Expand All @@ -885,17 +885,17 @@ def entrance_beveled_orifice(Di, do, l, angle):
Examples
--------
>>> entrance_beveled_orifice(Di=0.1, do=.07, l=0.003, angle=45)
>>> entrance_beveled_orifice(Di=0.1, Do=.07, l=0.003, angle=45)
1.2987552913818574
References
----------
.. [1] Rennels, Donald C., and Hobart M. Hudson. Pipe Flow: A Practical
and Comprehensive Guide. 1st edition. Hoboken, N.J: Wiley, 2012.
'''
Cb = (1-angle/90.)*(angle/90.)**(1./(1 + l/do ))
lbd = 1 + 0.622*(1 - Cb*(l/do)**((1 - sqrt(sqrt(l/do)))/2.))
return 0.0696*(1 - Cb*l/do)*lbd**2 + (lbd - (do/Di)**2)**2
Cb = (1-angle/90.)*(angle/90.)**(1./(1 + l/Do ))
lbd = 1 + 0.622*(1 - Cb*(l/Do)**((1 - sqrt(sqrt(l/Do)))/2.))
return 0.0696*(1 - Cb*l/Do)*lbd**2 + (lbd - (Do/Di)**2)**2


### Exits
Expand Down
57 changes: 57 additions & 0 deletions fluids/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,63 @@ def check_args_order(func):
raise ValueError(f'Function {func.__name__} signature is not the same as the documentation'
f' signature = {argspec.args}; documentation = {parsed_parameters}')

def check_module_docstring_parameters(module, bad_names={'__getattr__', 'all_submodules'}):
"""Reads all functions in a module and compares their Parameters and Other
Parameters sections from their numpydoc docstrings with the actual function
signatures. Raises an exception if any functions have mismatched definitions.
Parameters
----------
module : module
The Python module whose functions should be checked
bad_names : set[str], optional
Set of function names to skip during checking [-]
Returns
-------
None
Raises
------
AssertionError
If any function's signature does not match its docstring parameters
Examples
--------
>>> import fluids
>>> check_module_docstring_parameters(fluids)
"""
for name in dir(module):
if name in bad_names:
continue

obj = getattr(module, name)
if not isinstance(obj, types.FunctionType):
continue

if ((hasattr(obj, 'func_name') and obj.func_name == '<lambda>') or
(hasattr(obj, '__name__') and obj.__name__ == '<lambda>')):
continue

if not obj.__doc__:
continue

try:
sig_params = list(inspect.signature(obj).parameters.keys())
except:
sig_params = inspect.getargspec(obj).args

parsed = parse_numpydoc_variables_units(obj)
doc_params = parsed['Parameters']['vars']

if 'Other Parameters' in parsed:
doc_params.extend(parsed['Other Parameters']['vars'])

if isinstance(obj, types.MethodType) and sig_params and sig_params[0] == 'self':
sig_params = sig_params[1:]

assert sig_params == doc_params, \
f"Mismatch in {obj.__name__}:\nSignature: {sig_params}\nDocstring: {doc_params}"

def match_parse_units(doc, i=-1):
if doc is None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fittings.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def log_uniform(low, high):

def test_fittings():

K = entrance_beveled_orifice(Di=0.1, do=.07, l=0.003, angle=45.0)
K = entrance_beveled_orifice(Di=0.1, Do=.07, l=0.003, angle=45.0)
assert_close(K, 1.2987552913818574)

### Exits
Expand Down
5 changes: 4 additions & 1 deletion tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import fluids
from fluids.numerics import assert_close, assert_close1d, assert_close2d
from fluids.units import check_module_docstring_parameters
from fluids.units import (
ATMOSPHERE_1976,
ATMOSPHERE_NRLMSISE00,
Expand Down Expand Up @@ -341,7 +342,9 @@ def test_check_signatures():
continue # 3
check_args_order(obj)


def test_parse_numpydoc_variables_units():
import fluids
check_module_docstring_parameters(fluids)

def test_differential_pressure_meter_solver():
m = differential_pressure_meter_solver(D=0.07366*u.m, D2=0.05*u.m, P1=200000.0*u.Pa,
Expand Down

0 comments on commit 872b09f

Please sign in to comment.