Skip to content

Commit

Permalink
Finish adding a few long overdue utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebBell committed Oct 8, 2023
1 parent 1057a25 commit 4c85e6d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions chemicals/numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
to_change = ['utils.zs_to_ws', 'utils.ws_to_zs', 'utils.zs_to_Vfs',
'utils.ms_to_ns', 'utils.ns_to_ms',
'utils.ns_to_Qls', 'utils.Qls_to_ns',
'utils.Qls_to_ms', 'utils.ms_to_Qls',
'utils.dxs_to_dxsn1', 'utils.dxs_to_dns', 'utils.dns_to_dn_partials',
'utils.dxs_to_dn_partials', 'utils.dxs_to_dxsn1',
'utils.d2xs_to_dxdn_partials', 'viscosity.Lorentz_Bray_Clarke',
Expand Down
83 changes: 80 additions & 3 deletions chemicals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'Z', 'zs_to_ws', 'ws_to_zs', 'zs_to_Vfs',
'Vfs_to_zs',
'ms_to_ns', 'ns_to_ms', 'ns_to_Qls', 'Qls_to_ns',
'Qls_to_ms', 'ms_to_Qls',
'none_and_length_check', 'normalize', 'remove_zeros',
'mixing_simple',
'mixing_logarithmic', 'mixing_power', 'to_num', 'Parachor', 'property_molar_to_mass', 'property_mass_to_molar',
Expand Down Expand Up @@ -1006,6 +1007,7 @@ def Cp_minus_Cv(T, dP_dT, dP_dV):
C_p - C_v= -T\left(\frac{\partial V}{\partial T}\right)_P^2/\left(
\frac{\partial V}{\partial P}\right)_T
.. math::
C_p - C_v = T\left(\frac{\partial P}{\partial T}\right)
\left(\frac{\partial V}{\partial T}\right)
Expand Down Expand Up @@ -1494,6 +1496,7 @@ def zs_to_ws(zs, MWs):
.. math::
w_i = \frac{z_i MW_i}{MW_{avg}}
.. math::
MW_{avg} = \sum_i z_i MW_i
Parameters
Expand Down Expand Up @@ -1707,7 +1710,7 @@ def ns_to_ms(ns, MWs):
weights for all species.
.. math::
m_i = \frac{n_i \times MW_i}{1000}
m_i = \frac{n_i MW_i}{1000}
Parameters
----------
Expand Down Expand Up @@ -1741,7 +1744,7 @@ def ns_to_Qls(ns, Vmls):
flow rates. Requires standard liquid molar volumes for all species.
.. math::
{Ql}_i = n_i \times {Vml}_i
{Ql}_i = n_i {Vml}_i
Parameters
----------
Expand Down Expand Up @@ -1775,7 +1778,7 @@ def Qls_to_ns(Qls, Vmls):
to mole flow rates. Requires standard liquid molar volumes for all species.
.. math::
n_i = \frac{Ql_i}{Vml_i}
n_i = \frac{{Ql}_i}{{Vml}_i}
Parameters
----------
Expand Down Expand Up @@ -1804,6 +1807,80 @@ def Qls_to_ns(Qls, Vmls):
ns[i] = Qls[i]/Vmls[i]
return ns

def ms_to_Qls(ms, MWs, Vmls):
r'''Converts a list of mass flow rates to standard liquid volume
flow rates. Requires molecular weights and standard molar liquid
volumes for all species.
.. math::
{Ql}_i = \frac{1000 m_i {Vml}_i}{MW_i}
Parameters
----------
ms : iterable
Mass flow rates [kg/s]
MWs : iterable
Molecular weights [g/mol]
Vmls : iterable
Standard liquid molar volumes [m^3/mol]
Returns
-------
Qls : iterable
Standard liquid volume flow rates [m^3/s]
Notes
-----
Does not check that inputs are of the same length.
Examples
--------
>>> ms_to_Qls([4.0, 5.0], [24, 45], [1e-4, 2e-4])
[0.0166666666, 0.0222222222]
'''
N = len(ms)
Qls = [0.0]*N
for i in range(N):
Qls[i] = 1e3*ms[i]/MWs[i]*Vmls[i]
return Qls

def Qls_to_ms(Qls, MWs, Vmls):
r'''Converts a list of standard liquid volume flow rates to mass
flow rates. Requires molecular weights and standard liquid molar
volumes for all species.
.. math::
m_i = \frac{{Ql}_i {MW}_i}{1000 {Vml}_i}
Parameters
----------
Qls : iterable
Standard liquid volume flow rates [m^3/s]
MWs : iterable
Molecular weights [g/mol]
Vmls : iterable
Molar volumes in the liquid phase [m^3/mol]
Returns
-------
ms : iterable
Mass flow rates [kg/s]
Notes
-----
Does not check that inputs are of the same length.
Examples
--------
>>> Qls_to_ms([1.666666666e-02, 1.11111111e-01], [24, 45], [1e-4, 2e-4])
[4.0, 25.0]
'''
N = len(Qls)
ms = [0.0]*N
for i in range(N):
ms[i] = 1e-3*Qls[i]*MWs[i]/Vmls[i]
return ms

def dxs_to_dns(dxs, xs, dns=None):
r'''Convert the mole fraction derivatives of a quantity (calculated so
they do not sum to 1) to mole number derivatives (where the mole fractions
Expand Down
14 changes: 13 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
ns_to_ms,
ns_to_Qls,
Qls_to_ns,
Qls_to_ms,
ms_to_Qls,
)


Expand Down Expand Up @@ -693,4 +695,14 @@ def test_ns_to_Qls():

def test_Qls_to_ns():
ans = Qls_to_ns([3.405135210129374e-07, 1.885565470149901e-05, 3.4208426220155466e-06] , [1.8087205105724903e-05, 5.858784737690099e-05, 0.00019580845677748954])
assert_close1d(ans, [0.018826209965693327, 0.3218355946788565, 0.0174703517831351])
assert_close1d(ans, [0.018826209965693327, 0.3218355946788565, 0.0174703517831351])


def test_ms_to_Qls():
ans = ms_to_Qls([0.00033915944387075575, 0.005163034654211768, 0.002485711001895458], [18.01528, 16.04246, 142.28168], [1.8087205105724903e-05, 5.858784737690099e-05, 0.00019580845677748954])
assert_close1d(ans, [3.405135210129374e-07, 1.885565470149901e-05, 3.4208426220155466e-06])
# Qls_to_ms, ms_to_Qls

def test_Qls_to_ms():
ans = Qls_to_ms([3.405135210129374e-07, 1.885565470149901e-05, 3.4208426220155466e-06], [18.01528, 16.04246, 142.28168], [1.8087205105724903e-05, 5.858784737690099e-05, 0.00019580845677748954])
assert_close1d(ans, [0.00033915944387075575, 0.005163034654211768, 0.002485711001895458])

0 comments on commit 4c85e6d

Please sign in to comment.