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.

Problem 1

Here is a sample problem that gives a fair indication of what is possible with freesteam. It involves direct evaluation from the IAPWS-IF97 correlation, then one-variable iterative solution, then two-variable iterative solution of the steam properties.

  1. calculate the density of steam at 5 bar and 200°C
  2. calculate the temperature if the steam from (1) is isentropically compressed to 10 bar.
  3. calculate the pressure and temperature of steam at the same specific volume as (2) but with the specific internal energy raised by 200 kJ/kg

Solution

The following code is included in the source distribution as example.cpp. See the main page instructions for how to build and run it.

#include "steamcalculator.h"
#include "solver.h"
#include "solver2.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
    try{
        SteamCalculator S1, S2, S3;

        // turn on display of units
        cerr.flags(ios_base::showbase);

        // initialise T1, p1, p2
        Temperature T1 = fromcelsius(200.0);
        Pressure p1 = 5.0 * bar;
        Pressure p2 = 10.0 * bar;

        // Part 1
        cerr << endl << "Part (1) -"
            "density at 10 bar, 200°C" << endl;
        cerr << "p1 = " << p1/bar << " bar" << endl;
        cerr << "T1 = " << T1;
        cerr << " (" << tocelsius(T1) << "°C)" << endl;
        S1.set_pT(p1, T1);
        Density rho1 = S1.dens();
        cerr << "rho1 = " << rho1 << endl;

        // Part 2
        cerr << endl << "Part (2)
            "- isentropic compression to 10 bar" << endl;
        SpecificEntropy s1 = S1.specentropy();
        cerr << "s1 = " << s1 << endl;
        cerr << "p2 = " << p2/bar << " bar" << endl;
        Solver<Pressure,SpecificEntropy,Temperature> PS(p2, s1);
        S2 = PS.solve(0.0001 * kJ_kgK, 0.0001 * Kelvin);
        Temperature T2 = S2.temp();
        cerr << "T2 = " << T2;
        cerr << " (" << tocelsius(T2) << "°C)" << endl;

        // part (3) - Finding p,T for v as above
        // and u increased by 200 kJ_kg
        cerr << endl << "Part (3) - Finding p,T for v as above"
            << "and u increased by 200 kJ_kg" << endl;
        SpecificVolume v2 = S2.specvol();
        SpecificEnergy u2 = S2.specienergy();
        cerr << "v2 = " << v2 << endl;
        cerr << "u2 = " << u2 << endl;

        SpecificEnergy u3 = u2 + 200.0 * kJ_kg;
        cerr << "u3 = " << u3 << endl;

        Solver2<SpecificEnergy,SpecificVolume> SUV;
        S3 = SUV.solve(u3,v2);
        Temperature T3 = S3.temp();
        Pressure p3 = S3.pres();
        cerr << "p3 = " << p3/bar << " bar" << endl;
        cerr << "T3 = " << T3;
        cerr << " (" << tocelsius(T3) << "°C)" << endl;

        exit(0);

    }catch(Exception *E){
        cerr << "Error: " << E->what();
        exit(1);
    }
}

Output:

$ ./example

Part (1) - density at 10 bar, 200°C
p1 = 5 bar
T1 = 473.15 K (200°C)
rho1 = 2.35275 kg/m3

Part (2) - isentropic compression to 10 bar
s1 = 7061.07 J/kgK
p2 = 10 bar
T2 = 556.418 K (283.268°C)

Part (3) - Finding p,T for v as above and u increased by 200 kJ_kg
v2 = 0.249638 m3/kg
u2 = 2.76613e+06 J/kg
u3 = 2.96613e+06 J/kg
p3 = 12.372 bar
T3 = 679.854 K (406.704°C)

Problem 2

Use freesteam's Python bindings to quickly calculate some steam properties. This requires version 0.7 or newer.

Solution

from freesteam import *

p = Measurement(1,"bar")
h = Measurement(2000,"kJ/kg")

S = steam_ph().solve(p,h)

print S.temp()

SourceForge.net LogoJohn Pye
Last modified: 24 June 2008