PGM Example - Reaction Finder Classes and Functions

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: PGMs ➔ Example PGM Files ➔ Reaction Finder Classes and Functions

Example User Defined Classes
Set Species Splits Agitator
Power
Reaction
Finder
Aq Feed Conc
Calculator
Evaporation
Correlation
Bayer Liquor
Class
Check
Element Bal
Optimise Controllers
for sensitive PIDs
Relaxed Cross Page Connector

Related Links: Defining a Class, Defining a Function


Example PGM with ReactionFinder Classes and Functions

;========================================================================================================= 
; Project Used: Any with a Reaction Block.  
;========================================================================================================= 
; Description of the ReactionFinder Class function --- 
; This class frees the user from the problem of the reaction index changing when a new reaction  
; is added to the reaction file before the controlled reaction(s).  The "Init" function should be called 
; in the "InitialiseSolution" subroutine and this finds the index of the required reaction.  
; The "SetExtent" function then sets the extent of the reaction as a fraction. 
;==========================================================================================================  
 
Class Class_ReactionFinder
;--- input variables ------ 
 Textbreak
 String     ReqdUnit@{Tag}              ;the unit containing the reaction file
 String     ReqdReaction*               ;the actual reaction that will be controlled
 Real       ReqdExtent*("Frac","%")     ;the required reaction extent
 ;---- Output variables ------
 long       ReactionIndex@              ;the actual reaction index number in the unit
 long       totalNumReactions@          ;total number of reactions in the reaction file
 ;--- local variables ------
 String     NumReactions, ReactionName, RBReact, ReactionTag, errormsg
 long       Minlength, i, reactioncomp
 
 Sub Init()
  ReqdUnit = ClassTag()
  if (strlen(ReqdUnit) > 0)
    i = 1
    ReactionIndex = 0
    NumReactions = Concatenate(ReqdUnit, ".RB.NoOfReactions")
    totalNumReactions = [NumReactions]
      while (i <= totalNumReactions)
          ReactionName = Concatenate(ReqdUnit, ".RB.R", IntToStr(i), ".Reaction")
          RBReact = [ReactionName]
          Minlength = Min(strlen(RBReact),strlen(ReqdReaction))
            if (Minlength > 0)
               reactioncomp = StriCmp(Left(RBReact,Minlength), Left(ReqdReaction,Minlength))
                if (reactioncomp == 0)
                    ReactionTag = Concatenate(ReqdUnit, ".RB.R", IntToStr(i), ".Extent.Required (%)")
                    ReactionIndex = i
                    i = totalNumReactions + 1
                endif
            endif
          i = i + 1
      endwhile
      if (ReactionIndex == 0)
         errormsg = Concatenate("The reaction ", ReqdReaction, " not found in ", ReqdUnit)
         lognote(errormsg)
      endif
  endif
 EndSub      

 Sub SetExtent()
  if (ReactionIndex > 0)
      [ReactionTag] = ReqdExtent
  endif
 EndSub

EndClass
;The following is an example of pgm code using the ReactionFinder class to
;call a number of reactions.

>>C:\SysCADProjects\CommonFiles\ReactionFinderClass.pgm
PageLabel "Reaction Control"  
 
Class_ReactionFinder Cementation, NiCo_Precip

Sub InitialiseSolution()
	if IsEmpty(Cementation.ReqdReaction)
		Cementation.ReqdReaction = "CuSO4(aq) + NiS(s) <=> CuS(s) + NiSO4(aq)"
	Endif

	if IsEmpty(NiCo_Precip.ReqdReaction)
		NiCo_Precip.ReqdReaction = "NiSO4(aq) + MgO(s) <=> NiO(s) + MgSO4(aq)"
	Endif
	
	ForEachClass(Class_ReactionFinder, Init() )
EndSub

	ForEachClass(Class_ReactionFinder, SetExtent() )

EndFile

ReactionFinder.png