Upload
sanjiv-ch
View
250
Download
0
Embed Size (px)
Citation preview
7/29/2019 CSTR Calculation Routines
1/23
' CSTR Calculation Routines' Copyright 2008 Daniel Wagner O. de Medeiros'' This file is part of DWSIM.'' DWSIM is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by' the Free Software Foundation, either version 3 of the License, or' (at your option) any later version.'' DWSIM is distributed in the hope that it will be useful,' but WITHOUT ANY WARRANTY; without even the implied warranty of' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the' GNU General Public License for more details.'' You should have received a copy of the GNU General Public License' along with DWSIM. If not, see .
Imports Microsoft.MSDN.Samples.GraphicObjects
Imports DWSIM.DWSIM.ClassesBasicasTermodinamicaImports Ciloci.FleeImports System.MathImports DWSIM.DWSIM.MathExImports DWSIM.DWSIM.Flowsheet.FlowSheetSolver
Namespace DWSIM.SimulationObjects.Reactors
Public Class Reactor_CSTR
Inherits Reactor
Protected m_vol As DoubleProtected m_isotemp As Double
Dim C0 As Dictionary(Of String, Double)Dim C As Dictionary(Of String, Double)Dim Ri As Dictionary(Of String, Double)Dim Kf, Kr As ArrayListDim DN As Dictionary(Of String, Double)Dim N00 As Dictionary(Of String, Double)Dim Rxi As Dictionary(Of String, Double)Dim DHRi As Dictionary(Of String, Double)
Dim activeAL As Integer = 0
Dim form As FormFlowsheet Dim ims As
DWSIM.SimulationObjects.Streams.MaterialStream Dim pp As
DWSIM.SimulationObjects.PropertyPackages.PropertyPackage Dim ppr As New
DWSIM.SimulationObjects.PropertyPackages.RaoultPropertyPackage()
Public Property IsothermalTemperature() As DoubleGet
Return m_isotempEnd GetSet(ByVal value As Double)
m_isotemp = value
End SetEnd Property
7/29/2019 CSTR Calculation Routines
2/23
Public Property Volume() As Double
GetReturn m_vol
End GetSet(ByVal value As Double)
m_vol = valueEnd SetEnd Property
Public Sub New(ByVal nome As String, ByVal descricao As String)
MyBase.new()Me.m_ComponentName = nomeMe.m_ComponentDescription = descricaoMe.FillNodeItems()Me.QTFillNodeItems()Me.ShowQuickTable = False
N00 = New Dictionary(Of String, Double)DN = New Dictionary(Of String, Double)C0 = New Dictionary(Of String, Double)C = New Dictionary(Of String, Double)Ri = New Dictionary(Of String, Double)Rxi = New Dictionary(Of String, Double)DHRi = New Dictionary(Of String, Double)Kf = New ArrayListKr = New ArrayList
End Sub
Public Sub ODEFunc(ByVal y As Double(), ByRef dy As Double())
Dim i As Integer = 0Dim j As Integer = 0Dim scBC As Double = 0Dim BC As String = ""
'loop through reactionsDim rxn As ReactionDim ar As ArrayList = Me.ReactionsSequence(activeAL)
i = 0Do
'process reaction irxn = form.Options.Reactions(ar(i))For Each sb As ReactionStoichBase In rxn.Components.Values
Ri(sb.CompName) = 0Nexti += 1
Loop Until i = ar.Count
i = 0Do
'process reaction irxn = form.Options.Reactions(ar(i))BC = rxn.BaseReactantscBC = rxn.Components(BC).StoichCoeff
j = 1For Each sb As ReactionStoichBase In rxn.Components.Values
7/29/2019 CSTR Calculation Routines
3/23
C(sb.CompName) = y(j)j = j + 1
Next
Dim T As Double =ims.Fases(0).SPMProperties.temperature.GetValueOrDefault
Dim kxf As Double = rxn.A_Forward * Exp(-rxn.E_Forward /(8.314 * T))
Dim kxr As Double = rxn.A_Reverse * Exp(-rxn.E_Reverse /(8.314 * T))
If T < rxn.Tmin Or T > rxn.Tmax Thenkxf = 0kxr = 0
End If
Dim rx As Double = 0
Dim rxf As Double = 1Dim rxr As Double = 1
'kinetic expressionFor Each sb As ReactionStoichBase In rxn.Components.Values
rxf *= C(sb.CompName) ^ sb.DirectOrderrxr *= C(sb.CompName) ^ sb.ReverseOrder
Nextrx = kxf * rxf - kxr * rxrRxi(rxn.ID) = rx
Kf(i) = kxfKr(i) = kxr
For Each sb As ReactionStoichBase In rxn.Components.Values
Ri(sb.CompName) += rx * sb.StoichCoeff /rxn.Components(BC).StoichCoeff
Next
i += 1
Loop Until i = ar.Count
j = 1For Each kv As KeyValuePair(Of String, Double) In Ri
dy(j) = -kv.Value * Me.Volumej += 1
Next
End Sub
Public Overrides Function Calculate(Optional ByVal args As Object =Nothing) As Integer
If Rxi Is Nothing Then Rxi = New Dictionary(Of String, Double)If DHRi Is Nothing Then DHRi = New Dictionary(Of String,
Double)If DN Is Nothing Then DN = New Dictionary(Of String, Double)
7/29/2019 CSTR Calculation Routines
4/23
If N00 Is Nothing Then N00 = New Dictionary(Of String, Double)If Me.Conversions Is Nothing Then Me.m_conversions = New
Dictionary(Of String, Double)If Me.ComponentConversions Is Nothing Then
Me.m_componentconversions = New Dictionary(Of String, Double)
form = My.Application.ActiveSimulation
Dim objargs As New DWSIM.Outros.StatusChangeEventArgs
If Not Me.GraphicObject.InputConnectors(0).IsAttached Then'Call function to calculate flowsheetWith objargs
.Calculado = False
.Nome = Me.Nome
.Tipo = TipoObjeto.RCT_CSTREnd WithCalculateFlowsheet(FlowSheet, objargs, Nothing)Throw New
Exception(DWSIM.App.GetLocalString("Nohcorrentedematriac16"))ElseIf Not Me.GraphicObject.OutputConnectors(0).IsAttached Then'Call function to calculate flowsheetWith objargs
.Calculado = False
.Nome = Me.Nome
.Tipo = TipoObjeto.RCT_CSTREnd WithCalculateFlowsheet(FlowSheet, objargs, Nothing)Throw New
Exception(DWSIM.App.GetLocalString("Nohcorrentedematriac15"))ElseIf Not Me.GraphicObject.InputConnectors(1).IsAttached Then
'Call function to calculate flowsheetWith objargs
.Calculado = False
.Nome = Me.Nome
.Tipo = TipoObjeto.RCT_CSTREnd WithCalculateFlowsheet(FlowSheet, objargs, Nothing)Throw New
Exception(DWSIM.App.GetLocalString("Nohcorrentedeenerg17"))End If
ims =form.Collections.CLCS_MaterialStreamCollection(Me.GraphicObject.InputConnectors(0).AttachedConnector.AttachedFrom.Name).Clone
pp = Me.PropertyPackageppr = New
DWSIM.SimulationObjects.PropertyPackages.RaoultPropertyPackage()
Me.Reactions.Clear()Me.ReactionsSequence.Clear()Me.Conversions.Clear()Me.ComponentConversions.Clear()Me.DeltaQ = 0Me.DeltaT = 0Me.DN.Clear()
'check active reactions (kinetic only) in the reaction setFor Each rxnsb As ReactionSetBase In
form.Options.ReactionSets(Me.ReactionSetID).Reactions.Values
7/29/2019 CSTR Calculation Routines
5/23
If form.Options.Reactions(rxnsb.ReactionID).ReactionType =ReactionType.Kinetic And rxnsb.IsActive Then
Me.Reactions.Add(rxnsb.ReactionID)End If
Next
'order reactionsDim i As Integeri = 0Dim maxrank As Integer = 0For Each rxnsb As ReactionSetBase In
form.Options.ReactionSets(Me.ReactionSetID).Reactions.ValuesIf rxnsb.Rank > maxrank And
Me.Reactions.Contains(rxnsb.ReactionID) Then maxrank = rxnsb.RankNext
'ordering of parallel reactionsi = 0Dim arr As New ArrayList
Do arr = New ArrayListFor Each rxnsb As ReactionSetBase In
form.Options.ReactionSets(Me.ReactionSetID).Reactions.ValuesIf rxnsb.Rank = i And
Me.Reactions.Contains(rxnsb.ReactionID) Then arr.Add(rxnsb.ReactionID)NextIf arr.Count > 0 Then Me.ReactionsSequence.Add(i, arr)i = i + 1
Loop Until i = maxrank + 1
pp.CurrentMaterialStream = imsppr.CurrentMaterialStream = ims
Dim N0 As New Dictionary(Of String, Double)Dim N As New Dictionary(Of String, Double)Dim DNT As New Dictionary(Of String, Double)
C = New Dictionary(Of String, Double)C0 = New Dictionary(Of String, Double)
Kf = New ArrayList(Me.Reactions.Count)Kr = New ArrayList(Me.Reactions.Count)
Dim scBC, DHr, Hid_r, Hid_p, Hr, Hr0, Hp, Tin, Tin0, Pin, Pout,T As Double
Dim BC As String = ""Dim tmp As ObjectDim maxXarr As New ArrayList
Select Case Me.ReactorOperationMode
Case OperationMode.Isothermic
'Reactants Enthalpy before temperature change (kJ/kg *kg/s = kW)
Hr0 =ims.Fases(0).SPMProperties.enthalpy.GetValueOrDefault *ims.Fases(0).SPMProperties.massflow.GetValueOrDefault
Tin0 =
ims.Fases(0).SPMProperties.temperature.GetValueOrDefault
7/29/2019 CSTR Calculation Routines
6/23
If Me.IsothermalTemperature = 0.0# ThenMe.IsothermalTemperature =ims.Fases(0).SPMProperties.temperature.GetValueOrDefault
ims.Fases(0).SPMProperties.temperature =Me.IsothermalTemperature
With pp
.DW_CalcEquilibrium(DWSIM.SimulationObjects.PropertyPackages.FlashSpec.T,DWSIM.SimulationObjects.PropertyPackages.FlashSpec.P)
Ifims.Fases(3).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid1)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid1)
End IfIfims.Fases(4).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid2)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid2)End IfIf
ims.Fases(5).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid3)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid3)End IfIf
ims.Fases(6).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Aqueous)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Aqueous)End IfIf
ims.Fases(7).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Solid)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Solid)End IfIf
ims.Fases(2).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Vapor)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Vapor)
End If
7/29/2019 CSTR Calculation Routines
7/23
Ifims.Fases(2).SPMProperties.molarfraction.GetValueOrDefault >= 0 Andims.Fases(2).SPMProperties.molarfraction.GetValueOrDefault < 1 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid)End If.DW_CalcCompMolarFlow(-1).DW_CalcCompMassFlow(-1).DW_CalcCompVolFlow(-1).DW_CalcOverallProps()
.DW_CalcTwoPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid,DWSIM.SimulationObjects.PropertyPackages.Fase.Vapor)
.DW_CalcVazaoVolumetrica()
.DW_CalcKvalue()
End With
End Select
Tin = ims.Fases(0).SPMProperties.temperature.GetValueOrDefaultPin = ims.Fases(0).SPMProperties.pressure.GetValueOrDefaultPout = ims.Fases(0).SPMProperties.pressure.GetValueOrDefault -
Me.DeltaP.GetValueOrDefaultims.Fases(0).SPMProperties.pressure = Pout
'Reactants Enthalpy (kJ/kg * kg/s = kW)Hr = ims.Fases(0).SPMProperties.enthalpy.GetValueOrDefault *
ims.Fases(0).SPMProperties.massflow.GetValueOrDefault
N00.Clear()
'loop through reactionsDim rxn As ReactionFor Each ar As ArrayList In Me.ReactionsSequence.Values
i = 0DHr = 0Hid_r = 0Hid_p = 0
Do
'process reaction irxn = form.Options.Reactions(ar(i))
'initial mole flowsFor Each sb As ReactionStoichBase In
rxn.Components.Values
Select Case rxn.ReactionPhaseCase PhaseName.Liquid
If Not N0.ContainsKey(sb.CompName) ThenN0.Add(sb.CompName,
ims.Fases(3).Componentes(sb.CompName).MolarFlow.GetValueOrDefault)N00.Add(sb.CompName, N0(sb.CompName))
N.Add(sb.CompName, N0(sb.CompName))
7/29/2019 CSTR Calculation Routines
8/23
C0.Add(sb.CompName, N0(sb.CompName) /ims.Fases(3).SPMProperties.volumetric_flow.GetValueOrDefault)
ElseN0(sb.CompName) =
ims.Fases(3).Componentes(sb.CompName).MolarFlow.GetValueOrDefaultN(sb.CompName) = N0(sb.CompName)
C0(sb.CompName) = N0(sb.CompName) /ims.Fases(3).SPMProperties.volumetric_flow.GetValueOrDefaultEnd If
Case PhaseName.VaporIf Not N0.ContainsKey(sb.CompName) Then
N0.Add(sb.CompName,ims.Fases(2).Componentes(sb.CompName).MolarFlow.GetValueOrDefault)
N00.Add(sb.CompName, N0(sb.CompName))N.Add(sb.CompName, N0(sb.CompName))C0.Add(sb.CompName, N0(sb.CompName) /
ims.Fases(2).SPMProperties.volumetric_flow.GetValueOrDefault)Else
N0(sb.CompName) =
ims.Fases(2).Componentes(sb.CompName).MolarFlow.GetValueOrDefaultN(sb.CompName) = N0(sb.CompName)C0(sb.CompName) = N0(sb.CompName) /
ims.Fases(2).SPMProperties.volumetric_flow.GetValueOrDefaultEnd If
Case PhaseName.MixtureIf Not N0.ContainsKey(sb.CompName) Then
N0.Add(sb.CompName,ims.Fases(0).Componentes(sb.CompName).MolarFlow.GetValueOrDefault)
N00.Add(sb.CompName, N0(sb.CompName))N.Add(sb.CompName, N0(sb.CompName))C0.Add(sb.CompName, N0(sb.CompName) /
ims.Fases(0).SPMProperties.volumetric_flow.GetValueOrDefault)Else
N0(sb.CompName) =ims.Fases(0).Componentes(sb.CompName).MolarFlow.GetValueOrDefault
N(sb.CompName) = N0(sb.CompName)C0(sb.CompName) = N0(sb.CompName) /
ims.Fases(0).SPMProperties.volumetric_flow.GetValueOrDefaultEnd If
End Select
Next
i += 1
Loop Until i = ar.Count
Ri.Clear()Rxi.Clear()DHRi.Clear()
i = 0Do
'process reaction irxn = form.Options.Reactions(ar(i))BC = rxn.BaseReactantscBC = rxn.Components(BC).StoichCoeff
For Each sb As ReactionStoichBase Inrxn.Components.Values
7/29/2019 CSTR Calculation Routines
9/23
C(sb.CompName) = C0(sb.CompName)
Next
T =
ims.Fases(0).SPMProperties.temperature.GetValueOrDefault
Dim kxf As Double = rxn.A_Forward * Exp(-rxn.E_Forward/ (8.314 * T))
Dim kxr As Double = rxn.A_Reverse * Exp(-rxn.E_Reverse/ (8.314 * T))
Dim rx As Double = 0Dim rxf As Double = 1Dim rxr As Double = 1
'kinetic expressionFor Each sb As ReactionStoichBase In
rxn.Components.Values rxf *= C(sb.CompName) ^ sb.DirectOrderrxr *= C(sb.CompName) ^ sb.ReverseOrder
Nextrx = kxf * rxf - kxr * rxrIf Not Rxi.ContainsKey(rxn.ID) Then
Rxi.Add(rxn.ID, rx)Else
Rxi(rxn.ID) = rxEnd If
If Kf.Count - 1
7/29/2019 CSTR Calculation Routines
10/23
'
Me.activeAL = Me.ReactionsSequence.IndexOfValue(ar)
Dim vc(C.Count) As Double'vc(1) = Me.Volume
i = 1For Each d As Double In C.Valuesvc(i) = di = i + 1
Next
Dim bs As New MathEx.ODESolver.bulirschstoerbs.DefineFuncDelegate(AddressOf ODEFunc)bs.solvesystembulirschstoer(0, Me.Volume, vc, Ri.Count,
0.05 * Me.Volume, 0.000001, True)
C.Clear()i = 1
For Each sb As KeyValuePair(Of String, Double) In C0C(sb.Key) = CDbl(vc(i))i = i + 1
Next
i = 0Do
'process reaction irxn = form.Options.Reactions(ar(i))BC = rxn.BaseReactantscBC = rxn.Components(BC).StoichCoeff
For Each sb As ReactionStoichBase Inrxn.Components.Values
''comp. conversionsIf Not
Me.ComponentConversions.ContainsKey(sb.CompName) ThenMe.ComponentConversions.Add(sb.CompName, 0)
End If
Next
i += 1
Loop Until i = ar.Count
For Each sb As String In Me.ComponentConversions.KeysIf N0(sb) 0.0# Then N(sb) = N0(sb) * (1 - (C0(sb) -
C(sb)) / C0(sb)) Else N(sb) = 0.0#Next
For Each sb As String In Me.ComponentConversions.KeysIf Not DN.ContainsKey(sb) Then
DN.Add(sb, N(sb) - N0(sb))Else
DN(sb) = N(sb) - N0(sb)End If
Next
For Each sb As String In Me.ComponentConversions.Keys
7/29/2019 CSTR Calculation Routines
11/23
If Not DNT.ContainsKey(sb) ThenDNT.Add(sb, DN(sb))
ElseDNT(sb) += DN(sb)
End IfNext
'Ideal Gas Reactants Enthalpy (kJ/kg * kg/s = kW)Hid_r += 0 'ppr.RET_Hid(298.15,
ims.Fases(0).SPMProperties.temperature.GetValueOrDefault,PropertyPackages.Fase.Mixture) *ims.Fases(0).SPMProperties.massflow.GetValueOrDefault
'update mole flows/fractionsDim Nsum As Double = 0
'compute new mole flows'Nsum =
ims.Fases(0).SPMProperties.molarflow.GetValueOrDefault
For Each s2 As Substancia Inims.Fases(0).Componentes.ValuesIf DN.ContainsKey(s2.Nome) Then
Nsum += N(s2.Nome)Else
Nsum += s2.MolarFlow.GetValueOrDefaultEnd If
NextFor Each s2 As Substancia In
ims.Fases(0).Componentes.ValuesIf DN.ContainsKey(s2.Nome) Then
s2.FracaoMolar =(ims.Fases(0).Componentes(s2.Nome).MolarFlow.GetValueOrDefault +DN(s2.Nome)) / Nsum
s2.MolarFlow =ims.Fases(0).Componentes(s2.Nome).MolarFlow.GetValueOrDefault + DN(s2.Nome)
Elses2.FracaoMolar =
ims.Fases(0).Componentes(s2.Nome).MolarFlow.GetValueOrDefault / Nsums2.MolarFlow =
ims.Fases(0).Componentes(s2.Nome).MolarFlow.GetValueOrDefaultEnd If
Next
ims.Fases(0).SPMProperties.molarflow = Nsum
Dim mmm As Double = 0Dim mf As Double = 0For Each s3 As Substancia In
ims.Fases(0).Componentes.Valuesmmm += s3.FracaoMolar.GetValueOrDefault *
s3.ConstantProperties.Molar_WeightNextFor Each s3 As Substancia In
ims.Fases(0).Componentes.Valuess3.FracaoMassica = s3.FracaoMolar.GetValueOrDefault *
s3.ConstantProperties.Molar_Weight / mmms3.MassFlow = s3.FracaoMassica.GetValueOrDefault *
ims.Fases(0).SPMProperties.massflow.GetValueOrDefaultmf += s3.MassFlow.GetValueOrDefault
Next
7/29/2019 CSTR Calculation Routines
12/23
'Ideal Gas Products Enthalpy (kJ/kg * kg/s = kW)Hid_p += 0 'ppr.RET_Hid(298.15,
ims.Fases(0).SPMProperties.temperature.GetValueOrDefault,PropertyPackages.Fase.Mixture) *ims.Fases(0).SPMProperties.massflow.GetValueOrDefault
Dim NS As New Dictionary(Of String, Double)
i = 0Do
'process reaction irxn = form.Options.Reactions(ar(i))
If NS.ContainsKey(rxn.BaseReactant) ThenNS(rxn.BaseReactant) += Rxi(rxn.ID)
ElseNS.Add(rxn.BaseReactant, Rxi(rxn.ID))
End If
i += 1
Loop Until i = ar.Count
i = 0Do
'process reaction irxn = form.Options.Reactions(ar(i))
'Heat released (or absorbed) (kJ/s = kW) (Ideal Gas)Select Case Me.ReactorOperationMode
Case OperationMode.AdiabaticDHr = rxn.ReactionHeat *
Abs(DN(rxn.BaseReactant)) / 1000 * Rxi(rxn.ID) / Ri(rxn.BaseReactant)Case OperationMode.Isothermic
DHr += rxn.ReactionHeat *Abs(DN(rxn.BaseReactant)) / 1000 * Rxi(rxn.ID) / Ri(rxn.BaseReactant)
End Select
DHRi.Add(rxn.ID, DHr)
i += 1
Loop Until i = ar.Count
' comp. conversionsFor Each sb As Substancia In
ims.Fases(0).Componentes.ValuesIf Me.ComponentConversions.ContainsKey(sb.Nome) Then
Me.ComponentConversions(sb.Nome) += -DNT(sb.Nome) /N00(sb.Nome)
End IfNext
'do a flash calc (calculate final temperature/enthalpy)
Select Case Me.ReactorOperationMode
Case OperationMode.Adiabatic
7/29/2019 CSTR Calculation Routines
13/23
Me.DeltaQ =form.Collections.CLCS_EnergyStreamCollection(Me.GraphicObject.InputConnectors(1).AttachedConnector.AttachedFrom.Name).Energia.GetValueOrDefault
'Products Enthalpy (kJ/kg * kg/s = kW)Hp = Me.DeltaQ.GetValueOrDefault + Hr + Hid_p -
Hid_r - DHr
tmp =Me.PropertyPackage.DW_CalcEquilibrio_ISOL(PropertyPackages.FlashSpec.P,PropertyPackages.FlashSpec.H, Pout, Hp /ims.Fases(0).SPMProperties.massflow.GetValueOrDefault, Tin)
Dim Tout As Double = tmp(2)Me.DeltaT = Tout - Tin
ims.Fases(0).SPMProperties.temperature = Tout
With pp
.DW_CalcEquilibrium(DWSIM.SimulationObjects.PropertyPackages.FlashSpec.T,DWSIM.SimulationObjects.PropertyPackages.FlashSpec.P)
Ifims.Fases(3).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid1)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid1)End IfIf
ims.Fases(4).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid2)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid2)End IfIf
ims.Fases(5).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid3)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid3)End IfIf
ims.Fases(6).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Aqueous)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Aqueous)End IfIf
ims.Fases(7).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Solid)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Solid)
7/29/2019 CSTR Calculation Routines
14/23
End IfIf
ims.Fases(2).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Vapor)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Vapor)End IfIf
ims.Fases(2).SPMProperties.molarfraction.GetValueOrDefault >= 0 Andims.Fases(2).SPMProperties.molarfraction.GetValueOrDefault < 1 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid)End If.DW_CalcCompMolarFlow(-1)
.DW_CalcCompMassFlow(-1).DW_CalcCompVolFlow(-1)
.DW_CalcOverallProps()
.DW_CalcTwoPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid,DWSIM.SimulationObjects.PropertyPackages.Fase.Vapor)
.DW_CalcVazaoVolumetrica()
.DW_CalcKvalue()
End With
Case OperationMode.Isothermic
With pp
.DW_CalcEquilibrium(DWSIM.SimulationObjects.PropertyPackages.FlashSpec.T,DWSIM.SimulationObjects.PropertyPackages.FlashSpec.P)
Ifims.Fases(3).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid1)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid1)End IfIf
ims.Fases(4).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid2)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid2)End IfIf
ims.Fases(5).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid3)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid3)
7/29/2019 CSTR Calculation Routines
15/23
End IfIf
ims.Fases(6).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Aqueous)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Aqueous)End IfIf
ims.Fases(7).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Solid)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Solid)End IfIf
ims.Fases(2).SPMProperties.molarfraction.GetValueOrDefault > 0 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Vapor)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Vapor)End IfIf
ims.Fases(2).SPMProperties.molarfraction.GetValueOrDefault >= 0 Andims.Fases(2).SPMProperties.molarfraction.GetValueOrDefault < 1 Then
.DW_CalcPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid)Else
.DW_ZerarPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid)End If.DW_CalcCompMolarFlow(-1).DW_CalcCompMassFlow(-1).DW_CalcCompVolFlow(-1).DW_CalcOverallProps()
.DW_CalcTwoPhaseProps(DWSIM.SimulationObjects.PropertyPackages.Fase.Liquid,DWSIM.SimulationObjects.PropertyPackages.Fase.Vapor)
.DW_CalcVazaoVolumetrica()
.DW_CalcKvalue()
End With
End Select
Next
If Me.ReactorOperationMode = OperationMode.Isothermic Then
'Products Enthalpy (kJ/kg * kg/s = kW)Hp = ims.Fases(0).SPMProperties.enthalpy.GetValueOrDefault
* ims.Fases(0).SPMProperties.massflow.GetValueOrDefault'Heat (kW)Me.DeltaQ = DHr + Hp - Hr0Me.DeltaT = Tin - Tin0
End If
7/29/2019 CSTR Calculation Routines
16/23
Dim ms As DWSIM.SimulationObjects.Streams.MaterialStreamDim cp As ConnectionPointDim mtotal, wtotal As Double
cp = Me.GraphicObject.OutputConnectors(0)If cp.IsAttached Then
ms =form.Collections.CLCS_MaterialStreamCollection(cp.AttachedConnector.AttachedTo.Name)
With ms.Fases(0).SPMProperties.massflow =
ims.Fases(0).SPMProperties.massflow.GetValueOrDefault.Fases(0).SPMProperties.massfraction = 1.Fases(0).SPMProperties.temperature =
ims.Fases(0).SPMProperties.temperature.GetValueOrDefault.Fases(0).SPMProperties.pressure =
ims.Fases(0).SPMProperties.pressure.GetValueOrDefault.Fases(0).SPMProperties.enthalpy =
ims.Fases(0).SPMProperties.enthalpy.GetValueOrDefault
Dim comp AsDWSIM.ClassesBasicasTermodinamica.Substanciamtotal = 0wtotal = 0For Each comp In .Fases(0).Componentes.Values
mtotal +=ims.Fases(0).Componentes(comp.Nome).FracaoMolar.GetValueOrDefault
wtotal +=ims.Fases(0).Componentes(comp.Nome).FracaoMassica.GetValueOrDefault
NextFor Each comp In .Fases(0).Componentes.Values
comp.FracaoMolar =ims.Fases(0).Componentes(comp.Nome).FracaoMolar.GetValueOrDefault / mtotal
comp.FracaoMassica =ims.Fases(0).Componentes(comp.Nome).FracaoMassica.GetValueOrDefault /wtotal
comp.MassFlow =comp.FracaoMassica.GetValueOrDefault *.Fases(0).SPMProperties.massflow.GetValueOrDefault
comp.MolarFlow = comp.FracaoMolar.GetValueOrDefault* .Fases(0).SPMProperties.molarflow.GetValueOrDefault
NextEnd With
End If
'Corrente de energia - atualizar valor da potncia (kJ/s)
Withform.Collections.CLCS_EnergyStreamCollection(Me.GraphicObject.InputConnectors(1).AttachedConnector.AttachedFrom.Name)
.Energia = Me.DeltaQ.GetValueOrDefault
.GraphicObject.Calculated = TrueEnd With
'Call function to calculate flowsheetWith objargs
.Calculado = True
.Nome = Me.Nome
.Tag = Me.GraphicObject.Tag
.Tipo = TipoObjeto.RCT_CSTREnd With
form.CalculationQueue.Enqueue(objargs)
7/29/2019 CSTR Calculation Routines
17/23
End Function
Public Overrides Function DeCalculate() As Integer
'If Not Me.GraphicObject.InputConnectors(0).IsAttached Then
Throw New Exception(DWSIM.App.GetLocalString("Nohcorrentedematriac10"))'If Not Me.GraphicObject.OutputConnectors(0).IsAttached ThenThrow New Exception(DWSIM.App.GetLocalString("Nohcorrentedematriac11"))
'If Not Me.GraphicObject.OutputConnectors(1).IsAttached ThenThrow New Exception(DWSIM.App.GetLocalString("Nohcorrentedematriac11"))
Dim form As Global.DWSIM.FormFlowsheet =My.Application.ActiveSimulation
'Dim ems As DWSIM.SimulationObjects.Streams.MaterialStream =form.Collections.CLCS_MaterialStreamCollection(Me.GraphicObject.InputConnectors(0).AttachedConnector.AttachedFrom.Name)
'Dim W As Double =
ems.Fases(0).SPMProperties.massflow.GetValueOrDefaultDim j As Integer = 0
Dim ms As DWSIM.SimulationObjects.Streams.MaterialStreamDim cp As ConnectionPoint
cp = Me.GraphicObject.OutputConnectors(0)If cp.IsAttached Then
ms =form.Collections.CLCS_MaterialStreamCollection(cp.AttachedConnector.AttachedTo.Name)
With ms.Fases(0).SPMProperties.temperature = Nothing.Fases(0).SPMProperties.pressure = Nothing.Fases(0).SPMProperties.enthalpy = NothingDim comp As
DWSIM.ClassesBasicasTermodinamica.Substanciaj = 0For Each comp In .Fases(0).Componentes.Values
comp.FracaoMolar = 0comp.FracaoMassica = 0j += 1
Next.Fases(0).SPMProperties.massflow = Nothing.Fases(0).SPMProperties.massfraction = 1.Fases(0).SPMProperties.molarfraction = 1.GraphicObject.Calculated = False
End WithEnd If
'Call function to calculate flowsheetDim objargs As New DWSIM.Outros.StatusChangeEventArgsWith objargs
.Calculado = False
.Nome = Me.Nome
.Tipo = TipoObjeto.RCT_CSTREnd With
form.CalculationQueue.Enqueue(objargs)
End Function
7/29/2019 CSTR Calculation Routines
18/23
Public Overrides Sub QTFillNodeItems()
End Sub
Public Overrides Sub UpdatePropertyNodes(ByVal su As
SistemasDeUnidades.Unidades, ByVal nf As String)
End Sub
Public Overrides Sub PopulatePropertyGrid(ByRef pgrid AsPropertyGridEx.PropertyGridEx, ByVal su As SistemasDeUnidades.Unidades)
Dim Conversor As New DWSIM.SistemasDeUnidades.Conversor
With pgrid
.PropertySort = PropertySort.Categorized
.ShowCustomProperties = True
.Item.Clear()
MyBase.PopulatePropertyGrid(pgrid, su)
Dim ent, saida, energ As StringIf Me.GraphicObject.InputConnectors(0).IsAttached = True
Thenent =
Me.GraphicObject.InputConnectors(0).AttachedConnector.AttachedFrom.TagElse
ent = ""End IfIf Me.GraphicObject.OutputConnectors(0).IsAttached = True
Thensaida =
Me.GraphicObject.OutputConnectors(0).AttachedConnector.AttachedTo.TagElse
saida = ""End If
If Me.GraphicObject.InputConnectors(1).IsAttached = TrueThen
energ =Me.GraphicObject.InputConnectors(1).AttachedConnector.AttachedFrom.Tag
Elseenerg = ""
End If
.Item.Add(DWSIM.App.GetLocalString("Correntedeentrada"),ent, False, DWSIM.App.GetLocalString("Conexes1"), "", True)
With .Item(.Item.Count - 1).DefaultValue = Nothing.CustomEditor = New
DWSIM.Editors.Streams.UIInputMSSelectorEnd With
.Item.Add(DWSIM.App.GetLocalString("Correntedesada"),saida, False, DWSIM.App.GetLocalString("Conexes1"), "", True)
With .Item(.Item.Count - 1).DefaultValue = Nothing
.CustomEditor = NewDWSIM.Editors.Streams.UIOutputMSSelector
7/29/2019 CSTR Calculation Routines
19/23
End With
.Item.Add(DWSIM.App.GetLocalString("Correntedeenergia"),energ, False, DWSIM.App.GetLocalString("Conexes1"), "", True)
With .Item(.Item.Count - 1).DefaultValue = Nothing
.CustomEditor = NewDWSIM.Editors.Streams.UIInputESSelectorEnd With
.Item.Add(DWSIM.App.GetLocalString("RConvPGridItem1"),FlowSheet.Options.ReactionSets(Me.ReactionSetID).Name, False,DWSIM.App.GetLocalString("Parmetrosdeclculo2"),DWSIM.App.GetLocalString("RConvPGridItem1Help"), True)
With .Item(.Item.Count - 1).CustomEditor = New
DWSIM.Editors.Reactors.UIReactionSetSelector.IsDropdownResizable = True
End With
.Item.Add(DWSIM.App.GetLocalString("RConvPGridItem2"), Me,"ReactorOperationMode", False,DWSIM.App.GetLocalString("Parmetrosdeclculo2"),DWSIM.App.GetLocalString("RConvPGridItem2Help"), True)
With .Item(.Item.Count - 1).IsBrowsable = False
End With
Dim valor As String
If Me.ReactorOperationMode = OperationMode.Isothermic Thenvalor =
Format(Conversor.ConverterDoSI(su.spmp_temperature,Me.IsothermalTemperature), FlowSheet.Options.NumberFormat)
.Item.Add(FT(DWSIM.App.GetLocalString("RSCTRIsothermalTemperature"),su.spmp_temperature), valor, False,DWSIM.App.GetLocalString("Parmetrosdeclculo2"),DWSIM.App.GetLocalString("RCSTRPGridItem1Help"), True)
End If
valor = Format(Conversor.ConverterDoSI(su.spmp_deltaP,Me.DeltaP.GetValueOrDefault), FlowSheet.Options.NumberFormat)
.Item.Add(FT(DWSIM.App.GetLocalString("Quedadepresso"),su.spmp_deltaP), valor, False,DWSIM.App.GetLocalString("Parmetrosdeclculo2"),DWSIM.App.GetLocalString("Quedadepressoaplicad6"), True)
With .Item(.Item.Count - 1).DefaultValue = Nothing.DefaultType = GetType(Nullable(Of Double))
End With
valor = Format(Conversor.ConverterDoSI(su.volume,Me.Volume), FlowSheet.Options.NumberFormat)
.Item.Add(FT(DWSIM.App.GetLocalString("RCSTRPGridItem1"),su.volume), valor, False, DWSIM.App.GetLocalString("Parmetrosdeclculo2"),DWSIM.App.GetLocalString("RCSTRPGridItem1Help"), True)
With .Item(.Item.Count - 1).DefaultValue = Nothing
.DefaultType = GetType(Nullable(Of Double))End With
7/29/2019 CSTR Calculation Routines
20/23
If Me.GraphicObject.Calculated Then
.Item.Add(FT(DWSIM.App.GetLocalString("DeltaT2"),su.spmp_deltaT), Format(Conversor.ConverterDoSI(su.spmp_deltaT,Me.DeltaT.GetValueOrDefault), FlowSheet.Options.NumberFormat), True,
DWSIM.App.GetLocalString("Resultados3"),DWSIM.App.GetLocalString("Diferenadetemperatur"), True)With .Item(.Item.Count - 1)
.DefaultValue = Nothing
.DefaultType = GetType(Nullable(Of Double))End With
.Item.Add(FT(DWSIM.App.GetLocalString("RConvPGridItem3"),su.spmp_heatflow), Format(Conversor.ConverterDoSI(su.spmp_heatflow,Me.DeltaQ.GetValueOrDefault), FlowSheet.Options.NumberFormat), True,DWSIM.App.GetLocalString("Resultados3"), "", True)
With .Item(.Item.Count - 1)
.DefaultValue = Nothing.DefaultType = GetType(Nullable(Of Double))End With
'CustomPropertyCollectionDim m As New PropertyGridEx.CustomPropertyCollection()For Each dbl As KeyValuePair(Of String, Double) In
Me.ComponentConversionsvalor = Format(dbl.Value * 100,
FlowSheet.Options.NumberFormat)If dbl.Value >= 0 Then
m.Add(DWSIM.App.GetComponentName(dbl.Key),valor, False, DWSIM.App.GetLocalString("ComponentesConversoes"),DWSIM.App.GetLocalString("RCSTRPGridItem3Help"), True)
m.Item(m.Count - 1).IsReadOnly = Truem.Item(m.Count - 1).DefaultValue = Nothingm.Item(m.Count - 1).DefaultType =
GetType(Nullable(Of Double))End If
Next
.Item.Add(DWSIM.App.GetLocalString("ComponentesConversoes") & " (%)", m,True, DWSIM.App.GetLocalString("Resultados3"),DWSIM.App.GetLocalString("RCSTRPGridItem2Help"), True)
With .Item(.Item.Count - 1).IsReadOnly = True.IsBrowsable = True.BrowsableLabelStyle =
PropertyGridEx.BrowsableTypeConverter.LabelStyle.lsEllipsis.CustomEditor = New
System.Drawing.Design.UITypeEditorEnd With
'CustomPropertyCollectionDim m2 As New PropertyGridEx.CustomPropertyCollection()For Each dbl As KeyValuePair(Of String, Double) In Rxi
m2.Add(form.Options.Reactions(dbl.Key).Name,Format(dbl.Value / C(form.Options.Reactions(dbl.Key).BaseReactant),FlowSheet.Options.NumberFormat), False,
DWSIM.App.GetLocalString("ReactionExtents"), DWSIM.App.GetLocalString(""),True)
7/29/2019 CSTR Calculation Routines
21/23
m2.Item(m2.Count - 1).IsReadOnly = Truem2.Item(m2.Count - 1).DefaultValue = Nothingm2.Item(m2.Count - 1).DefaultType =
GetType(Nullable(Of Double))Next
.Item.Add(FT(DWSIM.App.GetLocalString("ReactionExtents"),su.spmp_molarflow), m2, True, DWSIM.App.GetLocalString("Resultados3"),DWSIM.App.GetLocalString(""), True)
With .Item(.Item.Count - 1).IsReadOnly = True.IsBrowsable = True.BrowsableLabelStyle =
PropertyGridEx.BrowsableTypeConverter.LabelStyle.lsEllipsis.CustomEditor = New
System.Drawing.Design.UITypeEditorEnd With
'CustomPropertyCollectionDim m3 As New PropertyGridEx.CustomPropertyCollection()For Each dbl As KeyValuePair(Of String, Double) In Rxi
m3.Add(form.Options.Reactions(dbl.Key).Name,Format(dbl.Value / C(form.Options.Reactions(dbl.Key).BaseReactant) /Me.Volume, FlowSheet.Options.NumberFormat), False,DWSIM.App.GetLocalString("ReactionExtents"), DWSIM.App.GetLocalString(""),True)
m3.Item(m3.Count - 1).IsReadOnly = Truem3.Item(m3.Count - 1).DefaultValue = Nothingm3.Item(m3.Count - 1).DefaultType =
GetType(Nullable(Of Double))Next
.Item.Add(FT(DWSIM.App.GetLocalString("ReactionRates"),su.reac_rate), m3, True, DWSIM.App.GetLocalString("Resultados3"),DWSIM.App.GetLocalString(""), True)
With .Item(.Item.Count - 1).IsReadOnly = True.IsBrowsable = True.BrowsableLabelStyle =
PropertyGridEx.BrowsableTypeConverter.LabelStyle.lsEllipsis.CustomEditor = New
System.Drawing.Design.UITypeEditorEnd With
'CustomPropertyCollectionDim m4 As New PropertyGridEx.CustomPropertyCollection()For Each dbl As KeyValuePair(Of String, Double) In DHRi
m4.Add(form.Options.Reactions(dbl.Key).Name,Format(Conversor.ConverterDoSI(su.spmp_heatflow, dbl.Value),FlowSheet.Options.NumberFormat), False,DWSIM.App.GetLocalString("ReactionHeats"), DWSIM.App.GetLocalString(""),True)
m4.Item(m4.Count - 1).IsReadOnly = Truem4.Item(m4.Count - 1).DefaultValue = Nothingm4.Item(m4.Count - 1).DefaultType =
GetType(Nullable(Of Double))Next
7/29/2019 CSTR Calculation Routines
22/23
.Item.Add(FT(DWSIM.App.GetLocalString("ReactionHeats"),su.spmp_heatflow), m4, True, DWSIM.App.GetLocalString("Resultados3"),DWSIM.App.GetLocalString(""), True)
With .Item(.Item.Count - 1).IsReadOnly = True.IsBrowsable = True
.BrowsableLabelStyle =PropertyGridEx.BrowsableTypeConverter.LabelStyle.lsEllipsis
.CustomEditor = NewSystem.Drawing.Design.UITypeEditor
End With
End If
End With
End Sub
Public Overrides Function GetPropertyValue(ByVal prop As String,
Optional ByVal su As SistemasDeUnidades.Unidades = Nothing) As ObjectIf su Is Nothing Then su = NewDWSIM.SistemasDeUnidades.UnidadesSI
Dim cv As New DWSIM.SistemasDeUnidades.ConversorDim value As Double = 0Dim propidx As Integer = CInt(prop.Split("_")(2))
Select Case propidx
Case 0'PROP_HT_0 Pressure Dropvalue = cv.ConverterDoSI(su.spmp_deltaP,
Me.DeltaP.GetValueOrDefault)
End Select
Return valueEnd Function
Public Overloads Overrides Function GetProperties(ByVal proptype AsSimulationObjects_BaseClass.PropertyType) As String()
Dim i As Integer = 0Dim proplist As New ArrayListSelect Case proptype
Case PropertyType.RWFor i = 0 To 0
proplist.Add("PROP_CS_" + CStr(i))Next
Case PropertyType.WRFor i = 0 To 0
proplist.Add("PROP_CS_" + CStr(i))Next
Case PropertyType.ALLFor i = 0 To 0
proplist.Add("PROP_CS_" + CStr(i))Next
End SelectReturn proplist.ToArray(GetType(System.String))proplist = Nothing
End Function
7/29/2019 CSTR Calculation Routines
23/23
Public Overrides Function SetPropertyValue(ByVal prop As String,ByVal propval As Object, Optional ByVal su AsDWSIM.SistemasDeUnidades.Unidades = Nothing) As Object
If su Is Nothing Then su = NewDWSIM.SistemasDeUnidades.UnidadesSI
Dim cv As New DWSIM.SistemasDeUnidades.Conversor
Dim propidx As Integer = CInt(prop.Split("_")(2))
Select Case propidx
Case 0'PROP_HT_0 Pressure DropMe.DeltaP = cv.ConverterParaSI(su.spmp_deltaP, propval)
End SelectReturn 1
End Function
Public Overrides Function GetPropertyUnit(ByVal prop As String,
Optional ByVal su As SistemasDeUnidades.Unidades = Nothing) As ObjectIf su Is Nothing Then su = NewDWSIM.SistemasDeUnidades.UnidadesSI
Dim cv As New DWSIM.SistemasDeUnidades.ConversorDim value As String = ""Dim propidx As Integer = CInt(prop.Split("_")(2))
Select Case propidx
Case 0'PROP_HT_0 Pressure Dropvalue = su.spmp_deltaP
End Select
Return valueEnd Function
End Class
End Namespace