User Defined Stream Flash Calculation

posted in: C#, Code Snippets 0

This code-snippet in C# shows how to define component flow rates and perform flash calculations. You want to perform flash calculations using MATLAB? See here.

using CCAPI;
using System;
using System.IO;
/* Feed stream will be defined by the user.
 * Use CHEMCAD TPflash calculation and retrieve the calculated liquid
 * and vapor stream, and the k-values at equilibrium. */

namespace User_Flash
{
    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);
            }
            // If flowsheet cannot be opened:
            catch (Exception ex)
            {
                // Display error message and close after key is pushed
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                return;
            }
            // Load the flowsheet
            Flowsheet flowsheet = chemcad.CurrentFlowsheet;

            //Collection of the components
            ComponentCollection components = flowsheet.Components;
            Component firstComponent = components.GetComponentAt(0);
            Component secondComponent = components.GetComponentAt(1);
            Component thirdComponent = components.GetComponentAt(2);
            float firstComponentflowrate;
            float secondComponentflowrate = 0;
            float thirdComponentflowrate = 0;
            bool inputOK = false;
            string input;

            Console.WriteLine("Enter the flowrate of " + firstComponent.Name + " in " + flowsheet.UnitsCurrent.MolarFlowRate);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out firstComponentflowrate) && (firstComponentflowrate >= 0)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false); // repeat input until user input is valid 
            FlowRateCurUnits flowFirst = new FlowRateCurUnits(firstComponent, firstComponentflowrate);
            inputOK = false;
            Console.WriteLine("Enter the flowrate of " + secondComponent.Name + " in " + flowsheet.UnitsCurrent.MolarFlowRate);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out secondComponentflowrate) && (secondComponentflowrate >= 0)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false); // repeat input until user input is valid 
            inputOK = false;

            FlowRateCurUnits flowSecond = new FlowRateCurUnits(secondComponent, secondComponentflowrate);
            Console.WriteLine("Give flowrate of " + thirdComponent.Name + " in " + flowsheet.UnitsCurrent.MolarFlowRate);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out thirdComponentflowrate) && (thirdComponentflowrate >= 0)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false); // repeat input until user input is valid 
            FlowRateCurUnits flowThird = new FlowRateCurUnits(thirdComponent, thirdComponentflowrate);
            inputOK = false;
            //Defining array of the flowrate of the components 
            FlowRateCurUnits[] flows = new FlowRateCurUnits[] { flowFirst, flowSecond, flowThird };
            float streamTemperature;
            float streamPressure;
            float streamEnthalpy;
            float flashTemperature;
            float flashPressure;

            Console.WriteLine("Enter the stream temperature in " + flowsheet.UnitsCurrent.Temperature);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out streamTemperature)) { inputOK = true; }
                else { Console.Write("Please enter the valid temperature "); }
            } while (inputOK == false); // repeat input until user input is valid 

            inputOK = false;
            Console.WriteLine("Enter the stream Pressure in " + flowsheet.UnitsCurrent.Pressure);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out streamPressure)) { inputOK = true; }
                else { Console.Write("Please enter the valid pressure "); }
            } while (inputOK == false); // repeat input until user input is valid 

            inputOK = false;
            Console.WriteLine("Enter the stream Enthalpy in " + flowsheet.UnitsCurrent.EnthalpyRate);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out streamEnthalpy)) { inputOK = true; }
                else { Console.Write("Please enter the valid enthalpy "); }
            } while (inputOK == false); // repeat input until user input is valid 

            inputOK = false;
            Console.WriteLine("Enter the Flash Temeperature in " + flowsheet.UnitsCurrent.Temperature);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out flashTemperature)) { inputOK = true; }
                else { Console.Write("Please enter the valid temperature "); }
            } while (inputOK == false); // repeat input until user input is valid 

            inputOK = false;
            Console.WriteLine("Enter the Flash Pressure in " + flowsheet.UnitsCurrent.Pressure);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out flashPressure)) { inputOK = true; }
                else { Console.Write("Please enter the valid pressure "); }
            } while (inputOK == false); // repeat input until user input is valid 


            //Calculating the flash method results
            FlashResult flashResults = flowsheet.CalculateTPFlash(streamTemperature, streamPressure, streamEnthalpy, flashTemperature, flashPressure, flows);


            StreamResult streamLiquid = flashResults.LiquidStream;

            StreamResult streamVapor = flashResults.VaporStream;
            ComponentValueCollection kValues;
            kValues = flashResults.KValues;
            Console.WriteLine("Vapor fraction after flashing the stream: " + flashResults.MoleVaporFraction);
            Console.WriteLine("Enthalpy of vapor stream is " + streamVapor.Enthalpy + " " + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
            Console.WriteLine("Enthalpy of liquid stream is " + streamLiquid.Enthalpy + " " + flowsheet.UnitsCurrent.EnthalpyRate + "\n");
            int counter = 0;
            foreach (ComponentValue comp in kValues)
            {
                Console.WriteLine("The K-Value of " + comp.Component.Name + " is " + comp.Amount + "\n");
                counter++;

            }
            Console.WriteLine("Press Any Key To Exit");
            Console.ReadLine(); ;
        }
    }
}

Output:

Files:
Download OneFeed.cc7