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 the Carnot Cycle in Python
The Carnot cycle is the most fundamental gas power cycle that serves as a benchmark for all engine cycles. Every engine cycle efficiency is validated against the Carnot cycle's theoretical maximum efficiency. It comprises four processes: two reversible adiabatic processes and two isothermal processes.
Heat rejection and addition occur isothermally in processes 1-2 and 3-4, while processes 4-1 and 2-3 are reversible adiabatic processes involving work interactions.
Input Parameters
The Carnot cycle modeling requires five key parameters:
- Maximum pressure (pmax) highest pressure in the cycle
- Minimum pressure (pmin) lowest pressure in the cycle
- Maximum volume (Vmax) largest volume in the cycle
- Compression ratio (r) ratio of maximum to minimum volume
- Adiabatic exponent (?) specific heat ratio
Process 1-2: Isothermal Expansion
Process 1-2 is an isothermal process where heat is added at constant temperature. The initial conditions are:
p? = pmin and v? = vmax
Using the compression ratio (r), volume at point 2 is calculated as:
v? = v?/r
The isothermal constant c? is:
c? = p? × v?
Pressure variation along line 1-2 follows:
p = c?/v
Process 2-3: Adiabatic Compression
Process 2-3 is a reversible adiabatic compression where p? = pmax. The adiabatic constant c? is calculated using continuity at point 2:
c? = c? × v?^(?-1)
Volume at point 3 is determined by:
v? = (c?/p?)^(1/?)
Process 3-4: Isothermal Compression
The isothermal constant c? for process 3-4 is:
c? = p? × v?
To find v?, we need the adiabatic constant c? for process 4-1:
c? = p? × v?^?
Therefore: v? = (c?/c?)^(1/(?-1))
Python Implementation
Here's the complete Python function to model and visualize the Carnot cycle ?
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
def carnot_cycle(p_min, p_max, v_max, r, gamma):
"""
Model and visualize the Carnot cycle
Parameters:
p_min: minimum pressure (Pa)
p_max: maximum pressure (Pa)
v_max: maximum volume (m³)
r: compression ratio
gamma: adiabatic exponent
"""
# State point 1
p1 = p_min
v1 = v_max
v2 = v1 / r
c1 = p1 * v1
# Process 1-2 (Isothermal expansion)
v_12 = np.linspace(v2, v1, 100)
p_12 = c1 / v_12
p2 = c1 / v2
# Process 2-3 (Adiabatic compression)
p3 = p_max
c2 = c1 * v2**(gamma - 1)
v3 = (c2 / p3)**(1/gamma)
v_23 = np.linspace(v3, v2, 100)
p_23 = c2 / v_23**gamma
# Process 3-4 (Isothermal compression)
c3 = p3 * v3
c4 = p1 * v1**gamma
v4 = (c4 / c3)**(1/(gamma - 1))
v_34 = np.linspace(v3, v4, 100)
p_34 = c3 / v_34
p4 = c3 / v4
# Process 4-1 (Adiabatic expansion)
v_41 = np.linspace(v4, v1, 100)
p_41 = c4 / v_41**gamma
# Create the plot
plt.figure(figsize=(8, 6))
plt.plot(v_12, p_12/1000, 'r-', linewidth=3, label='1-2 Isothermal')
plt.plot(v_23, p_23/1000, 'b-', linewidth=3, label='2-3 Adiabatic')
plt.plot(v_34, p_34/1000, 'g-', linewidth=3, label='3-4 Isothermal')
plt.plot(v_41, p_41/1000, 'c-', linewidth=3, label='4-1 Adiabatic')
# Mark state points
plt.plot([v1, v2, v3, v4], [p1/1000, p2/1000, p3/1000, p4/1000], 'ko', markersize=8)
# Labels and formatting
plt.xlabel('Volume (m³)')
plt.ylabel('Pressure (kPa)')
plt.title('Carnot Cycle P-V Diagram')
plt.grid(True, linestyle='--', alpha=0.7)
plt.legend()
# Annotate state points
plt.annotate('1', (v1, p1/1000), xytext=(5, 5), textcoords='offset points')
plt.annotate('2', (v2, p2/1000), xytext=(5, 5), textcoords='offset points')
plt.annotate('3', (v3, p3/1000), xytext=(5, 5), textcoords='offset points')
plt.annotate('4', (v4, p4/1000), xytext=(5, 5), textcoords='offset points')
plt.tight_layout()
plt.show()
# Return state data
data = {
'State': [1, 2, 3, 4],
'Pressure (Pa)': [p1, p2, p3, p4],
'Volume (m³)': [v1, v2, v3, v4]
}
return pd.DataFrame(data).set_index('State')
# Example usage
result = carnot_cycle(p_min=2e5, p_max=20e5, v_max=0.5, r=5, gamma=1.4)
print(result)
Example Results
For the given parameters (pmin = 2×10? Pa, pmax = 20×10? Pa, vmax = 0.5 m³, r = 5, ? = 1.4), the state points are ?
| State | Pressure (Pa) | Volume (m³) |
|---|---|---|
| 1 | 200,000 | 0.500 |
| 2 | 1,000,000 | 0.100 |
| 3 | 2,000,000 | 0.061 |
| 4 | 400,000 | 0.305 |
Conclusion
This tutorial demonstrates how to model the Carnot cycle using Python with mathematical equations for each process. The implementation creates both numerical data and visual representation of the theoretical cycle that serves as the efficiency benchmark for all heat engines.
