How to perform an Elemental Mass Balance

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: PGMs ➔ Example PGM Files ➔ Elemental Mass Balance

Simple Examples Subroutines Examples Dynamic Examples Steady State Overall Mass Balance Array and Matrix Examples
Basic
Layout
Simple
Calculations
Initialise
PreStart
Multi-Step Trigger Checking
Project
Counter, While
and Random
Belt Filter
Wash Loss
Startup
Actions
Mass
Balance
Mass
Balance
Species
Balance
Elemental
Balance
Lookup
Value
Set
Values
Tridiagonal
System

Related Links: PGM Files using Class and Functions, Tag Select Class, FAQ (Mass & Energy Balance)


It is often important to perform an elemental mass balance to account for the total in / out of the elements. Here we will show how to obtain a quick balance of an element by adding a PGM file using the TagSelect Class to sum up the total in and out and perform a balance.

Use PGM file to perform mass balance

To perform a mass balance of an element, we recommend using the Tag Select Class at the end of project solve, an example is given below. In this example we have used a Class. This allows it to be used for any valid element and also for multiple elements by declaring separate instances of the class.

Class ElementalBalance
  TagSelect Inputs, Outputs, Tears
  integer InCount@, OutCount@, i
  real FlowIn@("Qm","t/h"), FlowOut@("Qm","t/h"), Balance@("Qm","t/h"),RelError@
  bit OK@
  TextLabel("  Tears Balance")
  integer TearCount@
  real TearFlowIn@("Qm","t/h"), TearFlowOut@("Qm","t/h"), TearBalance@("Qm","t/h"),TearRelError@
  ; internal variables 
  string FlowTag, FlangeTag, Criteria, Element
 
  Sub Init(string req_element)
     Element = req_element
  EndSub
  
  Sub CalcBalance()
 
     ; find all true feeders (not connected) and makeup sources 
     Criteria = Concatenate("(([UnitType]=='FeederSink' AND [State]==1) OR ([UnitType] == 'MakeupSource')) AND [QProd.QEl.",Element,"]<>0")
     OK = Inputs.Exec(Criteria, false,false)
     ; find total number of inputs 
     InCount = Inputs.GetLen()
     ; sum all input flows 
     i=0
     FlowIn = 0
     while(i<InCount)
        FlowTag = Concatenate(Inputs.Tag(i),".QProd.QEl.",Element," (t/h)")
        FlowIn = FlowIn + [FlowTag]
        i = i + 1
     endwhile
     
     ; find all true sinks (not connected) 
     Criteria = Concatenate("([UnitType]=='FeederSink' AND [State]==2) AND [QProd.QEl.",Element,"]<>0")
     OK = Outputs.Exec(Criteria, false,false)
     ; find total number of outputs 
     OutCount = Outputs.GetLen()
     ; sum all output flows 
     i=0
     FlowOut = 0
     while(i<OutCount)
        FlowTag = Concatenate(Outputs.Tag(i),".QProd.QEl.",Element," (t/h)")
        FlowOut = FlowOut + [FlowTag]
        i = i + 1
     endwhile
     
     ; perform elemental mass balance 
     Balance = FlowIn - FlowOut
     RelError = Balance/Max(1.0e-12, Max(FlowIn, FlowOut))
    
  EndSub
 
  Sub CalcTearBalance()
    
     ; find all pipes with tears and element flow 
     Criteria = Concatenate("([UnitType]=='Pipe-1' AND ([Tear.State]==1) OR [Tear.State]==2)) AND [Qo.QEl.",Element,"]<>0")
     OK = Tears.Exec(Criteria, false,false)
     ; find total number of tears 
     TearCount = Tears.GetLen()
     ; sum all input and output flows 
     i=0
     TearFlowIn = 0
     TearFlowOut = 0
     while(i<TearCount)
        FlangeTag = [Concatenate(Tears.Tag(i),".Tear.Flange")]
        FlowTag = Concatenate(FlangeTag,".Qi.QEl.",Element," (t/h)")
        TearFlowIn = TearFlowIn + [FlowTag]
        FlowTag = Concatenate(FlangeTag,".Qo.QEl.",Element," (t/h)")
        TearFlowOut = TearFlowOut + [FlowTag]
        i = i + 1
     endwhile
     
     ; perform elemental mass balance 
     TearBalance = TearFlowIn - TearFlowOut
     TearRelError = TearBalance/Max(1.0e-12, Max(TearFlowIn, TearFlowOut))
    
  EndSub
 
EndClass
PageLabel("Fe")
  TextLabel(,"Iron Balance")
  ElementalBalance Iron
  real Overall_Fe_Balance@("Qm","t/h")

PageLabel("Cl")
  TextLabel(,"Chlorine Balance")
  ElementalBalance Chlorine
  real Overall_Cl_Balance@("Qm","t/h")

Sub PreStart() 
;--- Logic executed early before first iteration

; initialise classes by defining element of interest 
    Iron.Init("Fe")
    Chlorine.Init("Cl")
EndSub

Sub TerminateSolution() 
;--- Logic executed after last iteration (when solver is stopped)

; perform simple elemental balances (in - out) 
    Iron.CalcBalance()
    Chlorine.CalcBalance()
; perform tear balances (relevant if tears not converged) 
    Iron.CalcTearBalance()
    Chlorine.CalcTearBalance()
; calculate overall balances (including tears) 
    Overall_Fe_Balance = Iron.Balance - Iron.TearBalance
    Overall_Cl_Balance = Chlorine.Balance - Chlorine.TearBalance
EndSub

$ ; --- end of file ---