Change Component Flowrate

posted in: C#, Code Snippets 0

This code-snippet in C# shows how to change the flowrate of component in the stream.

using CCAPI;
using System;
using System.IO;

namespace SetCompFlow
{
    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;
            //Count the total number of components in the stream
            int totalComponets = components.Length;

            //Collection of the streams
            StreamCollectionCurUnits streamCollection = flowsheet.StreamsInCurUnits;

            //Select the first stream from the collection of streams

            StreamInCurUnits firstStream = streamCollection.GetStreamAt(0);

            //Write the components name and their flowrates
            int counter = 0;
            foreach (Component comp in components)
            {
                Console.WriteLine("The flowrate of " + comp.Name + " in stream " + firstStream.Id + " is " + firstStream.CompFlow[counter].MolarFlow);
                counter++;
            }

            string input;
            bool inputOK = false;


            float flowrate = 0;
            int position = 0;
            Console.WriteLine("Componet Position   Component Name");
            foreach (Component comp in components)
            {
                Console.WriteLine(comp.position + " " + comp.Name);
            }

            //Enter the component position
            Console.WriteLine("Select the component of which flowrate is to be changed");
            do
            {
                input = Console.ReadLine();
                if (int.TryParse(input, out position) && (position >= 0)  (position < totalComponets)) { inputOK = true; }
                else { Console.Write("Please enter the valid flowrate "); }
            } while (inputOK == false);

            //Enter the flowerate of the component

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

            var selectedComponent = new FlowRateCurUnits(components.GetComponentAt(position), flowrate);
            //Change the flowrate using SetOneComp Flow method
            firstStream.SetOneCompFlow(selectedComponent);
            counter = 0;
            //Check the changed flowrate of the component
            foreach (Component comp in components)

            {
                Console.WriteLine("The flowrate of " + comp.Name + " in stream " + firstStream.Id + " is " + firstStream.CompFlow[counter].MolarFlow);
                counter++;
            }
            Console.WriteLine("Press Any Key To Exit");
            Console.ReadLine(); ;
        }
    }
}

Output:

Files:
Download OneFeed.cc7