Esempio n. 1
0
        protected override void OnCalculate()
        {
            //
            //Check Components Number and their IDS
            //
            int zComponentNumber;

            zComponentNumber = InputPort.ComponentsCount;
            string[] zIds;
            zIds = InputPort.Ids;

            //
            //Input Port properties
            //
            double zTemperature = InputPort.Temperature;
            double zPressure    = InputPort.Pressure;
            double zTotalFlow   = InputPort.TotalFlow;

            //Volume is to Calculate...
            InputPort.CalculateProperty(CapeOpenThermoMaterialPropertyType.Volume, CapeOpenPhaseType.Overall, CapeOpenCalculationType.Pure);
            double zVolume   = InputPort.Volume;
            double zEnthalpy = InputPort.Enthalpy;

            double[] zFraction = InputPort.Fraction;

            //
            //Units of Measure changing
            //
            double dTotmod  = zTotalFlow * 3.6;
            double dTempmod = zTemperature - 273.15;
            double dPresmod = zPressure / 100000.0;

            //Calc. Partial Pressure
            double[] partial_pressure = new double[zComponentNumber];
            for (int i = 0; i < zComponentNumber; i++)
            {
                partial_pressure[i] = zFraction[i] * zPressure;
            }

            //Reaction Speed
            double R = 1.98578;

            double r1;
            double r2;
            double r3;

            //Reaction Temperature
            double Treaction = zTemperature + 119.0;

            r1 = K1Value * Math.Exp(-E1Value / (R * Treaction)) * (partial_pressure[0] / 1000.0) * (partial_pressure[2] / 1000.0);
            r2 = K2Value * Math.Exp(-E2Value / (R * Treaction)) * (partial_pressure[0] / 1000.0) * (partial_pressure[2] / 1000.0);
            r3 = K3Value * Math.Exp(-E3Value / (R * Treaction)) * (partial_pressure[0] / 1000.0) * (partial_pressure[2] / 1000.0);


            //display the output pressure
            double pressureout = zPressure - 80000.0;
            //define the reactor volume
            double vol = 101.78;

            double propene1;
            double propene2;
            double propene3;
            double k1tot;
            double k2tot;
            double k3tot;

            k1tot = K1Value * Math.Exp(-E1Value / (R * Treaction));
            k2tot = K2Value * Math.Exp(-E2Value / (R * Treaction));
            k3tot = K3Value * Math.Exp(-E3Value / (R * Treaction));

            propene1 = 0.00199 * vol * r1;
            propene2 = 0.00199 * vol * r2;
            propene3 = 0.00199 * vol * r3;

            double propene_final  = zFraction[0] * zTotalFlow * 3.6 - (propene1 + propene2 + propene3);
            double water_final    = zFraction[1] * zTotalFlow * 3.6 + (propene1 + propene2 + 3.0 * propene3);
            double oxygen_final   = zFraction[2] * zTotalFlow * 3.6 - (1.5 * propene1 + 2.5 * propene2 + 4.5 * propene3);
            double nitrogen_final = zFraction[3] * zTotalFlow * 3.6;
            double acetic_final   = zFraction[4] * zTotalFlow * 3.6 + propene2;
            double acrylic_final  = zFraction[5] * zTotalFlow * 3.6 + propene1;
            double CO2_final      = zFraction[6] * zTotalFlow * 3.6 + (propene2 + 3.0 * propene3);

            //Calculate outputstream
            double sum = propene_final + water_final + oxygen_final + nitrogen_final + acetic_final + acrylic_final + CO2_final;

            //mole fractions for output stream
            double fr_propene_final  = propene_final / sum;
            double fr_water_final    = water_final / sum;
            double fr_oxygen_final   = oxygen_final / sum;
            double fr_nitrogen_final = nitrogen_final / sum;
            double fr_acetic_final   = acetic_final / sum;
            double fr_acrylic_final  = acrylic_final / sum;
            double fr_CO2_final      = CO2_final / sum;

            double[] fractionout = new double[8];
            fractionout[0] = fr_propene_final;
            fractionout[1] = fr_water_final;
            fractionout[2] = fr_oxygen_final;
            fractionout[3] = fr_nitrogen_final;
            fractionout[4] = fr_acetic_final;
            fractionout[5] = fr_acrylic_final;
            fractionout[6] = fr_CO2_final;

            //Define Enthalpy
            double deltaH      = -3590030.0;
            double enthalpyOut = zEnthalpy - deltaH;

            //
            //Output Thermo Material Object
            //

            double suma = sum / 3.6;

            OutputPort.Temperature = Treaction;
            OutputPort.Pressure    = pressureout;
            OutputPort.TotalFlow   = suma;
            OutputPort.Enthalpy    = enthalpyOut;
            OutputPort.Volume      = zVolume;
            OutputPort.Fraction    = fractionout;

            OutputPort.CalculateEquilibrium(CapeOpenFlashType.TP);
        }
Esempio n. 2
0
        protected override void OnCalculate()
        {
            double zTemperature = InputPort.Temperature;
            double zPressure    = InputPort.Pressure;
            double zEnthalpy    = InputPort.Enthalpy;
            double zTotalFlow   = InputPort.TotalFlow;

            double[] zFraction = InputPort.Fraction;

            // totalflow ii inmultit cu 3.6 pt. ca CO il citeste in mol/s si tie iti trebuie in kmol/hr
            double zTotmod = zTotalFlow * 3.6;


            string[] zIds = InputPort.Ids;

            string[] zPropertyValue = new string[1];
            zPropertyValue[0] = "casRegistryNumber";

            string[] zPropVal = null;
            object[] zObj;

            zObj     = (object[])InputPort.ConnectedObject.GetComponentConstant(zPropertyValue, zIds);
            zPropVal = new string[zObj.GetLength(0)];
            for (int i = 0; i < zObj.GetLength(0); i++)
            {
                zPropVal[i] = (string)zObj[i];
            }

            //Volume
            InputPort.CalculateProperty(CapeOpenThermoMaterialPropertyType.Volume, CapeOpenPhaseType.Overall, CapeOpenCalculationType.Pure);
            double zVolume = InputPort.Volume;

            Dictionary <string, double> zComponentsList = new Dictionary <string, double>();
            DataAccessHelper            zAccessoDati    = new DataAccessHelper();

            foreach (string zCas in zPropVal)
            {
                double zSumIndexNorm = 0;
                try
                {
                    zSumIndexNorm = zAccessoDati.GetSumIndexNorm(zCas);
                }
                catch
                {
                    return;
                }
                zComponentsList.Add(zCas, zSumIndexNorm);
            }

            double zInCP;
            double zInEP;
            double zOutCP;
            double zOutEP;

            double MM;

            MM = 0;

            double[] zMolecularWeights;
            zObj = (object[])InputPort.ConnectedObject.GetComponentConstant("molecularWeight", zIds);
            zMolecularWeights = new double[zObj.GetLength(0)];

            for (int i = 0; i < zObj.GetLength(0); i++)
            {
                zMolecularWeights[i] = (double)zObj[i];
            }

            //calculate of molecular weight of the stream
            for (int i = 0; i < InputPort.ComponentsCount; i++)
            {
                MM = MM + zFraction[i] * zMolecularWeights[i];
            }

            double[] zFractionMasses = new double[InputPort.ComponentsCount];

            for (int i = 0; i < InputPort.ComponentsCount; i++)
            {
                zFractionMasses[i] = zFraction[i] * zMolecularWeights[i] / MM;
            }



            StreamWriter zWriter;

            if (ProcessTypeValue == 0.0)
            {     //for process_type=0 calc impact
                if (CapePositionValue == 0.0)
                { //    ' if the stream is the begin of process
                    zInCP = 0;
                    double zIndividual0;
                    for (int r0 = 0; r0 < InputPort.ComponentsCount; r0++)
                    {
                        zIndividual0 = zTotmod * MM * zFraction[r0] * zComponentsList[zPropVal[r0]];
                        zInCP        = zInCP + zIndividual0;
                    }
                    zWriter = new StreamWriter("WAR.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine(" type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine(" name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("  ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("value:" + zInCP.ToString());
                    zWriter.Close();
                    //' write the mass flow results
                    zWriter = new StreamWriter("stream.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                    //' write all results
                    zWriter = new StreamWriter("results.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("         value:" + zInCP.ToString());
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                }
                else if (CapePositionValue == 1.0)
                {
                    double zIndividual1;
                    zOutCP  = 0;
                    zWriter = new StreamWriter("stream_name_and_composition.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine(" type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine(" name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("  ind:" + CapePositionValue.ToString());

                    for (int g0 = 0; g0 < InputPort.ComponentsCount; g0++)
                    {
                        zIndividual1 = zTotmod * MM * zFractionMasses[g0] * zComponentsList[zPropVal[g0]];
                        zOutCP       = zOutCP + zIndividual1;
                        double zCompFlowRateWasteCp = zTotmod * MM * zFractionMasses[g0];
                        zWriter.WriteLine(zIds[g0]);
                        zWriter.WriteLine("comp.mass flow-rates=" + zCompFlowRateWasteCp.ToString());
                    }
                    zWriter.Close();

                    zWriter = new StreamWriter("WAR.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine(" type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine(" name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("  ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("value:" + zOutCP.ToString());
                    zWriter.Close();
                    //' write the mass flow results
                    zWriter = new StreamWriter("stream.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                    zWriter = new StreamWriter("results.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("         value:" + zOutCP.ToString());
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                }
                else if (CapePositionValue == 2.0)
                { //if the stream is the end of the process
                    zWriter = new StreamWriter("stream_name_and_composition.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine(" type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine(" name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("  ind:" + CapePositionValue.ToString());

                    for (int u0 = 0; u0 < InputPort.ComponentsCount; u0++)
                    {
                        double zCompFlowRateWasteCp = zTotmod * MM * zFractionMasses[u0];
                        zWriter.WriteLine(zIds[u0]);
                        zWriter.WriteLine("comp.mass flow-rates=" + zCompFlowRateWasteCp.ToString());
                    }
                    zWriter.Close();

                    zWriter = new StreamWriter("WAR.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine(" type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine(" name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("  ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("value:output stream of the chemical process");
                    zWriter.Close();
                    //' write the mass flow results
                    zWriter = new StreamWriter("stream.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                    zWriter = new StreamWriter("results.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("         value:0");
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                }
                else if (CapePositionValue == 3.0)
                { //if the stream is internal
                    zWriter = new StreamWriter("WAR.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine(" type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine(" name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("  ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("value:internal stream of the chemical process");
                    zWriter.Close();
                    //' write the mass flow results
                    zWriter = new StreamWriter("stream.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                    zWriter = new StreamWriter("results.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("         value:0");
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                }
            }

            if (ProcessTypeValue == 1.0)
            {     //' for process_type=1 calc. the impactof process of generation of energy
                if (CapePositionValue == 0.0)
                { // ' beginning of the process
                    zInEP = 0;
                    double zIndividual2;
                    for (int r1 = 0; r1 < InputPort.ComponentsCount; r1++)
                    {
                        zIndividual2 = zTotmod * MM * zFraction[r1] * zComponentsList[zPropVal[r1]];
                        zInEP        = zInEP + zIndividual2;
                    }
                    zWriter = new StreamWriter("WAR.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine(" type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine(" name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("  ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("value:" + zInEP.ToString());
                    zWriter.Close();
                    //' write the mass flow results
                    zWriter = new StreamWriter("stream.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                    //' write all results
                    zWriter = new StreamWriter("results.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                    zWriter.WriteLine("         value:" + zInEP.ToString());
                    zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                    zWriter.Close();
                }
                else if (CapePositionValue == 1.0)
                {//end of process
                    zWriter = new StreamWriter("stream_name_and_composition.txt", true, Encoding.UTF8);
                    zWriter.WriteLine("                   ");
                    zWriter.WriteLine(" type:" + ProcessTypeValue.ToString());
                    zWriter.WriteLine(" name:CO" + CapeIDValue.ToString());
                    zWriter.WriteLine("  ind:" + CapePositionValue.ToString());
                    for (int k0 = 0; k0 < InputPort.ComponentsCount; k0++)
                    {
                        double zCompFlowRateWasteEp = zTotmod * MM * zFractionMasses[k0];
                        zWriter.WriteLine(zIds[k0]);
                        zWriter.WriteLine("comp.mass flow-rates=" + zCompFlowRateWasteEp.ToString());
                    }
                    zWriter.Close();

                    //calculaction of the energy
                    StreamReader zReader = new StreamReader("ENERGY.txt");
                    string       s       = null;
                    while (!zReader.EndOfStream)
                    {
                        s = zReader.ReadLine();
                    }
                    zReader.Close();
                    s = s.Substring(s.Length - 15);

                    double zHProcess = Convert.ToDouble(s.Trim());
                    if (zHProcess < 0)
                    {
                        zOutEP = 0;
                        for (int g1 = 0; g1 < InputPort.ComponentsCount; g1++)
                        {
                            double zIndividual3 = zTotmod * MM * zFraction[g1] * zComponentsList[zPropVal[g1]];
                            zOutEP = zOutEP + zIndividual3;
                        }
                        zWriter = new StreamWriter("WAR.txt", true, Encoding.UTF8);
                        zWriter.WriteLine("                   ");
                        zWriter.WriteLine(" type:" + ProcessTypeValue.ToString());
                        zWriter.WriteLine(" name:CO" + CapeIDValue.ToString());
                        zWriter.WriteLine("  ind:" + CapePositionValue.ToString());
                        zWriter.WriteLine("value:" + zOutEP.ToString());
                        zWriter.Close();
                        //' write the mass flow results
                        zWriter = new StreamWriter("stream.txt", true, Encoding.UTF8);
                        zWriter.WriteLine("                   ");
                        zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                        zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                        zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                        zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                        zWriter.Close();
                        zWriter = new StreamWriter("results.txt", true, Encoding.UTF8);
                        zWriter.WriteLine("                   ");
                        zWriter.WriteLine("          type:" + ProcessTypeValue.ToString());
                        zWriter.WriteLine("          name:CO" + CapeIDValue.ToString());
                        zWriter.WriteLine("           ind:" + CapePositionValue.ToString());
                        zWriter.WriteLine("         value:" + zOutEP.ToString());
                        zWriter.WriteLine("mass flow-rate:" + Convert.ToString(zTotmod * MM));
                        zWriter.Close();
                    }
                    else
                    {
                    }
                }
            }

            //
            //Output thermo material object
            //
            OutputPort.Temperature = zTemperature;
            OutputPort.Pressure    = zPressure;
            OutputPort.Enthalpy    = zEnthalpy;
            OutputPort.TotalFlow   = zTotalFlow;
            OutputPort.Volume      = zVolume;
            OutputPort.Fraction    = zFraction;

            try
            {
                OutputPort.CalculateEquilibrium(CapeOpenFlashType.PH);
            }
            catch
            {
            }
        }