Set Column Pressure Profile

posted in: C#, Code Snippets 0

This code-snippet in C# shows how to change the column pressure profile. You want to access distillation column from MATLAB? See here.

using CCAPI;
using System;
using System.IO;
using System.Linq;

/* Update the column pressure profile with your values. */


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

            // Load the unit with ID 1, in TrayColumn.cc7 it is a column
            UnitOperation column = flowsheet.GetUnitOperationById(1);
            // The number of stages of that column and the number of 
            // components in the flowsheet are relevant
            int noStages = column.NumberOfStages;
            int noComp = flowsheet.Components.Count();
            // variables are pepared to take the stages data of the column
            float[] old_pressures = new float[noStages];
            float temperatures;
            float[] vapFlow = new float[noComp + 1];
            float[] liqFlow = new float[noComp + 1];
            Console.WriteLine("Old pressure profile:");
            Console.WriteLine("Stage Number" + " " + " Pressure");
            var results = column.GetColumnHydraulicsFromSizing(ColumnType.Tray);
            for (int i = 0; i < noStages; i++)
            {
                // get the pressure from each colmn stage 
                // (other data has to be taken as well, but won't be used)
            

                Console.WriteLine(i + " " + results.Pressure[i] + " " + flowsheet.UnitsCurrent.Pressure);
            }
            // Example for a very simple pressure modofication:
            // Pressure increases from stage to stage by 1%
            float[] new_pressures = new float[noStages];
            new_pressures[0] = old_pressures[0];
            new_pressures[1] = old_pressures[1];
            float incFactor = (float)1.01;
            for (int i = 2; i < noStages; i++)
            {
                new_pressures[i] = new_pressures[i - 1] * incFactor;
            }
            // apply new pressures to column
            column.SetColumnPressureProfile(new_pressures);
            chemcad.Run();

            column = flowsheet.GetUnitOperationById(1);
            float pressure;
            Console.WriteLine();
            Console.WriteLine("New pressure profile:");
            Console.WriteLine("Stage Number" + " " + " Pressure");
            results = column.GetColumnHydraulicsFromSizing(ColumnType.Tray);
            for (int i = 0; i < noStages; i++)
            {
               

                Console.WriteLine(i + " " + results.Pressure[i] + " " + flowsheet.UnitsCurrent.Pressure);
            }

            Console.ReadLine();
        }
    }
}


Output:

Files:
Download TrayColumn.cc7