用 Python 模拟热力学熵
熵是热力学系统的一个属性,在可逆绝热过程中保持恒定。此外,我们还可以说它是系统中随机性或无序性的程度。如果一个系统在其周围环境中以温度 T 交换 dQ 热量,则熵的变化可以写成 -
$$\mathrm{ds \: = \: \frac{dQ}{T} \dotso \dotso \: (1)}$$
根据克劳修斯不等式,$\mathrm{\frac{dQ}{T}}$ 沿可逆路径的循环积分要么小于或等于零。数学上,它可以写成 -
$$\mathrm{\oint\frac{dQ}{T} \: \leq \: 0\dotso \dotso \: (2)}$$
等式适用于可逆循环,不等式适用于不可逆循环。任何不遵循公式 2 的发动机循环都是不可能的。
对于不同类型的过程,熵变的计算方式不同。在显热相互作用期间,由于没有相变,只有温度变化,则从状态 1 到状态 2 的熵变可以写成 -
$$\mathrm{\triangle S \: = \: mc_{p} \: In \: (\frac{T_{2}}{T_{1}})\dotso \dotso \: (3)}$$
其中,$\mathrm{c_{p}}$ 是恒压比热容。而如果存在相变,则温度不会发生变化,因此熵的计算方法只是潜热除以相变温度,如下所示 -
$$\mathrm{\triangle S \: = \: \frac{mL}{T}\dotso \dotso \: (4)}$$
其中,L 是比潜热。公式 3 和 4 中提到的熵是总熵,即单位为 kJ/K,但在大多数情况下,我们处理的是比性质,因此比熵(kJ/kg-K)用小写 s 表示,定义为 -
$$\mathrm{s \: = \: \frac{ds}{dm}\dotso \dotso \: (5)}$$
在某个过程中,如果气体状态从压力和温度 $\mathrm{p_{1} \: , \: T_{1}}$ 变为 $\mathrm{p_{2} \: , \: T_{2}}$,则比熵的变化可以写成 -
$$\mathrm{\triangle s \: = \: c_{p} \: In \: (\frac{T_{2}}{T_{1}}) \: − \: R \: In \: (\frac{p_{2}}{p_{1}})\dotso \dotso \: (6)}$$
在许多情况下,使用熵可以很容易地解决问题。这些特殊情况包括 -
当两个相同系统分别在温度 $\mathrm{T_{1}}$ 和 $\mathrm{T_{2}}$ 下通过可逆发动机连接时,从这些有限物体中获得的最大功及其最终平衡温度将由下式给出 -
$$\mathrm{W_{max} \: = \: C \: \times \:(\sqrt{T_{1}} \: − \: \sqrt{T_{2}})^{2}\dotso \dotso \: (7)}$$
$$\mathrm{T_{eq} \: = \: \sqrt{T_{1} \: \times \: T_{2}}\dotso \dotso \: (8)}$$
其中,C 是系统的热容量,是质量和比热的乘积。
在相同热容量 (C) 但不同温度 $\mathrm{T_{1} \: and \: T_{2}}$ 的两种流体混合期间,整个系统的熵变将由下式给出 -
$$\mathrm{\triangle S \: = \: C \: In \:(\frac{(T_{1} \: + \: T_{2})/2}{\sqrt{T_{1}T_{2}}})\dotso \dotso \: (9)}$$
从系统(在温度 T 下)和热能储库(在温度 $\mathrm{T_{0}}$ 下)通过可逆发动机进行通信获得的最大功为 -
$$\mathrm{W_{max} \: = \: C \:((T_ \: − \: T_{0}) \: − \: T_{0} \: In \: (\frac{T}{T_{0}})\dotso \dotso \: (10)}$$
用于模拟热力学熵的 Python 程序
以下函数是用 Python 编写的,用于对不同情况下的熵进行计算 -
相变过程中的熵变
def s_pc(T,L): return L/T
显热传递过程中的熵变
def s_se(c,T1,T2,m=1): return m*c*log(T2/T1)
克劳修斯不等式的建模
def clausius_inequality(Q,T):
Sm=sum(Q/T)
if Sm<0:
print("The cycle is irreversible and possible")
elif Sm==0:
print("The cycle is reversible and possible")
else:
print("The cycle is not possible")
从有限热容量物体中获得的最大功
def max_work_fb(T1,T2,c): Tf=sqrt(T1*T2) work=c*(sqrt(T1)-sqrt(T2))**2 return work,Tf
从与热能储库相互作用的有限热容量物体中获得的最大功
def max_work_fb_TER(T,T0,c): return c*((T-T0)-T0*log(T/T0))
给定压力和温度变化的情况下,气体在过程中的熵变。
def s_process(T1,T2,p1,p2,R,cp): return cp*log(T2/T1)-R*log(p2/p1)
绘制相变过程 (液体到蒸汽) 的 T-S 图的函数
def plot_pc(Δs1,Δs2,Δs3,Ti,Tpc,Tf):
# Plotting cycle
s1=0
s2=s1+Δs1
s3=s2+Δs2
s4=s3+Δs3
# 1-2
s=linspace(s1,s2,10)
T=empty(len(s))
T[0]=Ti
for i in range(1,len(s)):
T[i]=T[i-1]/(1-(s[1]-s[0])/ci)
plot(s,T,'r-')
# 2-3
s=linspace(s2,s3,20)
T=zeros(20)+Tpc
plot(s,T,'b-')
# 3-1
s=linspace(s3,s4,20)
T=empty(len(s))
T[0]=Tpc
for i in range(1,len(s)):
T[i]=T[i-1]/(1-(s[1]-s[0])/cw)
plot(s,T,'g-')
xlabel('S')
ylabel('T')
savefig("Entropy_pc_jpg")
show()
除了这些之外,还有一个非常有用的函数是过程中的热量交互量(包括显热和潜热)
#-----------------------------------------------
# Heat transfer during phase change
#-----------------------------------------------
def q_mel_sol(m,Ti,Tf,T_pc,c_bpc,c_apc,L):
"""
Function for the evaluation of heat transfer during phase change
Input : mass (m), initial temp (Ti), final temp (Tf),
phase change temp (T_pc),sp. heat below phase change (c_bpc),
sp. heat above phase change (c_apc), latent heat (L)
Output: heat interaction
"""
if Ti>Tf:
print('Process is either freezing or condensation')
return m*(c_bpc*(T_pc-Ti)-L+c_apc*(Tf-T_pc))
else:
print('Process is either melting or vaporization')
return m*(c_bpc*(T_pc-Ti)+L+c_apc*(Tf-T_pc))
现在让我们举一些例子来演示上述函数的使用。
示例 1
一台发动机在 400 K 时接收 105 kJ 热量,并在 200 K 时向外排放 42 kJ 热量。检查该发动机是否可行。
解决方案
我们将使用函数 clausius_inequality() 来检查过程的有效性。
程序及其输出如下所示 -
代码 |
输出 |
|---|---|
from numpy import * Q=array([105,-42]) T=array([400,200]) clausius_inequality(Q,T) |
循环不可行 |
示例 2
1 千克 268 K 的冰通过从 298 K 的大气中吸收热量转化为 293 K 的水。冰和水的比热分别为 2.093 和 4.187 kJ/kg-K。如果冰的熔点为 273 K,则计算冰、周围环境和宇宙的熵变。同时,在 T-s 图中绘制该过程。(相变过程中的潜热为 333.3 kJ/kgK)
解决方案
from pylab import *
# Initial ice temp.
T1=268
# Phase change temp.
T2=273
# Final water temp.
T3=T0=293
# Specific heat of ice
ci=2.093
# specific heat of water
cw=4.187
# Latent heat during melting
L=333.3
# 1-2 Ice
Δs1=s_se(ci,T1,T2,m=1)
# 2-3 Phase Change
Δs2=s_pc(T2,L)
# 3-4 Water
Δs3=s_se(cw,T2,T3,m=1)
# Entropy change of system
Δs_ice=Δs1+Δs2+Δs3
# Heat transfer from the atmosphere
Q=q_mel_sol(1,T1,T3,T2,ci,cw,L)
# Entropy change of surrounding
Δs_atm=-Q/T0
# Entropy change of universe
Δs_uni=Δs_ice+Δs_atm
print("Δs_system = ",round(Δs_ice,4))
print("Δs_surr = ",round(Δs_atm,4))
print("Δs_uni = ",round(Δs_uni,4))
# Plotting T-S Diagram
plot_pc(Δs1,Δs2,Δs3,T1,T2,T3)
输出
程序输出将为 -
Process is either melting or vaporization Δs_system = 1.5556 Δs_surr = -1.4591 Δs_uni = 0.0965
它还将生成以下图表 -
结论
在本教程中,我们使用 Python 编程对热力学熵进行了建模,并对开发的函数进行了数值问题的实现和测试。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP