
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Modelling Thermodynamic Entropy in Python
Entropy is a property of a thermodynamic system that remains constant during a reversible adiabatic process. Moreover, we can also say that it is the degree of randomness or disorder in the system. If a system exchanges dQ heat from its surroundings at temperature T, then the chance in the entropy can be written as
$$\mathrm{ds \: = \: \frac{dQ}{T} \dotso \dotso \: (1)}$$
According to Clausius' inequality the cyclic integral of $\mathrm{\frac{dQ}{T}}$ along a reversible path is either less or equal to zero. Mathematically, it can be written as
$$\mathrm{\oint\frac{dQ}{T} \: \leq \: 0\dotso \dotso \: (2)}$$
The equality holds for a reversible and inequality holds for irreversible cycle. Any engine cycle, which does not follow Eq. 2, is not possible.
For different types of processes, the entropy change is evaluated differently. During sensible energy interaction as there is no phase change and only temperature changes then the change entropy from state 1 to state 2 can be written as
$$\mathrm{\triangle S \: = \: mc_{p} \: In \: (\frac{T_{2}}{T_{1}})\dotso \dotso \: (3)}$$
where, $\mathrm{c_{p}}$ is the specific heat at constant pressure. Whereas if there is a phase change then the temperature does not change so the entropy is simply evaluated as latent heat divided by the phase change temperature as follows
$$\mathrm{\triangle S \: = \: \frac{mL}{T}\dotso \dotso \: (4)}$$
where, L is the specific latent heat. The entropy mentioned in Eq. 3, and 4 are total entropy i.e. having unit kJ/K but in most of the cases we deals with specific properties so the specific entropy (kJ/kg-K) is written by small s and defined as
$$\mathrm{s \: = \: \frac{ds}{dm}\dotso \dotso \: (5)}$$
During a process if a gas has changed its state from pressure and temperature $\mathrm{p_{1} \: , \: T_{1}}$ to $\mathrm{p_{2} \: , \: T_{2}}$ then the change in specific entropy can be written as
$$\mathrm{\triangle s \: = \: c_{p} \: In \: (\frac{T_{2}}{T_{1}}) \: \: R \: In \: (\frac{p_{2}}{p_{1}})\dotso \dotso \: (6)}$$
There are many situation in which the use of entropy can solve the problem very easily. These special cases are
-
When two identical systems at temperatures $\mathrm{T_{1}}$ and $\mathrm{T_{2}}$ are connected by a reversible engine then the maximum work obtained from these finite bodies and their final equilibrium temperature will be given by
$$\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)}$$
where, C is the heat capacity of the systems which is the product of mass and specific heat.
-
During mixing of two fluid of same heat capacity (C) and different temperatures $\mathrm{T_{1} \: and \: T_{2}}$, the entropy change of the whole system will be given by
$$\mathrm{\triangle S \: = \: C \: In \:(\frac{(T_{1} \: + \: T_{2})/2}{\sqrt{T_{1}T_{2}}})\dotso \dotso \: (9)}$$
-
The maximum work obtainable from a system (at temperature T) and a thermal energy reservoir (at temperature $\mathrm{T_{0}}$) in communication via a reversible engine is
$$\mathrm{W_{max} \: = \: C \:((T_ \: \: T_{0}) \: \: T_{0} \: In \: (\frac{T}{T_{0}})\dotso \dotso \: (10)}$$
Python Program for Modelling Thermodynamic Entropy
Following functions are written in Python to perform the entropy calculations for different cases
Entropy change During Phase Change
def s_pc(T,L): return L/T
Entropy change during Sensible Energy Transfer
def s_se(c,T1,T2,m=1): return m*c*log(T2/T1)
Modelling Clausius' Inequality
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")
Maximum work obtainable from bodies of finite heat capacity
def max_work_fb(T1,T2,c): Tf=sqrt(T1*T2) work=c*(sqrt(T1)-sqrt(T2))**2 return work,Tf
Maximum work obtained from a finite heat capacity body interacting with thermal energy reservoir
def max_work_fb_TER(T,T0,c): return c*((T-T0)-T0*log(T/T0))
Entropy change of a gas during a process with change in pressure and temperature given.
def s_process(T1,T2,p1,p2,R,cp): return cp*log(T2/T1)-R*log(p2/p1)
Function to plot T-S diagram of a phase change process (liquid to vapour)
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()
One more function will be of great use apart from these is the amount of heat interaction during a process (sensible and latent heat both involved)
#----------------------------------------------- # 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))
Now let us take some problems to demonstrate the use of above functions.
Example 1
An engine receives 105 kJ at 400 K and rejects 42 kJ to 200 K. Check that whether the engine is possible or not.
Solution
We will be using function clausius_inequality() to check the validity of the process.
The program and output of the program are as follows
Code |
Output |
---|---|
from numpy import * Q=array([105,-42]) T=array([400,200]) clausius_inequality(Q,T) |
The cycle is not possible |
Example 2
One kg of Ice at 268 K is converted to water at 293 K by gaining heat from atmosphere at 298 K. The specific heat of ice and water are 2.093 and 4.187 kJ/kg-K. If the melting point of ice is 273 K then evaluate the entropy change of ice, surroundings and the universe. Also, plot the process in T-s diagram. (Latent heat during phase change 333.3 kJ/kgK)
Solution
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)
Output
The program output will be
Process is either melting or vaporization ?s_system = 1.5556 ?s_surr = -1.4591 ?s_uni = 0.0965
It will also produce the following plot

Conclusion
In this tutorial, thermodynamics entropy has been modelled using Python programming and the developed functions have been implemented and tested on numerical problems.