top of page

Educational Python Codes for Thermodynamics

I have dedicated over a decade to teaching thermodynamics, during which I developed Python codes tailored for educational purposes. These codes address two areas of undergraduate-level thermodynamics: the first focuses on the expansion and compression of ideal gases in a piston-cylinder system, while the second handles the computation of enthalpy, entropy, and fugacity for real fluids using the Peng-Robinson Equation.

01

Expansion of Gas in Piston-Cylinder System

images_large_ed-2015-00361w_0002.jpeg

The expansion and compression of an ideal gas in a piston-cylinder system is a classical example used to illustrate the principles of energy balance. However, most textbooks do not delve into the details of the frictional forces that occur between the piston and the cylinder wall during the process. These frictional forces are crucial for two reasons: they contribute to heat transfer between the gas and its surroundings, and more significantly, they result in irreversible entropy generation. The effects of friction can be quantified by applying energy as well as entropy balance to two system definitions—one that includes both the gas and the piston, and another that focuses solely on the confined gas. Below are three code examples that demonstrate this process in action.

isothermal

v1.5

This Python code simulates the isothermal expansion of 100 moles of an ideal gas at 298 K. The user inputs the mass of the piston and the friction coefficient between the piston and the cylinder wall. The initial volume of the gas is set to 1 m³, with a constant external pressure of 1 bar. The cylinder has a cross-sectional area of 0.01 m², and during expansion, the piston moves vertically under the influence of gravity. The code computes the thermodynamic properties of different parts of the system during the expansion.

adiabiatic_outside

v1.5

This Python code simulates the adiabatic expansion of 100 moles of an ideal gas with a fixed heat capacity at constant volume (Cv) of 1.5R. The user inputs the mass of the piston and the friction coefficient between the piston and the cylinder wall. The initial volume and temperature of the gas are set to 1 m³ and 298 K, respectively, with a constant external pressure of 1 bar. The cylinder has a cross-sectional area of 0.01 m², and during expansion, the piston moves vertically under the influence of gravity. The system defines the adiabatic region as including both the gas and the piston, meaning that the heat generated by friction cannot cross the boundary into the surrounding.

adiabatic_inside

v1.5

This Python code simulates the adiabatic expansion of 100 moles of an ideal gas, similar to the previous case. The key difference is that the system defines the adiabatic region as excluding the piston. This means that any heat generated by friction must be transferred to the surrounding, rather than remaining within the system.

02

Enthalpy and Entropy of Real Fluids based on Peng-Robinson Equation

The computation of enthalpy, entropy, and fugacity for real fluids using the Peng-Robinson equation can be tedious and often challenging for students. These Python codes simplify the process by performing the calculations based on user-input properties. The results are presented graphically in a PV diagram, offering a clear visual representation of the fluid behavior.

Figure_2-new.png

ideal_gas

v2.1

This Python code calculates the enthalpy and entropy of an ideal gas. The user is required to input four coefficients for the polynomial expression of the constant-pressure heat capacity (Cp), which characterizes the type of ideal gas. Additionally, the user must specify the reference temperature and pressure. The final inputs are the target temperature and pressure, for which the code will compute the enthalpy and entropy values.

real_fluid

v1.6

This Python code calculates the departure enthalpy and departure entropy of a real fluid. The user is required to input the critical pressure, critical temperature, and the acentric factor, which define the type of fluid to be considered. Next, the user inputs the target temperature and pressure. The code computes the departure enthalpy and entropy values, as well as the fugacity coefficients for both the liquid and vapor states. By comparing these fugacity values, the user can determine the stable phase of the fluid under the given pressure and temperature conditions.

vap_liq_equilibrium

v1.6

This Python code calculates the vapor-liquid equilibrium (VLE) pressure of a real fluid at a specified temperature. The user is required to input the critical pressure, critical temperature, and the acentric factor, which define the fluid to be considered. Then, the user inputs the target temperature for the VLE calculation. The code computes the VLE pressure and the corresponding volumes of the liquid and vapor phases.

Acknowledgment

I express my gratitude to Prof. Shiang-Tai Lin at National Taiwan University for his valuable discussions on these educational thermodynamics problems during my early career, which played a key role in the development of these codes.

Share this webpage if you find it useful.

bottom of page