Skip to content

Commit

Permalink
Add the remaining unit testing of function signatures in fluids
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebBell committed Jul 17, 2017
1 parent a10a695 commit 6e0533f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 78 deletions.
21 changes: 0 additions & 21 deletions docs/fluids.units.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,3 @@ exception will be raised.

>>> K_separator_Watkins(985.4*u.kg/u.m**3, 1.3*u.kg/u.m**3, 0.88*u.dimensionless, horizontal=True)
Exception: Converting 0.88 dimensionless to units of kg/m^3 raised DimensionalityError: Cannot convert from 'dimensionless' (dimensionless) to 'kilogram / meter ** 3' ([mass] / [length] ** 3)

Unsupported functions
---------------------
Some functions are too "clever" to be wrapped and are not surrently supported
in fluids.units:

* SA_tank
* isentropic_work_compression
* polytropic_exponent
* isothermal_gas
* Panhandle_A
* Panhandle_B
* Weymouth
* Spitzglass_high
* Spitzglass_low
* Oliphant
* Fritzsche
* Muller
* IGT
* roughness_Farshad
* nu_mu_converter
2 changes: 1 addition & 1 deletion fluids/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ def nu_mu_converter(rho, mu=None, nu=None):
Returns
-------
mu or nu : float
Dynamic viscosity, [Pa*s] or Kinematic viscosity, [m^2/s]
Dynamic viscosity, Pa*s or Kinematic viscosity, m^2/s
Examples
--------
Expand Down
76 changes: 20 additions & 56 deletions fluids/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,6 @@ def wrapper(*values, **kw):
globals().update(__funcs)


'''
Known unsupported functions:
* A_multiple_hole_cylinder
* V_multiple_hole_cylinder
* SA_tank
isentropic_work_compression, polytropic_exponent, isothermal_gas, Panhandle_A, Panhandle_B, Weymouth, Spitzglass_high, Spitzglass_low, Oliphant, Fritzsche, Muller, IGT
roughness_Farshad
nu_mu_converter
All the classes
'''

def A_multiple_hole_cylinder(Do, L, holes):
Do = Do.to(u.m).magnitude
L = L.to(u.m).magnitude
Expand All @@ -278,21 +265,29 @@ def V_multiple_hole_cylinder(Do, L, holes):
wrapped_Muller = Muller
wrapped_IGT = IGT
wrapped_nu_mu_converter = nu_mu_converter
wrapped_SA_tank= SA_tank


def nu_mu_converter(rho, mu=None, nu=None):
ans = wrapped_nu_mu_converter(rho, mu, nu)
if mu is None:
return ans*u.Pa*u.s
return ans*u.m**2/u.s


def SA_tank(D, L, sideA=None, sideB=None, sideA_a=0*u.m,
sideB_a=0*u.m, sideA_f=None, sideA_k=None, sideB_f=None, sideB_k=None,
full_output=False):

ans = wrapped_SA_tank(D, L, sideA, sideB, sideA_a, sideB_a, sideA_f,
sideA_k, sideB_f, sideB_k, full_output)
if full_output:
return SA*u.m**2, (sideA_SA*u.m**2, sideB_SA*u.m**2, lateral_SA*u.m**2)
SA, (sideA_SA, sideB_SA, lateral_SA) = ans
else:
return SA*u.m**2
SA = ans
if full_output:
return SA, (sideA_SA*u.m**2, sideB_SA*u.m**2, lateral_SA*u.m**2)
else:
return SA


def isothermal_gas(rho, fd, P1=None, P2=None, L=None, D=None, m=None): # pragma: no cover
Expand All @@ -312,8 +307,9 @@ def isothermal_gas(rho, fd, P1=None, P2=None, L=None, D=None, m=None): # pragma:
elif D is None and (None not in [P2, P1, L, m]):
return ans*u.m

def Muller(SG, Tavg, mu, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
Ps=101325., Zavg=1, E=1):

def Muller(SG, Tavg, mu, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7*u.K,
Ps=101325.*u.Pa, Zavg=1, E=1): # pragma: no cover
ans = wrapped_Muller(SG, Tavg, mu, L, D, P1, P2, Q, Ts, Ps, Zavg, E)
if Q is None and (None not in [L, D, P1, P2]):
return ans*u.m**3/u.s
Expand All @@ -326,8 +322,9 @@ def Muller(SG, Tavg, mu, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
elif L is None and (None not in [P2, Q, D, P1]):
return ans*u.m

def IGT(SG, Tavg, mu, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
Ps=101325., Zavg=1, E=1):

def IGT(SG, Tavg, mu, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7*u.K,
Ps=101325.*u.Pa, Zavg=1, E=1): # pragma: no cover
ans = wrapped_IGT(SG, Tavg, mu, L, D, P1, P2, Q, Ts, Ps, Zavg, E)
if Q is None and (None not in [L, D, P1, P2]):
return ans*u.m**3/u.s
Expand All @@ -340,47 +337,17 @@ def IGT(SG, Tavg, mu, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
elif L is None and (None not in [P2, Q, D, P1]):
return ans*u.m

#def Panhandle_B(SG, Tavg, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
# Ps=101325., Zavg=1, E=0.92):
#def Weymouth(SG, Tavg, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
# Ps=101325., Zavg=1, E=0.92):
#def Spitzglass_high(SG, Tavg, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
# Ps=101325., Zavg=1, E=1.):
#def Spitzglass_low(SG, Tavg, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
# Ps=101325., Zavg=1, E=1.):
#def Oliphant(SG, Tavg, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
# Ps=101325., Zavg=1, E=0.92):
#def Fritzsche(SG, Tavg, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7,
# Ps=101325., Zavg=1, E=1):


def compressible_flow_wrapper(wrapper, SG, Tavg, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7*u.K,
Ps=101325.*u.Pa, Zavg=1, E=0.92):
'''
>>> Panhandle_A(SG=0.693, D=0.340*u.m, P1=90E5*u.Pa, P2=20E5*u.Pa, L=160E3*u.m, Tavg=277.15*u.K)
<Quantity(42.560820512, 'meter ** 3 / second')>
'''
ans = wrapper(SG, Tavg, L, D, P1, P2, Q, Ts, Ps, Zavg, E)
if Q is None and (None not in [L, D, P1, P2]):
return ans*u.m**3/u.s
elif D is None and (None not in [L, Q, P1, P2]):
return ans*u.m
elif P1 is None and (None not in [L, Q, D, P2]):
return ans*u.Pa
elif P2 is None and (None not in [L, Q, D, P1]):
return ans*u.Pa
elif L is None and (None not in [P2, Q, D, P1]):
return ans*u.m


funcs = ['Panhandle_A', 'Panhandle_B', 'Weymouth', 'Spitzglass_high', 'Spitzglass_low', 'Oliphant', 'Fritzsche']
Es = [.92, .92, .92, 1, 1, .92, 1]

for wrapper, E in zip(funcs, Es):
wrapper_name = wrapper + '_wrapper'
globals()[wrapper_name] = globals()[wrapper]

def compressible_flow_wrapper(SG, Tavg, L=None, D=None, P1=None, P2=None, Q=None, Ts=288.7*u.K,
Ps=101325.*u.Pa, Zavg=1, E=0.92, _=wrapper_name):
Ps=101325.*u.Pa, Zavg=1, E=E, _=wrapper_name): # pragma: no cover
'''
>>> Panhandle_A(SG=0.693, D=0.340*u.m, P1=90E5*u.Pa, P2=20E5*u.Pa, L=160E3*u.m, Tavg=277.15*u.K)
<Quantity(42.560820512, 'meter ** 3 / second')>
Expand All @@ -396,9 +363,6 @@ def compressible_flow_wrapper(SG, Tavg, L=None, D=None, P1=None, P2=None, Q=None
return ans*u.Pa
elif L is None and (None not in [P2, Q, D, P1]):
return ans*u.m



globals()[wrapper] = compressible_flow_wrapper


Expand All @@ -418,7 +382,7 @@ def check_args_order(func):
parsed_parameters += parsed_data['Other Parameters']['vars']
parsed_units += parsed_data['Other Parameters']['units']

if argspec.args != parsed_parameters:
if argspec.args != parsed_parameters: # pragma: no cover
raise Exception('Function signature is not the same as the documentation'
'signature = %s; documentation = %s' %(argspec.args, parsed_parameters))

Expand Down
38 changes: 38 additions & 0 deletions tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
from fluids.units import *


def assert_pint_allclose(value, magnitude, units):
assert_allclose(value.to_base_units().magnitude, magnitude)
assert dict(value.dimensionality) == units


def test_convert_input():
from fluids.units import convert_input

Expand Down Expand Up @@ -155,6 +160,9 @@ def test_sample_cases():
assert_allclose(roughness.to_base_units().magnitude, 5.3141677781137006e-05)
assert dict(roughness.dimensionality) == {u'[length]': 1.0}



def test_custom_wraps():
A = A_multiple_hole_cylinder(0.01*u.m, 0.1*u.m, [(0.005*u.m, 1)])
assert_allclose(A.to_base_units().magnitude, 0.004830198704894308)
assert dict(A.dimensionality) == {u'[length]': 2.0}
Expand All @@ -163,10 +171,40 @@ def test_sample_cases():
assert_allclose(V.to_base_units().magnitude, 5.890486225480862e-06)
assert dict(V.dimensionality) == {u'[length]': 3.0}

# custom compressible flow model wrappers
functions = [Panhandle_A, Panhandle_B, Weymouth, Spitzglass_high, Oliphant, Fritzsche]
values = [42.56082051195928, 42.35366178004172, 32.07729055913029, 29.42670246281681, 28.851535408143057, 39.421535157535565]
for f, v in zip(functions, values):
ans = f(D=0.340*u.m, P1=90E5*u.Pa, P2=20E5*u.Pa, L=160E3*u.m, SG=0.693, Tavg=277.15*u.K)
assert_pint_allclose(ans, v, {u'[length]': 3.0, u'[time]': -1.0})

ans = IGT(D=0.340*u.m, P1=90E5*u.Pa, P2=20E5*u.Pa, L=160E3*u.m, SG=0.693, mu=1E-5*u.Pa*u.s, Tavg=277.15*u.K)
assert_pint_allclose(ans, 48.92351786788815, {u'[length]': 3.0, u'[time]': -1.0})

ans = Muller(D=0.340*u.m, P1=90E5*u.Pa, P2=20E5*u.Pa, L=160E3*u.m, SG=0.693, mu=1E-5*u.Pa*u.s, Tavg=277.15*u.K)
assert_pint_allclose(ans, 60.45796698148659, {u'[length]': 3.0, u'[time]': -1.0})


nu = nu_mu_converter(rho=1000*u.kg/u.m**3, mu=1E-4*u.Pa*u.s)
assert_pint_allclose(nu, 1E-7, {u'[length]': 2.0, u'[time]': -1.0})

mu = nu_mu_converter(rho=1000*u.kg/u.m**3, nu=1E-7*u.m**2/u.s)
assert_pint_allclose(mu, 1E-4, {u'[time]': -1.0, u'[length]': -1.0, u'[mass]': 1.0})

SA = SA_tank(D=1.*u.m, L=0*u.m, sideA='ellipsoidal', sideA_a=2*u.m, sideB='ellipsoidal', sideB_a=2*u.m)
assert_pint_allclose(SA, 28.480278854014387, {u'[length]': 2.0})

SA, (sideA_SA, sideB_SA, lateral_SA) = SA_tank(D=1.*u.m, L=0*u.m, sideA='ellipsoidal', sideA_a=2*u.m, sideB='ellipsoidal', sideB_a=2*u.m, full_output=True)
expect = [28.480278854014387, 14.240139427007193, 14.240139427007193, 0]
for value, expected in zip([SA, sideA_SA, sideB_SA, lateral_SA], expect):
assert_pint_allclose(value, expected, {u'[length]': 2.0})



def test_check_signatures():
from fluids.units import check_args_order
for name in dir(fluids):
obj = getattr(fluids, name)
if isinstance(obj, types.FunctionType):
check_args_order(obj)

0 comments on commit 6e0533f

Please sign in to comment.