In [30]:
from sympy import *
init_session(quiet=True)
init_printing()
Yields a Helmholtz energy term in the form
$$\alpha^{ass} = A\left(\ln X-\frac{X}{2}+\frac{1}{2} \right) $$where
$$X = \frac{2}{\sqrt{1+4\bar\Delta\delta}+1}$$$$\bar\Delta(\delta,\tau) = g(\eta(\delta))[\exp(\bar\varepsilon\tau)-1]\bar\kappa$$$$\eta = \bar v_n\delta $$$$ g(\eta) = \frac{1}{2}\frac{2-\eta}{(1-\eta)^3} $$Following the patterns of the derivatives
$$\left(\frac{\partial^3 \bar \Delta}{\partial \tau^3} \right)_{\delta} = g(\eta)\bar\kappa\exp(\bar\varepsilon\tau)\bar \varepsilon^3 $$$$\frac{\partial^3 \bar \Delta}{\partial\delta\partial \tau^2} = \frac{dg(\eta)}{d\eta}\bar\kappa\exp(\bar\varepsilon\tau)\bar \varepsilon^2\bar v_n$$$$\frac{\partial^3 \bar \Delta}{\partial \tau\partial\delta^2} = \frac{d^2g(\eta)}{d\eta^2}\exp(\bar\varepsilon\tau)\bar\varepsilon\bar\kappa\bar v_n^2$$$$\left(\frac{\partial^3 \bar \Delta}{\partial \delta^3} \right)_{\tau} = \frac{d^3g(\eta)}{d\eta^3}[\exp(\bar\varepsilon\tau)-1]\bar\kappa\bar v_n^3 $$
In [31]:
X,delta,Delta,Delta_t,Delta_tt = symbols('X,delta,Delta,Delta_t,Delta_tt')
X_tt = (2*Delta_t**2*delta**2*X**3*(Delta*delta*X+1)/(2*Delta*delta*X+1)**3
+(2*delta**2*X**3*Delta_t**2)/(2*Delta*delta*X+1)**2
-(delta*X**2*Delta_tt)/(2*Delta*delta*X+1)
)
print 'dX = '+ccode(simplify(diff(X_tt,X)))
print 'ddelta = '+ccode(simplify(diff(X_tt,delta)))
print 'dDelta = '+ccode(simplify(diff(X_tt,Delta)))
print 'dDelta_t = '+ccode(simplify(diff(X_tt,Delta_t)))
print 'dDelta_tt = '+ccode(simplify(diff(X_tt,Delta_tt)))
In [32]:
X,delta,Delta,Delta_d,Delta_dd = symbols('X,delta,Delta,Delta_d,Delta_dd')
X_dd = (X**2*(2*Delta**2*X-Delta_d)/(2*Delta*delta*X+1)**2
-(Delta+delta*Delta_d)*2*(Delta*delta*X**2+X)/(2*Delta*delta*X+1)**2*(-Delta*X**2/(2*Delta*delta*X+1))
-(Delta+delta*Delta_d)*2*(Delta*delta*X**2+X)/(2*Delta*delta*X+1)**2*(-delta*X**2/(2*Delta*delta*X+1))*Delta_d
+X**2*(2*delta**2*X*Delta_d-1)/(2*Delta*delta*X+1)**2*Delta_d
-delta*X**2/(2*Delta*delta*X+1)*Delta_dd
)
print 'dX = '+ccode(simplify(diff(X_dd,X)))
print 'ddelta = '+ccode(simplify(diff(X_dd,delta)))
print 'dDelta = '+ccode(simplify(diff(X_dd,Delta)))
print 'dDelta_d = '+ccode(simplify(diff(X_dd,Delta_d)))
print 'dDelta_dd = '+ccode(simplify(diff(X_dd,Delta_dd)))
In [28]:
from __future__ import division
from math import exp, log
A = 2
class Methanol(object):
m = 0.977118832
vbarn = 0.204481952
kappabar = 0.148854832e-2
epsilonbar = 5.46341463
def X(self, delta, Deltabar):
return 2/((1+4*Deltabar*delta)**0.5+1)
def dX_dDeltabar__constdelta(self, delta, Deltabar):
X = self.X(delta,Deltabar)
return -delta*X**2/(2*Deltabar*delta*X+1)
def dX_ddelta__constDeltabar(self, delta, Deltabar):
X = self.X(delta,Deltabar)
return -Deltabar*X**2/(2*Deltabar*delta*X+1)
def dX_dtau(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
return self.dX_dDeltabar__constdelta(delta, Deltabar)*self.dDeltabar_dtau__constdelta(tau, delta)
def dX_ddelta(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
return (self.dX_ddelta__constDeltabar(delta, Deltabar)
+ self.dX_dDeltabar__constdelta(delta, Deltabar)*self.dDeltabar_ddelta__consttau(tau, delta))
def d2X_dtau2(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
X = self.X(delta, Deltabar)
beta = self.dDeltabar_dtau__constdelta(tau, delta)
d_dXdtau_dbeta = -delta*X**2/(2*Deltabar*delta*X+1)
d_dXdtau_dDeltabar = 2*delta**2*X**3/(2*Deltabar*delta*X+1)**2*beta
d_dXdtau_dX = -2*beta*delta*X*(Deltabar*delta*X+1)/(2*Deltabar*delta*X+1)**2
dbeta_dtau = self.d2Deltabar_dtau2__constdelta(tau, delta)
dX_dDeltabar = self.dX_dDeltabar__constdelta(delta, Deltabar)
val = d_dXdtau_dX*dX_dDeltabar*beta+d_dXdtau_dDeltabar*beta+d_dXdtau_dbeta*dbeta_dtau
#print val, self.X_tt(X,delta,Deltabar,beta,dbeta_dtau)
return val
def X_tt(self, X, delta, Delta, Delta_t, Delta_tt):
return (2*Delta_t**2*delta**2*X**3*(Delta*delta*X+1)/(2*Delta*delta*X+1)**3
+(2*delta**2*X**3*Delta_t**2)/(2*Delta*delta*X+1)**2
-(delta*X**2*Delta_tt)/(2*Delta*delta*X+1)
)
def d3X_dtau3(self, tau, delta):
Delta = self.Deltabar(tau, delta)
Delta_t = self.dDeltabar_dtau__constdelta(tau, delta)
Delta_tt = self.d2Deltabar_dtau2__constdelta(tau, delta)
dX_dDeltabar = self.dX_dDeltabar__constdelta(tau, delta)
dDeltabar_dtau = self.dDeltabar_dtau__constdelta(tau, delta)
dDeltabart_dtau = self.d2Deltabar_dtau2__constdelta(tau, delta)
dDeltabartt_dtau = self.d3Deltabar_dtau3__constdelta(tau, delta)
X = self.X(delta, Delta)
d = delta
t = tau
dXtt_dDeltabartt = (-d*X**2)/(1+2*Delta*d*X)
dXtt_dDeltabart = ((4*Delta_t*d**2*X**3*(Delta*d*X+1))/(2*Delta*d*X+1)**3
+(4*d**2*X**3*Delta_t)/(2*Delta*d*X+1)**2
)
dXtt_dDeltabar = ((-12*Delta_t**2*d**3*X**4*(Delta*d*X+1))/(2*Delta*d*X+1)**4
-(6*d**3*X**4*Delta_t**2)/(2*Delta*d*X+1)**3
+(2*d**2*X**3*Delta_tt)/(2*Delta*d*X+1)**2
)
dXtt_dX = (-(12*Delta_t**2*Delta*d**3*X**3*(Delta*d*X+1))/(2*Delta*d*X+1)**4
+2*d**2*X**2*(3*Delta_t**2+Delta_tt*Delta)/(2*Delta*d*X+1)**2
-(2*d*X*Delta_tt)/(2*Delta*d*X+1)
)
print '1', dXtt_dDeltabartt, (self.X_tt(X,delta,Delta,Delta_t,Delta_tt+1e-8)-self.X_tt(X,delta,Delta,Delta_t,Delta_tt-1e-8))/(2e-8)
print '2', dXtt_dDeltabart, (self.X_tt(X,delta,Delta,Delta_t+1e-8,Delta_tt)-self.X_tt(X,delta,Delta,Delta_t-1e-8,Delta_tt))/(2e-8)
print '3', dXtt_dDeltabar, (self.X_tt(X,delta,Delta+1e-8,Delta_t,Delta_tt)-self.X_tt(X,delta,Delta-1e-8,Delta_t,Delta_tt))/(2e-8)
print '4', dXtt_dX, (self.X_tt(X+1e-8,delta,Delta,Delta_t,Delta_tt)-self.X_tt(X-1e-8,delta,Delta,Delta_t,Delta_tt))/(2e-8)
return (dXtt_dX*dX_dDeltabar*dDeltabar_dtau
+dXtt_dDeltabar*dDeltabar_dtau
+dXtt_dDeltabart*dDeltabart_dtau
+dXtt_dDeltabartt*dDeltabartt_dtau)
def d2X_ddeltadtau(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
X = self.X(delta, Deltabar)
alpha = self.dDeltabar_ddelta__consttau(tau, delta)
beta = self.dDeltabar_dtau__constdelta(tau, delta)
dalpha_dtau = self.d2Deltabar_ddelta_dtau(tau, delta)
d_dXddelta_dDeltabar = X**2*(2*delta**2*X*alpha-1)/(2*Deltabar*delta*X+1)**2
d_dXddelta_dalpha = -delta*X**2/(2*Deltabar*delta*X+1)
d_dXddelta_dX = -(Deltabar+delta*alpha)*2*(Deltabar*delta*X**2+X)/(2*Deltabar*delta*X+1)**2
dX_dDeltabar = self.dX_dDeltabar__constdelta(delta, Deltabar)
return d_dXddelta_dX*dX_dDeltabar*beta+d_dXddelta_dDeltabar*beta+d_dXddelta_dalpha*dalpha_dtau
def d2X_ddelta2(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
X = self.X(delta, Deltabar)
alpha = self.dDeltabar_ddelta__consttau(tau, delta)
dalpha_ddelta = self.d2Deltabar_ddelta2__consttau(tau, delta)
d_dXddelta_dDeltabar = X**2*(2*delta**2*X*alpha-1)/(2*Deltabar*delta*X+1)**2
d_dXddelta_dalpha = -delta*X**2/(2*Deltabar*delta*X+1)
d_dXddelta_dX = -(Deltabar+delta*alpha)*2*(Deltabar*delta*X**2+X)/(2*Deltabar*delta*X+1)**2
dX_dDeltabar = self.dX_dDeltabar__constdelta(delta, Deltabar)
dX_ddelta_constall = X**2*(2*Deltabar**2*X-alpha)/(2*Deltabar*delta*X+1)**2
return (dX_ddelta_constall
+ d_dXddelta_dX*self.dX_ddelta__constDeltabar(delta, Deltabar)
+ d_dXddelta_dX*dX_dDeltabar*alpha
+ d_dXddelta_dDeltabar*alpha
+ d_dXddelta_dalpha*dalpha_ddelta)
def g(self, eta):
return 0.5*(2-eta)/(1-eta)**3
def dg_deta(self, eta):
return 0.5*(5-2*eta)/(1-eta)**4
def d2g_deta2(self, eta):
return 3*(3-eta)/(1-eta)**5
def d3g_deta3(self, eta):
return 6*(7-2*eta)/(1-eta)**6
def eta(self, delta):
return self.vbarn*delta
def Deltabar(self, tau, delta):
return self.g(self.eta(delta))*(exp(self.epsilonbar*tau)-1)*self.kappabar
def dDeltabar_ddelta__consttau(self, tau, delta):
return self.dg_deta(self.eta(delta))*(exp(self.epsilonbar*tau)-1)*self.kappabar*self.vbarn
def d2Deltabar_ddelta2__consttau(self, tau, delta):
return self.d2g_deta2(self.eta(delta))*(exp(self.epsilonbar*tau)-1)*self.kappabar*self.vbarn**2
def dDeltabar_dtau__constdelta(self, tau, delta):
return self.g(self.eta(delta))*self.kappabar*exp(self.epsilonbar*tau)*self.epsilonbar
def d2Deltabar_dtau2__constdelta(self, tau, delta):
return self.g(self.eta(delta))*self.kappabar*exp(self.epsilonbar*tau)*self.epsilonbar**2
def d3Deltabar_dtau3__constdelta(self, tau, delta):
return self.g(self.eta(delta))*self.kappabar*exp(self.epsilonbar*tau)*self.epsilonbar**3
def d2Deltabar_ddelta_dtau(self, tau, delta):
return self.dg_deta(self.eta(delta))*exp(self.epsilonbar*tau)*self.epsilonbar*self.kappabar*self.vbarn
def base(self, tau, delta):
X = self.X(delta, self.Deltabar(tau, delta))
return 2*(log(X)-X/2.0+0.5)
def dDelta(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
X = self.X(delta, Deltabar)
return 2*(1/X-0.5)*self.dX_ddelta(tau, delta)
def dTau(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
X = self.X(delta, Deltabar)
return 2*(1/X-0.5)*self.dX_dtau(tau, delta)
def dTau2(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
X = self.X(delta, Deltabar)
X_tau = self.dX_dtau(tau, delta)
X_tautau = self.d2X_dtau2(tau, delta)
return 2*(1/X-0.5)*X_tautau-2*(X_tau/X)**2
def dDeltadTau(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
X = self.X(delta, Deltabar)
X_delta = self.dX_ddelta(tau, delta)
X_tau = self.dX_dtau(tau, delta)
X_deltatau = self.d2X_ddeltadtau(tau, delta)
return 2*(-X_tau/X**2)*X_delta+2*X_deltatau*(1/X-0.5)
def dDelta2(self, tau, delta):
Deltabar = self.Deltabar(tau, delta)
X = self.X(delta, Deltabar)
X_delta = self.dX_ddelta(tau, delta)
X_deltadelta = self.d2X_ddelta2(tau, delta)
return 2*(1/X-0.5)*X_deltadelta-2*(X_delta/X)**2
def dTau3(self, tau, delta):
X = self.X(delta, Deltabar)
X_t = self.dX_dtau(tau, delta)
X_tt = self.d2X_dtau2(tau, delta)
X_ttt = self.d3X_dtau3(tau, delta)
return (A*(1/X-1/2)*X_ttt
+A*(-X_t/X**2)*X_tt
-2*A*(X**2*(X_t*X_tt)-X_t**2*(X*X_t))/X**4
)
M = Methanol()
#print M.base(0.5,0.5)
Deltabar = M.Deltabar(0.5,0.5)
#print M.dX_dDeltabar__constdelta(0.5,Deltabar),(M.X(0.5,Deltabar+1e-8)-M.X(0.5,Deltabar))/1e-8
print 'ttt', M.d3X_dtau3(0.5,0.5),(M.d2X_dtau2(0.5+1e-8,0.5)-M.d2X_dtau2(0.5-1e-8,0.5))/2e-8
print 'd',M.dDelta(0.5,0.5), (M.base(0.5,0.5+1e-8)-M.base(0.5,0.5))/1e-8
print 't',M.dTau(0.5,0.5), (M.base(0.5+1e-8,0.5)-M.base(0.5,0.5))/1e-8
print 'tt',M.dTau2(0.5,0.5), (M.dTau(0.5+1e-8,0.5)-M.dTau(0.5,0.5))/1e-8
print 'ttt',M.dTau3(0.5,0.5), (M.dTau2(0.5+1e-8,0.5)-M.dTau2(0.5,0.5))/1e-8
print 'dt',M.dDeltadTau(0.5,0.5), (M.dTau(0.5,0.5+1e-8)-M.dTau(0.5,0.5))/1e-8
print 'dd',M.dDelta2(0.5,0.5), (M.dDelta(0.5,0.5+1e-8)-M.dDelta(0.5,0.5))/1e-8
In [ ]: