Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Modelling Thermodynamic Entropy in Python
Thermodynamic entropy is a fundamental property that measures the degree of randomness or disorder in a system. In Python, we can model entropy changes for various thermodynamic processes using mathematical formulations and create visualization tools.
Understanding Entropy
Entropy remains constant during a reversible adiabatic process. When a system exchanges dQ heat with its surroundings at temperature T, the entropy change is:
ds = dQ/T ... (1)
According to Clausius' inequality, the cyclic integral along any path satisfies:
?(dQ/T) ? 0 ... (2)
The equality holds for reversible processes, while inequality holds for irreversible cycles.
Entropy Change Formulations
For different processes, entropy changes are calculated differently:
Sensible heat transfer: ?S = mcp ln(T2/T1) ... (3)
Phase change: ?S = mL/T ... (4)
Gas state change: ?s = cp ln(T2/T1) - R ln(p2/p1) ... (6)
Python Functions for Entropy Calculations
Basic Entropy Functions
import numpy as np
from math import log, sqrt
import matplotlib.pyplot as plt
# Entropy change during phase change
def s_pc(T, L):
"""Calculate entropy change during phase change"""
return L / T
# Entropy change during sensible energy transfer
def s_se(c, T1, T2, m=1):
"""Calculate entropy change for temperature change"""
return m * c * log(T2 / T1)
# Entropy change for gas process
def s_process(T1, T2, p1, p2, R, cp):
"""Calculate entropy change for gas with pressure and temperature change"""
return cp * log(T2 / T1) - R * log(p2 / p1)
print("Entropy functions loaded successfully")
Entropy functions loaded successfully
Clausius Inequality Check
def clausius_inequality(Q, T):
"""Check if a thermodynamic cycle is possible using Clausius inequality"""
Sm = sum(Q / T)
if Sm < 0:
return "The cycle is irreversible and possible"
elif Sm == 0:
return "The cycle is reversible and possible"
else:
return "The cycle is not possible"
# Example: Engine receives 105 kJ at 400 K and rejects 42 kJ at 200 K
Q = np.array([105, -42])
T = np.array([400, 200])
result = clausius_inequality(Q, T)
print(result)
The cycle is not possible
Maximum Work Calculations
def max_work_fb(T1, T2, c):
"""Calculate maximum work from two finite bodies"""
Tf = sqrt(T1 * T2)
work = c * (sqrt(T1) - sqrt(T2))**2
return work, Tf
def max_work_fb_TER(T, T0, c):
"""Calculate maximum work from body with thermal reservoir"""
return c * ((T - T0) - T0 * log(T / T0))
# Example calculation
T1, T2, c = 400, 300, 10 # Temperatures in K, heat capacity in kJ/K
work, equilibrium_temp = max_work_fb(T1, T2, c)
print(f"Maximum work: {work:.2f} kJ")
print(f"Equilibrium temperature: {equilibrium_temp:.2f} K")
Maximum work: 28.58 kJ Equilibrium temperature: 346.41 K
Complete Example: Ice Melting Process
def q_mel_sol(m, Ti, Tf, T_pc, c_bpc, c_apc, L):
"""Calculate heat transfer during phase change process"""
if Ti > Tf:
process_type = "freezing or condensation"
return m * (c_bpc * (T_pc - Ti) - L + c_apc * (Tf - T_pc)), process_type
else:
process_type = "melting or vaporization"
return m * (c_bpc * (T_pc - Ti) + L + c_apc * (Tf - T_pc)), process_type
# Ice melting example: 1 kg ice at 268 K to water at 293 K
T1 = 268 # Initial ice temperature (K)
T2 = 273 # Melting point (K)
T3 = 293 # Final water temperature (K)
T0 = 298 # Atmosphere temperature (K)
ci = 2.093 # Specific heat of ice (kJ/kg-K)
cw = 4.187 # Specific heat of water (kJ/kg-K)
L = 333.3 # Latent heat of melting (kJ/kg)
# Calculate entropy changes for each stage
delta_s1 = s_se(ci, T1, T2, m=1) # Ice heating
delta_s2 = s_pc(T2, L) # Phase change
delta_s3 = s_se(cw, T2, T3, m=1) # Water heating
# Total entropy change of system
delta_s_ice = delta_s1 + delta_s2 + delta_s3
# Heat transfer calculation
Q, process_type = q_mel_sol(1, T1, T3, T2, ci, cw, L)
# Entropy change of surroundings
delta_s_atm = -Q / T0
# Entropy change of universe
delta_s_uni = delta_s_ice + delta_s_atm
print(f"Process: {process_type}")
print(f"?S_system = {delta_s_ice:.4f} kJ/K")
print(f"?S_surroundings = {delta_s_atm:.4f} kJ/K")
print(f"?S_universe = {delta_s_uni:.4f} kJ/K")
Process: melting or vaporization ?S_system = 1.5556 kJ/K ?S_surroundings = -1.4591 kJ/K ?S_universe = 0.0965 kJ/K
Key Applications
The entropy modeling functions can be used for:
- Cycle analysis: Checking feasibility using Clausius inequality
- Phase change processes: Calculating entropy during melting, boiling
- Heat engine optimization: Finding maximum work output
- Process irreversibility: Quantifying entropy generation
Conclusion
Python provides powerful tools for modeling thermodynamic entropy through mathematical functions and numerical calculations. These models help analyze process feasibility, calculate maximum work, and understand irreversibility in thermodynamic systems.
