Example code

Examples

We propose to add more examples here when time permits. If you have any examples from your own work, I'll put them here and link to your site.

Example 1

This example demonstrates use of a few of the basic capabilities of freesteam via C code.

Calculate the rise in temperature seen in isentropic compression of a sample of steam initially at 1 bar, 400 K, to a final pressure of 10 bar. Next, calculate the saturation temperature at the final pressure.

Solution

The following code is included in the source distribution as examples/isentropic.c. See the examples/README.txt files for instructions on how to build and run it.

isentropic.c
/*
This is a simple example of a C program that calculates using steam properties
from freesteam.

It demonstrates use of a few of the basic capabilities of freesteam. It
calculates the rise in temperature seen in isentropic compression of a sample
of steam initially at 1 bar, 400 K, to a final pressure of 10 bar. It also
calculates the saturation temperature for steam at that final pressure.
*/

#include <freesteam/steam_ps.h>
#include <freesteam/steam_pT.h>
#include <freesteam/region4.h>
#include <stdio.h>

int main(void){

	fprintf(stderr,"\nThis example demonstrates use of a few of the basic"
		" capabilities of freesteam. It calculates the rise in temperature"
		" seen in isentropic compression of a sample of steam initially at"
		" 1 bar, 400 K, to a final pressure of 10 bar. It also calculates"
		" the saturation temperature for steam at that final pressure.\n\n"
	);

	double T = 400.; /* in Kelvin! */
	double p = 1e5; /* = 1 bar */

	fprintf(stderr,"Initial temperature = %f K, pressure = %f bar\n", T, p/1e5);

	/* set a steam state of 1 bar, 400 K */
	SteamState S = freesteam_set_pT(1e5, 400);

	double s = freesteam_s(S);
	fprintf(stderr,"Entropy at initial state is %f kJ/kgK\n",s/1e3);

	/* calculate a steam state with entropy from above and 10 bar pressure */
	SteamState S2 = freesteam_set_ps(10e5, s);

	double T2 = freesteam_T(S2);
	double p2 = freesteam_p(S2);

	/* output the new temperature */
	fprintf(stderr,"New temperature is %f K at %f bar\n", T2, p2/1e5);

	fprintf(stderr,"Check: final entropy is %f kJ/kgK\n", freesteam_s(S2)/1e3);

	/* saturation temperature at final pressure */
	double Tsat = freesteam_region4_Tsat_p(p2);
	fprintf(stderr,"Saturation temperature at %f bar is %f K.\n\n",p2/1e5, Tsat);

	return 0;
}

Output:

This example demonstrates use of a few of the basic capabilities of freesteam.
It calculates the rise in temperature seen in isentropic compression of a sample
of steam initially at 1 bar, 400 K, to a final pressure of 10 bar. It also
calculates the saturation temperature for steam at that final pressure.

Initial temperature = 400.000000 K, pressure = 1.000000 bar
Entropy at initial state is 7.502401 kJ/kgK
New temperature is 684.505123 K at 10.000000 bar
Check: final entropy is 7.502401 kJ/kgK
Saturation temperature at 10.000000 bar is 453.035632 K.

Example 2

Use freesteam's Python bindings to quickly calculate some steam properties. The Python API has changed extensively with the release of freesteam 2.0, so you need to use that version for this to work.

Solution

from freesteam import *

# Calculation of properties for given (p,h)
p = 1e5
h = 2000e3

S = steam_ph(p,h)

print S.T
print S.rho
print S.s

# Calculation of properties for given (p,T)
S2 = steam_pT(p, 400)

print S2.region
print S2.h

# Calculation of saturation properties at given temperature, quality
S3 = steam_Tx(400, 0.5)
print S3.h
print S3.region

# Calculation of saturated liquid properties at given pressure
Tsat = Tsat_p(p)
print Tsat
S3 = steam_Tx(Tsat, 0)
print S3.h

Example 3

This example demonstrates how the new mplot3d features in Matplotlib can be used to plot properties surfaces using data from freesteam. This example plots a (T,p,s) surface.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Example of 3D (T,s,p) surface plotted using new Matplotlib 3D-plotting feature.
# Contributed by Rod Stephenson. Thanks!

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import freesteam

def plot_tsp3d():
    fig = plt.figure()
    ax = Axes3D(fig)
    pp = np.logspace(-3,3,20)
    ss = np.linspace(0.,10., 40)
    xdata,ydata = np.meshgrid(pp, ss)
    zdata = np.zeros(xdata.shape)

    for i, p in enumerate(pp):
        for j, s in enumerate(ss):
            T = None
            if not freesteam.bounds_ps(p*1e5,s*1e3,0):
                try:
                    T = freesteam.steam_ps(p*1e5,s*1e3).T
                except:
                    pass
            zdata[j, i]= T
    ax.plot_wireframe(xdata, ydata, zdata, rstride=1, cstride=1)
    ax.set_xlabel('Pressure / [bar]')
    ax.set_ylabel('Entropy / [kJ/kgK]')
    ax.set_zlabel('Temperature / [K]')

    TT0 = np.linspace(273.15, freesteam.TCRIT, 100)
    psat = [freesteam.psat_T(T)/1e5 for T in TT0]
    sf = [freesteam.region4_Tx(T,0).s/1e3 for T in TT0]
    sg = [freesteam.region4_Tx(T,1).s/1e3 for T in TT0]
    ax.plot(psat, sf, TT0,'k-')
    ax.plot(psat, sg, TT0,'r-')
    plt.show()

if __name__ == '__main__':
    plot_tsp3d()

The resulting plot can be rotated in 3D using the mouse. A screenshot is shown below.


SourceForge.net LogoJohn Pye
Last modified: 25 February 2013