Skip to content

Commit

Permalink
add rtd for cstrs
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMVale committed Dec 7, 2024
1 parent 26f2700 commit 083a9e4
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/reference/reactors/rtd/_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* [cstr_cumulative](cstr_cumulative.md)
* [cstr_differential](cstr_differential.md)
* [tanks_series_cumulative](tanks_series_cumulative.md)
* [tanks_series_differential](tanks_series_differential.md)
6 changes: 6 additions & 0 deletions docs/reference/reactors/rtd/cstr_cumulative.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# polykin.reactors.rtd

::: polykin.reactors.rtd
options:
members:
- cstr_cumulative
6 changes: 6 additions & 0 deletions docs/reference/reactors/rtd/cstr_differential.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# polykin.reactors.rtd

::: polykin.reactors.rtd
options:
members:
- cstr_differential
6 changes: 6 additions & 0 deletions docs/reference/reactors/rtd/tanks_series_cumulative.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# polykin.reactors.rtd

::: polykin.reactors.rtd
options:
members:
- tanks_series_cumulative
6 changes: 6 additions & 0 deletions docs/reference/reactors/rtd/tanks_series_differential.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# polykin.reactors.rtd

::: polykin.reactors.rtd
options:
members:
- tanks_series_differential
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ nav:
- Thermal Conductivity: reference/properties/thermal_conductivity/
- Vaporization Enthalpy: reference/properties/vaporization_enthalpy/
- Viscosity: reference/properties/viscosity/
- Reactors:
- Residence Time Distributions: reference/reactors/rtd/
- Thermodynamics:
#- reference/thermo/index.md
- Activity Coeff. Models: reference/thermo/acm/
Expand Down
5 changes: 5 additions & 0 deletions src/polykin/reactors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# PolyKin: A polymerization kinetics library for Python.
#
# Copyright Hugo Vale 2024

from .rtd import *
122 changes: 122 additions & 0 deletions src/polykin/reactors/rtd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# PolyKin: A polymerization kinetics library for Python.
#
# Copyright Hugo Vale 2024

from math import gamma

from numpy import exp

__all__ = ['cstr_differential',
'cstr_cumulative',
'tanks_series_differential',
'tanks_series_cumulative',
]


def cstr_differential(t: float, tavg: float) -> float:
r"""Differential residence time distribution for a single CSTR.
$$ E(t) = \frac{1}{\bar{t}} e^{-t/\bar{t}} $$
**References**
* Levenspiel, O. "Chemical reaction engineering", John wiley & sons, 1998,
p. 322.
Parameters
----------
t : float
Time.
tavg : float
Average residence time, $\bar{t}$.
Returns
-------
float
Differential residence time distribution.
"""
return (1/tavg)*exp(-t/tavg)


def cstr_cumulative(t: float, tavg: float) -> float:
r"""Cumulative residence time distribution for a single CSTR.
$$ F(t) = 1 - e^{-t/\bar{t}} $$
**References**
* Levenspiel, O. "Chemical reaction engineering", John wiley & sons, 1998,
p. 327.
Parameters
----------
t : float
Time.
tavg : float
Average residence time, $\bar{t}$.
Returns
-------
float
Cumulative residence time distribution.
"""
return 1 - exp(-t/tavg)


def tanks_series_differential(t: float, tavg: float, N: int) -> float:
r"""Differential residence time distribution for a series of equal CSTRs.
$$ E(t) = \frac{1}{\bar{t}} \left(\frac{t}{\bar{t}} \right)^{N-1}
\frac{N^N}{(N-1)!} e^{-N t / \bar{t}} $$
**References**
* Levenspiel, O. "Chemical reaction engineering", John wiley & sons, 1998,
p. 322.
Parameters
----------
t : float
Time.
tavg : float
Total average residence time, $\bar{t}$.
N : int
Number of tanks in series.
Returns
-------
float
Differential residence time distribution.
"""
q = t/tavg
return q**(N-1) * (N**N / gamma(N)) * exp(-N*q) / tavg


def tanks_series_cumulative(t: float, tavg: float, N: int) -> float:
r"""Cumulative residence time distribution for a series of equal CSTRs.
$$ F(t) = 1 - e^{-N t / \bar{t}} \;
\sum_{i=0}^{N-1} \frac{(N t / \bar{t})^i}{i!} $$
**References**
* Levenspiel, O. "Chemical reaction engineering", John wiley & sons, 1998,
p. 327.
Parameters
----------
t : float
Time.
tavg : float
Total average residence time, $\bar{t}$.
N : int
Number of tanks in series.
Returns
-------
float
Cumulative residence time distribution.
"""
q = t/tavg
S = sum((N*q)**i / gamma(i+1) for i in range(1, N))
return 1 - exp(-N*q) * (1 + S)
39 changes: 39 additions & 0 deletions tests/reactors/test_rtd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from numpy import exp, isclose

from polykin.reactors.rtd import *


def test_cstr_differential():
assert isclose(cstr_differential(0, 1), 1)
assert isclose(cstr_differential(1e8, 1), 0)
assert isclose(cstr_differential(1, 1), exp(-1))


def testcstr_cumulative():
assert isclose(cstr_cumulative(0, 1), 0)
assert isclose(cstr_cumulative(1e8, 1), 1)
assert isclose(cstr_cumulative(1, 1), 0.63, rtol=0.01)


def test_tanks_series_differential():
# N = 1
N = 1
assert isclose(tanks_series_differential(0, 1, N), 1)
assert isclose(tanks_series_differential(1e8, 1, N), 0)
assert isclose(tanks_series_differential(1, 1, N), exp(-1))
# N > 1
for N in range(2, 10):
assert isclose(tanks_series_differential(0, 1, N), 0)
assert isclose(tanks_series_differential(1e8, 1, N), 0)
assert isclose(tanks_series_differential(1, 1, 20), 1.8, atol=0.1)


def test_tanks_series_cumulative():
for N in range(1, 10):
assert isclose(tanks_series_cumulative(0, 1, N), 0)
assert isclose(tanks_series_cumulative(1e8, 1, N), 1)
assert isclose(tanks_series_cumulative(1, 1, N=1), 0.63, rtol=0.01)
assert isclose(tanks_series_cumulative(0.85, 1, N=2), 0.5, atol=0.01)
assert isclose(tanks_series_cumulative(1, 1, N=20), 0.52, atol=0.01)
assert isclose(tanks_series_cumulative(0.5, 1, N=20), 0.0, atol=0.01)
assert isclose(tanks_series_cumulative(1.6, 1, N=20), 1.0, atol=0.01)

0 comments on commit 083a9e4

Please sign in to comment.