Feed Flash

posted in: C#, Code Snippets 0

Flash methods

Several unit operations requires vapor liquid equilibrium data and thermodynamic models. Flash methods are used to determine the vapor liquid equilibrium.

 

CC-API has several flash methods which can be used for development of new unit operation. This code-snippet in C# shows how to use flash methods. You want to same method in MATLAB? See here.

using CCAPI;
using System;
using System.IO;
namespace SCDS
{/* Feed stream is already defined in the flowsheet.
 * Use CHEMCAD flash calculations and retrieve the calculated liquid
 * and vapor stream, and the k-values at equilibrium and ion flow rates 
 * (if electrolyte is chosen). */

    class Program
    {
        static void Main(string[] args)
        {
            // Initiate a new instance of CHEMCAD
            Console.WriteLine("Initializing CC-API...");
            Server chemcad;
            //Put your .NET Interface key here
            var license_key="...";
            try
            {
                chemcad = new Server(license_key);
            }
            // If the server doesn't respond or
            // if reply results in end of program:
            catch (Exception ex)
            {
                // Display server message and close after key is pushed
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                return;
            }

            // Open a CHEMCAD file relative from the current directory
            string path = Path.Combine(Directory.GetCurrentDirectory(),
                "...\\...\\...\\OneFeed.cc7");
            Console.WriteLine("Opening: " + path + "...\n");
            try
            {
                chemcad.LoadFlowsheet(path);

                // Load the flowsheet
                Flowsheet flowsheet = chemcad.CurrentFlowsheet;
                //Reading the feed stream

                StreamInCurUnits feed = flowsheet.StreamsInCurUnits.GetStreamAt(0);
                Console.WriteLine("Choose a flash method:");
                Console.WriteLine("0: - Use inlet Temperature and Pressure. Calculate Vapor fraction and enthalpy");
                Console.WriteLine("1: HP - Heating rate and product pressure");
                Console.WriteLine("2: TP - Product temperature and pressure");
                Console.WriteLine("3: VP - vapor fraction and product pressure");
                Console.WriteLine("4: VT - vapor fraction and product temperature");
                Console.Write("Please select 0, 1, 2, 3 or 4: ");

                string input;
                bool inputOK = false;
                int method;


                inputOK = false;
                do
                {
                    input = Console.ReadLine();
                    if (int.TryParse(input, out method) && (method == 0 || method == 1 || method == 2 || method == 3 || method == 4)) { inputOK = true; }
                    else { Console.Write("Input invalid, please select 0,1, 2, 3 or 4: "); }
                } while (inputOK == false); // repeat input until user input is valid 

                float temp = 0;
                float pres = 0;
                float vaporFraction = 0;
                float enthalpy = 0;
                FlashResult flashResults;

                switch (method)
                {
                    case 0:
                        Console.WriteLine("Using inlet Temperature and Pressure \n");

                        flashResults = flowsheet.CalculateTPFlash(feed.Temperature, feed.Pressure, feed);
                        break;
                    case 1:
                        inputOK = false;
                        Console.Write("Enter Enthalpy Rate [" + flowsheet.UnitsCurrent.EnthalpyRate + "]: ");
                        do
                        {
                            input = Console.ReadLine();
                            if (Single.TryParse(input, out enthalpy)) { inputOK = true; }
                            else { Console.Write("Input invalid, please enter a real number: "); }

                        } while (inputOK == false); // repeat input until user inputs valid temperature
                        Console.Write("Enter Pressure [" + flowsheet.UnitsCurrent.Pressure + "]: ");
                        do
                        {
                            input = Console.ReadLine();
                            if (Single.TryParse(input, out pres)) { inputOK = true; }
                            else { Console.Write("Input invalid, please enter a real number: "); }

                        } while (inputOK == false); // repeat input until user inputs valid temperature

                        flashResults = flowsheet.CalculateHPFlash(enthalpy, pres, feed);


                        break;
                    case 2:
                        inputOK = false;
                        Console.Write("Enter Temperarture [" + flowsheet.UnitsCurrent.Temperature + "]: ");
                        do
                        {
                            input = Console.ReadLine();
                            if (Single.TryParse(input, out temp)) { inputOK = true; }
                            else { Console.Write("Input invalid, please enter a real number: "); }
                        } while (inputOK == false); // repeat input until user inputs valid enthalpy
                        Console.Write("Enter Pressure [" + flowsheet.UnitsCurrent.Pressure + "]: ");

                        do
                        {
                            input = Console.ReadLine();
                            if (Single.TryParse(input, out pres)) { inputOK = true; }
                            else { Console.Write("Input invalid, please enter a real number: "); }
                        } while (inputOK == false); // repeat input until user inputs valid enthalpy

                        flashResults = flowsheet.CalculateTPFlash(temp, pres, feed);
                        break;
                    case 3:
                        inputOK = false;
                        Console.Write("Enter Vapor Fraction : ");
                        do
                        {
                            input = Console.ReadLine();
                            if (Single.TryParse(input, out vaporFraction)) { inputOK = true; }
                            else { Console.Write("Input invalid, please enter a real number: "); }
                        } while (inputOK == false); // repeat input until user inputs valid enthalpy
                        Console.Write("Enter Pressure [" + flowsheet.UnitsCurrent.Pressure + "]: ");

                        do
                        {
                            input = Console.ReadLine();
                            if (Single.TryParse(input, out pres)) { inputOK = true; }
                            else { Console.Write("Input invalid, please enter a real number: "); }
                        } while (inputOK == false); // repeat input until user inputs valid enthalpy

                        flashResults = flowsheet.CalculateVPFlash(vaporFraction, pres, feed);
                        break;


                    case 4:
                        inputOK = false;
                        Console.Write("Enter Vapor Fraction ");
                        do
                        {
                            input = Console.ReadLine();
                            if (Single.TryParse(input, out vaporFraction)) { inputOK = true; }
                            else { Console.Write("Input invalid, please enter a real number: "); }
                        } while (inputOK == false); // repeat input until user inputs valid enthalpy
                        Console.Write("Enter Temperature [" + flowsheet.UnitsCurrent.Temperature + "]: ");
                        do
                        {
                            input = Console.ReadLine();
                            if (Single.TryParse(input, out temp)) { inputOK = true; }
                            else { Console.Write("Input invalid, please enter a real number: "); }
                        } while (inputOK == false); // repeat input until user inputs valid enthalpy

                        flashResults = flowsheet.CalculateVTFlash(vaporFraction, temp, feed);

                        break;
                    default:

                        flashResults = flowsheet.CalculateVTFlash(temp, pres, feed);
                        break;
                }
                //Print the flash results



                StreamResult streamLiquid = flashResults.LiquidStream;

                StreamResult streamVapor = flashResults.VaporStream;
                ComponentValueCollection kValues;
                kValues = flashResults.KValues;

                //Printing Molar flowrate of vapor stream
                Console.WriteLine("The Molar Flowrate of vapor stream is "
                    + streamVapor.MoleRate.ToString() + " " + flowsheet.UnitsCurrent.MolarFlowRate);


                switch (method)
                {
                    case 0:
                        Console.WriteLine("Vapor fraction after flashing the stream: " + flashResults.MoleVaporFraction);
                        Console.WriteLine("Enthlpy of the vapor stream is " + streamVapor.Enthalpy + "" + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
                        break;
                    case 1:
                        Console.WriteLine("Vapor fraction after flashing the stream: " + flashResults.MoleVaporFraction);
                        Console.WriteLine("Temperature of the vapor stream is " + streamVapor.Temperature + "" + flowsheet.UnitsCurrent.Temperature + "\n");
                        break;


                    case 2:
                        Console.WriteLine("Vapor fraction after flashing the stream: " + flashResults.MoleVaporFraction);
                        Console.WriteLine("Enthalpy of vapor stream is " + streamVapor.Enthalpy + "" + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
                        break;
                    case 3:
                        Console.WriteLine("Temperature of the vapor stream is : " + flashResults.Temperature + flowsheet.UnitsCurrent.Temperature);
                        Console.WriteLine("Enthalpy of vapor stream is " + streamVapor.Enthalpy + "" + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
                        break;
                    case 4:
                        Console.WriteLine("Pressure of the vapor stream is : " + flashResults.Pressure + flowsheet.UnitsCurrent.Pressure);
                        Console.WriteLine("Enthalpy of vapor stream is " + streamVapor.Enthalpy + "" + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
                        break;

                    default:
                        break;
                }
                //Print stream data of liquid stream
                Console.WriteLine("The Molar Flowrate of liquid stream is " + streamLiquid.MoleRate.ToString() + " " + flowsheet.UnitsCurrent.MolarFlowRate);

                switch (method)
                {   //Inlet Tempeature and Pressure
                    case 0:

                        Console.WriteLine("Enthlpy of liquid stream is " + streamLiquid.Enthalpy + "" + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
                        break;
                    case 1:

                        Console.WriteLine("Temperature of liquid stream is " + streamLiquid.Temperature + "" + flowsheet.UnitsCurrent.Temperature + "\n");
                        break;


                    case 2:

                        Console.WriteLine("Enthalpy of liquid stream is " + streamLiquid.Enthalpy + "" + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
                        break;
                    case 3:
                        Console.WriteLine("Temperature of the liquid stream is : " + flashResults.Temperature + flowsheet.UnitsCurrent.Temperature);
                        Console.WriteLine("Enthalpy of liquid stream is " + streamLiquid.Enthalpy + "" + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
                        break;
                    case 4:
                        Console.WriteLine("Pressure of the liquid stream is : " + flashResults.Pressure + flowsheet.UnitsCurrent.Pressure);
                        Console.WriteLine("Enthalpy of liquid stream is " + streamLiquid.Enthalpy + "" + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
                        break;

                    default:
                        break;
                }



                //Printing the K-Values
                int counter = 0;
                foreach (ComponentValue comp in kValues)
                {
                    Console.WriteLine("The K-Value of " + comp.Component.Name + " is " + comp.Amount + "\n");
                    counter++;

                }
              

            }


            // If flowsheet cannot be opened:
            catch (Exception ex)
            {
                // Display error message and close after key is pushed
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                return;
            }

            Console.WriteLine("Press Any Key to exit");
            Console.ReadLine();

        }
    }
}


Output:

Files:
Download OneFeed.cc7