Differentiate: Modified Huron-Vidal mixrule¶
Example of differentiating the modified Huron-Vidal mix rule. Not heavy explanations, just doing it.
[ ]:
# Install if you are in Google Colab
import sys
if "google.colab" in sys.modules:
!pip install git+https://github.com/ipqa-research/thermodiff
# You can also do: "import thermodiff as td", and access to all this things.
from thermodiff import idx_function # Index based functions
from thermodiff import sum_components # Sympy summation over component number
from thermodiff import sum_custom # Sympy summation over custom limits
from thermodiff import n # Mole number vector
from thermodiff import T # Temperature
from thermodiff import P # Pressure
from thermodiff import V # Volume
from thermodiff import R # Ideal gas constant
from thermodiff import nc # Number of components in the mixture
from thermodiff import k, l, m # indexes for summations
from thermodiff import DiffPlz # Class that makes the derivatives
# We may still need sympy for some things.
import sympy as sp
# To display latext code in the notebook.
from IPython.display import display, Math
Build the expression to differentiate¶
[2]:
# Functions
ak = idx_function("a")(k, T)
b = sp.IndexedBase("b", shape=(nc,))
# i know that B and Ge are not index based, but we need lazy derivatives for
# that ones (sympy will apply chain rules otherwise)
B = idx_function("B")(n)
Dsym = idx_function("D")(n, T)
Ge = idx_function("G^E")(n, T)
nt = sum_components(n[l], l)
# Parameter
q = sp.symbols("q")
[3]:
D = (
R * T * B * (
sum_components(n[k] * ak / b[k], k) +
1 / q * (Ge / (R * T) + sum_components(n[k] * sp.log(B / (nt * b[k])), k))
)
)
D
[3]:
[4]:
diff = DiffPlz(D, [B, Dsym, ak, Ge], [k, l], "D")
dT and dT2¶
[5]:
diff.clean_plz(["dT", "dT2"])
[6]:
diff.dt
[6]:
[7]:
diff.dt2 = diff.dt2.simplify()
diff.dt2
[7]:
dTdni¶
because of the form of the equation, de cross derivative must have the compositional derivative over T:
Differentiating respect to \(T\) and then respect to \(n_i\)
The first term is equal to:
[8]:
diff.dtdni.has(
diff.dni.args[0] / T + diff.dni.args[1] / T
)
[8]:
True
[9]:
# The terms with only R at the start are the ones we have to replace with
# (1 / T) * dD / dn_i
diff.dtdni.args
[9]:
(R*(Sum(a(k, T)*n[k]/b[k], (k, 1, N_c)) + (Sum(log(B(n)/(b[k]*Sum(n[l], (l, 1, N_c))))*n[k], (k, 1, N_c)) + G^E(n, T)/(R*T))/q)*Derivative(B(n), n[i]),
R*(a(i, T)/b[i] + (log(B(n)/(b[i]*Sum(n[l], (l, 1, N_c)))) + Sum((-B(n)/(b[k]*Sum(n[l], (l, 1, N_c))**2) + Derivative(B(n), n[i])/(b[k]*Sum(n[l], (l, 1, N_c))))*b[k]*n[k]*Sum(n[l], (l, 1, N_c))/B(n), (k, 1, N_c)) + Derivative(G^E(n, T), n[i])/(R*T))/q)*B(n),
R*T*(Sum(Derivative(a(k, T), T)*n[k]/b[k], (k, 1, N_c)) + (Derivative(G^E(n, T), T)/(R*T) - G^E(n, T)/(R*T**2))/q)*Derivative(B(n), n[i]),
R*T*(Derivative(a(i, T), T)/b[i] + (Derivative(G^E(n, T), T, n[i])/(R*T) - Derivative(G^E(n, T), n[i])/(R*T**2))/q)*B(n))
[10]:
# I need the "i" index to manually add the derivative
from thermodiff import i
expr = diff.dtdni.args[2] + diff.dtdni.args[3] + sp.Derivative(Dsym, n[i]) / T
diff.dtdni = expr
diff.dtdni
[10]:
Check that the second term of dtdni is:
[11]:
diff.dtdni.args[1]
[11]:
Is equal to the first term of dt over \(B(n)\) multiplied by \(\frac{d B}{d n_i}\) (a little reuse of terms)
[12]:
diff.dt
[12]:
dni¶
[13]:
diff.dni
[13]:
Checkout that the second term of dni is:
so I will replace it
[14]:
diff.dni.args
[14]:
(R*T*(Sum(a(k, T)*n[k]/b[k], (k, 1, N_c)) + (Sum(log(B(n)/(b[k]*Sum(n[l], (l, 1, N_c))))*n[k], (k, 1, N_c)) + G^E(n, T)/(R*T))/q)*Derivative(B(n), n[i]),
R*T*(a(i, T)/b[i] + (log(B(n)/(b[i]*Sum(n[l], (l, 1, N_c)))) + Sum((-B(n)/(b[k]*Sum(n[l], (l, 1, N_c))**2) + Derivative(B(n), n[i])/(b[k]*Sum(n[l], (l, 1, N_c))))*b[k]*n[k]*Sum(n[l], (l, 1, N_c))/B(n), (k, 1, N_c)) + Derivative(G^E(n, T), n[i])/(R*T))/q)*B(n))
[15]:
expr = diff.dni.replace(
diff.dni.args[0],
Dsym / B * sp.diff(B, n[i])
)
diff.dni = expr
diff.dni
[15]:
Now I will clean up all the total mole number terms:
[16]:
diff.dni = diff.dni.replace(nt, sp.symbols(r"\textbf{n}"))
diff.dni
[16]:
Is possible to distribute that \(\textbf{n}\), but it’s okay…
dnidnj¶
The next is what happens when you not differentiate by terms:
[17]:
diff.dnidnj
[17]:
Just for the laughs (not that bad):
[18]:
diff.dnidnj.expand().simplify().replace(nt, sp.symbols(r"\textbf{n}"))
[18]:
Let’s do it better from the first derivative terms:
[19]:
Term1 = diff.dni.args[0]
Term1
[19]:
[20]:
Term2 = diff.dni.args[1]
Term2
[20]:
The first term is easier:
[21]:
from thermodiff import j
dTerm1 = sp.diff(Term1, n[j])
dTerm1
[21]:
The second term:
[22]:
dTerm2 = sp.diff(Term2, n[j]).replace(nt, sp.symbols(r"\textbf{n}"))
dTerm2
[22]:
Check that the first term of the dTerm2 is equal to the first term of dni over \(B\) multiplied by \(\frac{dB}{d n_j}\)
[23]:
diff.dni
[23]:
So we can express that first term of dTerm2 in terms of the first derivative of \(D\) as:
[24]:
dTerm2 = dTerm2.replace(
dTerm2.args[0],
(sp.diff(Dsym, n[i]) - Dsym * sp.diff(B, n[i]) / B) / B * sp.diff(B, n[j])
)
dTerm2
[24]:
then we have the ugly Kroneckers. Lets see if I can take them out by hand:
[25]:
import thermodiff as td
dTerm2 = td.handle_sum_kronecker(dTerm2, j)
dTerm2
[25]:
[26]:
diff.dnidnj = dTerm1 + dTerm2
diff.dnidnj = diff.dnidnj.simplify()
diff.dnidnj
[26]:
[27]:
diff.dnidnj = diff.dnidnj.replace(sum_components(n[k], k), sp.symbols(r"\textbf{n}"))
diff.dnidnj
[27]:
Clean Latex¶
[28]:
latex = diff.latex_readable_plz()
derivs = ["dT", "dT2", "dTn", "dni", "dn2"]
for d in derivs:
print(f"{d}:")
display(Math(latex[d]))
dT:
dT2:
dTn:
dni:
dn2:
[29]:
latex = diff.latex_readable_plz()
derivs = ["dT", "dT2", "dTn", "dni", "dn2"]
for d in derivs:
print(f"{d}:")
print(latex[d])
dT:
B R T \left(\sum_{k=1}^{N_{c}} \frac{\frac{\partial a_{k}}{\partial T} {n}_{k}}{{b}_{k}} + \frac{- \frac{G^{E}}{R T^{2}} + \frac{\frac{\partial G^{E}}{\partial T}}{R T}}{q}\right) + \frac{D{\left(n,T \right)}}{T}
dT2:
B \left(R \sum_{k=1}^{N_{c}} \frac{\left(T \frac{\partial^2 a_{k}}{\partial T^2} + 2 \frac{\partial a_{k}}{\partial T}\right) {n}_{k}}{{b}_{k}} + \frac{\frac{\partial^2 G^{E}}{\partial T^2}}{q}\right)
dTn:
B R T \left(\frac{\frac{\partial a_{i}}{\partial T}}{{b}_{i}} + \frac{\frac{\frac{\partial^2 G^{E}}{\partial T \partial n_i}}{R T} - \frac{\frac{\partial G^{E}}{\partial n_i}}{R T^{2}}}{q}\right) + R T \frac{\partial B}{\partial n_i} \left(\sum_{k=1}^{N_{c}} \frac{\frac{\partial a_{k}}{\partial T} {n}_{k}}{{b}_{k}} + \frac{- \frac{G^{E}}{R T^{2}} + \frac{\frac{\partial G^{E}}{\partial T}}{R T}}{q}\right) + \frac{\frac{\partial D}{\partial n_i}}{T}
dni:
B R T \left(\frac{a_{i}}{{b}_{i}} + \frac{\log{\left(\frac{B}{\textbf{n} {b}_{i}} \right)} + \sum_{k=1}^{N_{c}} \frac{\textbf{n} \left(- \frac{B}{\textbf{n}^{2} {b}_{k}} + \frac{\frac{\partial B}{\partial n_i}}{\textbf{n} {b}_{k}}\right) {b}_{k} {n}_{k}}{B} + \frac{\frac{\partial G^{E}}{\partial n_i}}{R T}}{q}\right) + \frac{D \frac{\partial B}{\partial n_i}}{B}
dn2:
- \frac{B R T}{\textbf{n} q} + \frac{B \frac{\partial^2 G^{E}}{\partial n_i \partial n_j}}{q} + \frac{R T \frac{\partial B}{\partial n_i}}{q} + \frac{R T \frac{\partial B}{\partial n_j}}{q} + \frac{R T \textbf{n} \left(\frac{\partial^2 B}{\partial n_i \partial n_j} - \frac{\frac{\partial B}{\partial n_i} \frac{\partial B}{\partial n_j}}{B}\right)}{q} + \frac{D \frac{\partial^2 B}{\partial n_i \partial n_j}}{B} + \frac{\frac{\partial B}{\partial n_i} \frac{\partial D}{\partial n_j}}{B} + \frac{\frac{\partial B}{\partial n_j} \frac{\partial D}{\partial n_i}}{B} - \frac{2 D \frac{\partial B}{\partial n_i} \frac{\partial B}{\partial n_j}}{B^{2}}