使用具有类的Antoine方程评估温度

时间:2019-02-18 21:07:20

标签: python python-3.x numpy jupyter-notebook

我需要通过使用类来获得使用Antoine Eq的温度的帮助。

我的根本动作失败了,我也不知道为什么。

我的代码:

from __future__ import division, print_function
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import root

class Chemical(object):

    def __init__(self, name_1, balance_1, name_2, balance_2):
        self.name = name_1 + ' ' + '+' + ' ' + name_2
        self.data_1 = []
        self.data_2 = []
        self.data_1 = balance_1
        self.data_2 = balance_2

    def __str__(self):
        if (self.name):
            return "Mixture: %s" % (self.name)
        else:
            return None

    def bubble_solve(self,a,P = 1.01,T = 300):
        A1,B1,C1 = self.data_1
        A2,B2,C2 = self.data_2
        PA = 10**(A1 - B1/(C1+T)) 
        PB = 10**(A2 - B2/(C2+T)) 
        sol = root(lambda T: P - (a*PA + (1-a)*PB),T)
        return sol.x[0]

    def dew_solve(self, b, P = 1.01, T = 300):
        A1,B1,C1 = self.data_1
        A2,B2,C2 = self.data_2
        PA = 10**(A1 - B1/(C1+T)) 
        PB = 10**(A2 - B2/(C2+T)) 
        sol = root(lambda T: 1 - (b*P/PA + (1-b)*P/PB), T)
        return sol.x[0]


mixture = Chemical('benzene', [ 3.98523 , 1184.24 , -55.578], 'toulene', 
[4.05043 , 1327.62 , -55.528])
print(mixture)
print()

print(mixture.bubble_solve(0.5)) #at a = 0.5
print(mixture.bubble_solve(0.5,2)) #at a = 0.5 and P = 2
print(mixture.dew_solve(0.5)) #at b = 0.5
print(mixture.dew_solve(0.5,2)) #at b = 0.5 and P = 2

这是我的代码正在打印:

Mixture: benzene + toulene

300.0
300.0
300.0
300.0

但是,答案必须是: 365.087、390.14188、371.7743、396.688。

为什么root操作失败?

1 个答案:

答案 0 :(得分:0)

欢迎来到StackOverflow,@ Nathaniel!

问题出在PAPB中的solbubble_solvedew_solve行中。您似乎正在将常量与自变量混合!例如,对于气泡求解,您为T设置了默认值300。因此,在PA行中,值300被用于T!解决此问题的一种方法是:

    ...
    def bubble_solve(self,a,P = 1.01,T = 300):
        A1,B1,C1 = self.data_1
        A2,B2,C2 = self.data_2
        PA = lambda x: 10**(A1 - B1/(C1+x)) 
        PB = lambda x: 10**(A2 - B2/(C2+x)) 
        sol = root(lambda x: P - (a*PA(x) + (1-a)*PB(x)),T)
        return sol.x[0]

    def dew_solve(self, b, P = 1.01, T = 300):
        A1,B1,C1 = self.data_1
        A2,B2,C2 = self.data_2
        PA = lambda x: 10**(A1 - B1/(C1+x)) 
        PB = lambda x: 10**(A2 - B2/(C2+x)) 
        sol = root(lambda x: 1 - (b*P/PA(x) + (1-b)*P/PB(x)), T)
        return sol.x[0]
    ...

这将返回您期望的值。请注意,我使用x作为PAPB的自变量,但是它是任意的。但是,如果要向其传递值,最好不要为匿名函数重用T