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 Otto and Diesel Cycles in Python
The Otto cycle and Diesel cycle are fundamental thermodynamic cycles used in internal combustion engines. Python provides powerful tools for modeling these cycles using mathematical equations and visualization libraries like matplotlib and pandas.
Otto Cycle
An air standard cycle called the Otto Cycle is employed in spark ignition (SI) engines. It comprises of two reversible adiabatic processes and two isochoric processes (constant volume), totaling four processes. When the work interactions take place in reversible adiabatic processes, the heat addition (2-3) and rejection (4-1) occur isochorically (3-4 and 1-2).
To model the cycle in Python, the input variables considered are maximum pressure, minimum pressure, maximum volume, compression ratio (r), and adiabatic exponent (?).
Thermodynamic Processes
Process 1-2 (Adiabatic Compression): Starting from minimum pressure and maximum volume, the gas is compressed adiabatically following the relation p?v?^? = constant.
Process 2-3 (Isochoric Heat Addition): Heat is added at constant volume, increasing pressure from p? to p?.
Process 3-4 (Adiabatic Expansion): The gas expands adiabatically, doing work and decreasing in pressure.
Process 4-1 (Isochoric Heat Rejection): Heat is rejected at constant volume, returning to the initial state.
Python Implementation of Otto Cycle
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def otto_cycle(p_min, p_max, v_max, r, gamma):
"""
Model the Otto cycle
Parameters:
p_min: minimum pressure (Pa)
p_max: maximum pressure (Pa)
v_max: maximum volume (m³)
r: compression ratio
gamma: adiabatic exponent
"""
# Process 1-2 (Adiabatic compression)
p1 = p_min
v1 = v_max
v2 = v1 / r
c1 = p1 * v1**gamma
p2 = c1 / v2**gamma
# Process 2-3 (Isochoric heat addition)
p3 = p_max
v3 = v2
# Process 3-4 (Adiabatic expansion)
c2 = p3 * v3**gamma
v4 = v1
p4 = c2 / v4**gamma
# Generate curves for plotting
plt.figure(figsize=(10, 8))
# Process 1-2
v_12 = np.linspace(v2, v1, 100)
p_12 = c1 / v_12**gamma
plt.plot(v_12, p_12/1000, 'b-', linewidth=3, label='1-2 Compression')
# Process 2-3
v_23 = np.full(100, v3)
p_23 = np.linspace(p2, p3, 100)
plt.plot(v_23, p_23/1000, 'r-', linewidth=3, label='2-3 Heat Addition')
# Process 3-4
v_34 = np.linspace(v3, v4, 100)
p_34 = c2 / v_34**gamma
plt.plot(v_34, p_34/1000, 'g-', linewidth=3, label='3-4 Expansion')
# Process 4-1
v_41 = np.full(100, v1)
p_41 = np.linspace(p4, p1, 100)
plt.plot(v_41, p_41/1000, 'm-', linewidth=3, label='4-1 Heat Rejection')
# Add state point labels
plt.plot(v1, p1/1000, 'ko', markersize=8)
plt.plot(v2, p2/1000, 'ko', markersize=8)
plt.plot(v3, p3/1000, 'ko', markersize=8)
plt.plot(v4, p4/1000, 'ko', markersize=8)
plt.text(v1+0.01, p1/1000-30, '1', fontsize=12)
plt.text(v2-0.01, p2/1000+50, '2', fontsize=12)
plt.text(v3+0.01, p3/1000-20, '3', fontsize=12)
plt.text(v4-0.02, p4/1000+10, '4', fontsize=12)
plt.title('Otto Cycle', fontsize=16)
plt.xlabel('Volume (m³)', fontsize=14)
plt.ylabel('Pressure (kPa)', fontsize=14)
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
# Create results dataframe
data = {
'Pressure (Pa)': [p1, p2, p3, p4],
'Volume (m³)': [v1, v2, v3, v4],
'Process': ['Start', 'After Compression', 'After Heat Addition', 'After Expansion']
}
df = pd.DataFrame(data, index=[1, 2, 3, 4])
df.index.name = 'State'
return df
# Example usage
otto_results = otto_cycle(p_min=2e5, p_max=35e5, v_max=0.5, r=5, gamma=1.4)
print(otto_results)
Diesel Cycle
An air standard cycle used in compression ignition (CI) engines is the diesel cycle. Four processes make up the cycle ? two reversible adiabatic, one isobaric (constant pressure), and one isochoric (constant volume). Heat addition happens in process 2-3 at constant pressure, whereas heat rejection happens in process 4-1 at constant volume.
Python Implementation of Diesel Cycle
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def diesel_cycle(p_min, p_max, v_max, r_c, gamma):
"""
Model the Diesel cycle
Parameters:
p_min: minimum pressure (Pa)
p_max: maximum pressure (Pa)
v_max: maximum volume (m³)
r_c: cut-off ratio
gamma: adiabatic exponent
"""
# Process 1-2 (Adiabatic compression)
p1 = p_min
v1 = v_max
p2 = p_max
v2 = v1 * (p1/p2)**(1/gamma)
c1 = p1 * v1**gamma
# Process 2-3 (Isobaric heat addition)
p3 = p2
v3 = r_c * v2
# Process 3-4 (Adiabatic expansion)
c2 = p3 * v3**gamma
v4 = v1
p4 = c2 / v4**gamma
# Generate curves for plotting
plt.figure(figsize=(10, 8))
# Process 1-2
v_12 = np.linspace(v2, v1, 100)
p_12 = c1 / v_12**gamma
plt.plot(v_12, p_12/1000, 'b-', linewidth=3, label='1-2 Compression')
# Process 2-3
v_23 = np.linspace(v2, v3, 100)
p_23 = np.full(100, p2)
plt.plot(v_23, p_23/1000, 'r-', linewidth=3, label='2-3 Heat Addition')
# Process 3-4
v_34 = np.linspace(v3, v4, 100)
p_34 = c2 / v_34**gamma
plt.plot(v_34, p_34/1000, 'g-', linewidth=3, label='3-4 Expansion')
# Process 4-1
v_41 = np.full(100, v4)
p_41 = np.linspace(p4, p1, 100)
plt.plot(v_41, p_41/1000, 'm-', linewidth=3, label='4-1 Heat Rejection')
# Add state point labels
plt.plot(v1, p1/1000, 'ko', markersize=8)
plt.plot(v2, p2/1000, 'ko', markersize=8)
plt.plot(v3, p3/1000, 'ko', markersize=8)
plt.plot(v4, p4/1000, 'ko', markersize=8)
plt.text(v1+0.01, p1/1 