Calculate KValues (Short)

posted in: C#, Code Snippets 0

This code-snippet in C# shows how to calculate K-Values using CCAPI.

using CCAPI;
using System;
using System.IO;


//Define the temperature, pressure, flow rates 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;

            //Enter the temperature of the stream in current flowsheet units.
            var temperature = 20;
            //Enter the pressure of the stream in current flowsheet units.
            var pressure = 12;

            Component firstComponent = flowsheet.GetComponentByPosition(0);
            Component secondComponent = flowsheet.GetComponentByPosition(1);

            //Define flowrate of the components in the liquid stream
            FlowRateCurUnits liqFirst = new FlowRateCurUnits(firstComponent, 2);
            FlowRateCurUnits liqSecond = new FlowRateCurUnits(secondComponent, 2);
            FlowRateCurUnits[] liqFlow = new FlowRateCurUnits[] { liqFirst, liqSecond };

            //Define flowrate of the components in the vapor stream
            FlowRateCurUnits vapFirst = new FlowRateCurUnits(firstComponent, 2);
            FlowRateCurUnits vapSecond = new FlowRateCurUnits(secondComponent, 2);
            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