Calculate KValues (Long Version)

posted in: C#, Code Snippets 0

This code-snippet in C# shows how to use the K-Value method to get the K-Values.

using CCAPI;
using System;
using System.IO;


//Define the temperature, pressure, flowrates of the components in liquid and vapor stream. Use KValue //method to obtain activity co-efficients and fugacity co-efficients.
namespace Kvalues
{
    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.WriteLine("Press Any Key To Exit");
                Console.ReadLine();
                return;
            }
            // Load the flowsheet
            Flowsheet flowsheet = chemcad.CurrentFlowsheet;
            bool inputOK = false;
            //Enter the temperautre of the stream in current flowhseet units.
            float temperature = 0;
            string input;
            Console.WriteLine("Enter the tempertaure of stream" + " in " + flowsheet.UnitsCurrent.Temperature);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out temperature) && (temperature >= -500)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false); // repeat input until user input is valid 
            //Enter the pressure of the stream in current flowsheet units.
            float pressure = 0;
            Console.WriteLine("Enter the pressure of stream" + " in " + flowsheet.UnitsCurrent.Pressure);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out pressure) && (pressure >= 0)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false); // repeat input until user input is valid 

            Component firstComponent = flowsheet.GetComponentByPosition(0);
            Component secondComponent = flowsheet.GetComponentByPosition(1);
            float liqFlowOne = 0;
            float liqFlowTwo = 0;
            float vapFlowOne = 0;
            float vapFlowTwo = 0;
            //Define flowrate of the components in the liquid stream
            Console.WriteLine("Enter the flowrate of " + firstComponent.Name + " present in liquid stream in " + flowsheet.UnitsCurrent.MolarFlowRate);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out liqFlowOne) && (liqFlowOne >= 0)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false);
            Console.WriteLine("Enter the flowrate of " + secondComponent.Name + " present in liquid stream in " + flowsheet.UnitsCurrent.MolarFlowRate);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out liqFlowTwo) && (liqFlowTwo >= 0)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false);

            FlowRateCurUnits liqFirst = new FlowRateCurUnits(firstComponent, liqFlowOne);
            FlowRateCurUnits liqSecond = new FlowRateCurUnits(secondComponent, liqFlowTwo);
            FlowRateCurUnits[] liqFlow = new FlowRateCurUnits[] { liqFirst, liqSecond };
            //Define flowrate of the components in the vapor stream
            Console.WriteLine("Enter the flowrate of " + firstComponent.Name + " present in vapor stream in " + flowsheet.UnitsCurrent.MolarFlowRate);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out vapFlowOne) && (vapFlowOne >= 0)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false);

            FlowRateCurUnits vapFirst = new FlowRateCurUnits(firstComponent, vapFlowOne);

            Console.WriteLine("Enter the flowrate of " + secondComponent.Name + " present in vapor stream in " + flowsheet.UnitsCurrent.MolarFlowRate);
            do
            {
                input = Console.ReadLine();
                if (float.TryParse(input, out vapFlowTwo) && (vapFlowTwo >= 0)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false);

            FlowRateCurUnits vapSecond = new FlowRateCurUnits(secondComponent, vapFlowTwo);
            FlowRateCurUnits[] vapFlow = new FlowRateCurUnits[] { vapFirst, vapSecond };

            //Calculate the Kvalues and obtain the results
            var results = flowsheet.CalculateKValues(temperature, pressure, liqFlow, vapFlow);

            var ActivityArray = results.ActivityCoefficients;
            //Display the results of activity coefficients.
            foreach (ComponentValue item in ActivityArray)
            {
                Console.WriteLine("The activity coefficient of " + item.Component.Name + " is " + item.Amount);
            }
            //Display the results of fugacity coefficients.
            var FugacityArray = results.FugacityCoefficients;

            foreach (ComponentValue item in FugacityArray)
            {
                Console.WriteLine("The fugacity coefficient of " + item.Component.Name + " is " + item.Amount);
            }

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


            Console.ReadLine();
        }
    }
}


Output:

Files:
Download OneFeed.cc7