Tray Column and its Properties

posted in: C#, Code Snippets 0

This code-snippet in C# shows how to define a user stream and read stream properties.

using CCAPI;
using CCAPI.UnitOperations;
using System;
using System.IO;

//Read the distillation column profiles and then 
//define a user stream to read the stream properties
//e.g density, viscosity, heat capactiy etc.


namespace TrayColumn
{
    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(),
                "..\\..\Deisobutanizer.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;

            /* Get an array containing pressure of each tray
             *  from first column in your flowsheet.
             */

            SCDSColumn column = flowsheet.GetUnitOperationById(1) as SCDSColumn;
            int noStages = (int)column.NoOfStages;
            //Read the column hydraulics
            var hydraulics = column.GetColumnHydraulicsFromSizing(ColumnType.Tray);
            var stagedata = hydraulics.StageData;
            float[] density = new float[noStages + 1];
            int st = 0;
            foreach (var stage in stagedata)
            {

                var liquidData = stage.LiquidFlowRate;
                var vaporData = stage.VaporFlowRate;
                float compLiquid = 0;
                float compVapor = 0;
                int compPos = 0;
                float totalFlow = 0;
                var components = flowsheet.Components;
                var componentsNumber = components.Length;
                FlowRateCurUnits[] componentFlowrates = new FlowRateCurUnits[componentsNumber];
                foreach (var xx in liquidData)
                {
                    compLiquid = xx.MolarFlow;
                    compVapor = vaporData[compPos].MolarFlow;
                    totalFlow = compLiquid + compVapor;
                    //Set the component flowrates
                    var comp = flowsheet.GetComponentByPosition((short)compPos);
                    FlowRateCurUnits flowrate = new FlowRateCurUnits(comp, totalFlow);
                    componentFlowrates[compPos] = flowrate;

                    compPos++;

                }
                //Put stream by id in current units
                flowsheet.PutStreamByIdInCurrentUnits(hydraulics.Temeprature[st], hydraulics.Pressure[st], -12312, 0.1f, componentFlowrates, 2000);

                var userStream = flowsheet.GetStreamByIdInCurUnits(2000);
                //Read the property of the user defined stream
                density[st] = userStream.TotalActualDensity;
                Console.WriteLine("The density of stage number " + st + " is " + density[st] + " " + flowsheet.UnitsCurrent.LiquidDensity);
                st++;


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


            Console.ReadLine();
        }
    }
}



Output:

Files:
Download Deisobutanizer.cc7