HOWTO manipulate Streams

An interesting thing that you can do with a two-phase stream such as StreamIdealLiquidVapor is the flash calculation.

Start from the Hello world tutorial, and perform these steps:

  1. in the #include section, include the header file streams to register all stream types with the model factory:

     #include <libpf/streams/streams.h>
    

    and the NodeFactory because we’ll need the object factory:

     #include <libpf/persistency/NodeFactory.h>
    
  2. in the main function, remove the “Hello, world !” statement and replace it as follows:

    • define water and nitrogen as components:

        components.addcomp(new purecomps::water);
        components.addcomp(new purecomps::N2);
      
    • instantiate an object of type StreamIdealLiquidVapor using the object factory:

        NodeFactory nodeFactory;
        Stream *feed = my_cast<Stream *>(nodeFactory.create("StreamIdealLiquidVapor", Libpf::User::Defaults("test")), CURRENT_FUNCTION);
      
    • add the actual stream manipulation:

        feed->Tphase->Q("x[0]").set(0.99);  // water
        feed->Tphase->Q("x[1]").set(0.01);  // nitrogen
        feed->P.set(1.0, "atm");
        feed->flashoption = "PA";
      
        feed->Q("Vphase.fraction").set(Zero);
        feed->resetErrors();
        feed->calculate();
        Value HBP(feed->Tphase->Q("H"));
        diagnostic(0, "Bubble T = " << feed->T << " alfa = " << feed->Vphase->fraction << " errori = " << feed->errors.size())
      
        feed->Q("Vphase.fraction").set(One);
        feed->resetErrors();
        feed->calculate();
        Value HDP(feed->Tphase->Q("H"));
        diagnostic(0, "Dew T = " << feed->T << " alfa = " << feed->Vphase->fraction << " errori = " << feed->errors.size())
      
        for (Value H(HDP); H > HBP; H -= (HDP - HBP)/10.0) {
          feed->Tphase->Q("H").set(H);
          feed->resetErrors();
          feed->flashoption = "PH";
          feed->calculate();
          diagnostic(0, "T = " << feed->T << " alfa = " << feed->Vphase->fraction << " errori = " << feed->errors.size())
        }
      

This is the expected output:

* ****************** LIBPF 01.00.2350 [2016/04/12 11:39:44] ******************
* (C) Copyright 2004-2016 Paolo Greppi simevo s.r.l.
* ****************************************************************************
* All rights reserved; do not distribute without permission.
* ****************************************************************************

main * Bubble T = 116.820195854 K alfa = 0  errori = 0
main * Dew T = 372.886233827 K alfa = 1  errori = 0
main * T = 372.886233827 K alfa = 0.999999999979  errori = 0
Model::reportError **** ===== Logging error exceeded maximum iterations limit in test
Model::reportError **** ===== Logging error Flash convergence failure in stream::calculate; in test
main * T = 373.189533152 K alfa = 0.0636330272048  errori = 2
Model::reportError **** ===== Logging error exceeded maximum iterations limit in test
Model::reportError **** ===== Logging error Flash convergence failure in stream::calculate; in test
main * T = 371.157572589 K alfa = 0.047073818361  errori = 2
main * T = 372.684062031 K alfa = 0.583834979816  errori = 0
main * T = 372.53206077 K alfa = 0.445249019609  errori = 0
main * T = 372.241451471 K alfa = 0.306885595449  errori = 0
main * T = 371.469337236 K alfa = 0.169295673503  errori = 0
main * T = 365.367832489 K alfa = 0.0403266251862  errori = 0
main * T = 302.040433526 K alfa = 0.010398592351  errori = 0
main * T = 213.164185187 K alfa = 0.00993543925688  errori = 0
Model::reportWarning ** ===== Logging warning convergence on deltax; dubious convergence in test
main * T = 116.820195543 K alfa = 1.72743588323e-08  errori = 0

How to get on

  1. check the streams chapter in the LIBPF® SDK Classes overview

  2. Proceed to the tutorial for the Pasteurization example