Thermodynamics and Python Packages

Erfan Hamdi
4 min readNov 3, 2021

--

As a Mechanical Engineering Student interested in the Open Source world, it won’t be easy to come across many Free and Open Source software for doing your projects. One very popular software among ME students and frequently referred to in Thermodynamics Textbooks is Engineering Equation Solver aka. EES. Although very Easy to Use, it isn’t free. So here we will find an open Source and free substitute for that which its’ usage only requires a basic knowledge of Python! so let’s go!

CoolProp

According to its Documentation, CoolProp is a C++ Library that implements many great features such as

And many more that you can find in the documentation. And It is also supported by many different types of Programming Languages like Python (2.x, 3.x), MATLAB, C#, etc.
Here we are going to Analyse a Gas Turbine Cycle using this Package so that you can learn to use it the way you want!

Installation

First I recommend you to make a virtual environment and install all of the packages required by the project in that virtual environment. this can be done either by venv package or virtualenv package.

you can install virtualenv package using pip

pip install virtualenv

Then you can create your virtual environment like the following, I have named it ThermoEnv:

virtualenv ThermoEnv

This creates a folder in your current directory named as

ThermoEnv

then you need to activate the Virtual Environment that you just made.

source ThermoEnv/bin/activate

Now you can see the name of the virtual environment that you are using at the beginning of the line in the Command Prompt

To Install CoolProp Package you can use pip, use pip3 instead if you are running it on a Linux device.

pip install CoolProp

as you are using a new virtual environment, you may need to install other packages such as numpy and matplotlib

pip install numpy matplotlib

Thermodynamic Cycle

As a good starting point, we can try to analyze a turbine cycle. consider this example which is the Example 9.5 of Fundamentals of Thermodynamics 8th Edition by Borgenakke and Sonntag.

A steam power plant operates on a cycle with pressures and temperatures as designated in Fig. The efficiency of the turbine is 86%, and the efficiency of the pump is 80%. Determine the thermal efficiency of this cycle.

A Thermodynamic Cycle, Fundamentals of Thermodynamics by Borgnakke and Sonntag, 8th ed. chapter 9

First of all, we have to define the material for which we are calculating the thermodynamic properties. In this example, it is stated that the material is Steam. We can find a list of fluids supported by default in the Package, but you can again define your fluid if it wasn’t included in this list. so here we define a variable called fluid as water we could have as well used the chemical formula H2O

Here I have also defined the parameters like Pressure, Temperature, Enthalpy, and Entropy as a dictionary to access them wherever needed more quickly. As you can see here, I have converted the pressure units from MPa to Pa, which is how this Package expects to get them. And I have entered Temperature in Centigrades but remember to convert it to Kelvin whenever you are passing it as an input to a function of the Package.
Then we will get the enthalpy of points 4 and 5. as you know, we need two independent variables to calculate the value of a thermodynamic property using tables. Here using the CoolProp, you should keep that in mind too. The syntax to get any parameter that you need is like this:

Table of string inputs to PropsSI function and some other information like what the units should be can be found in this Table.

If we want to find the parameter A based on two independent parameters B1 and B2, for the material F, we should write this:

cp.PropsSI('A', 'B1', <B1 Value>, 'B2', <B2 Value>, 'F')

Here I have calculated the Enthalpy and Entropy of point 5 in the cycle using the Pressure and Temperature of that point.
Our choices are not limited to Pressure and Temperature. You can put whatever you want to get the results that you need. For example, here, we needed to calculate the saturation value of Entropy and Enthalpy. In that case, we can use the quality parameter Q as our input. In the case of Q being 0, our fluid is saturated, and every parameter on the saturation line needs one other parameter like Pressure or Temperature.

As you can see, we have set the isentropic Entropy of point 6 equal to the Entropy of point 5 just like that. Using all that information, we can calculate the isentropic quality at point 6.

After calculating other needed properties and using the efficiency parameters, we can calculate the total thermal efficiency of the cycle.

I highly recommend you check Ali’s great post where he have introduced many great packages that a mechanical engineering student can find them useful!

--

--

Erfan Hamdi

A Mechanical Engineering Student who is Interested in OpenSource World!