Example PGM - Species Check

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: PGMs ➔ Example PGM Files ➔ Species Check

Related Links: Plant Model - Species


Overview

NOTE: This is provided only as an example of PGM code. This particular workflow (to list used/unused species) is available via built-in reports from the Plant Model - Species tab. The PGM examples below are not required to perform these calculations.

Use of PGM Classes and Functions

These PGM examples demonstrate the use of several useful pre-defined classes and functions:

  • Species Database Class - How to retrieve species data information
  • TagSelect - How to apply search criteria to select relevant models. In the examples below, this is used to find models belonging to the stream unit group (e.g. pipes and makeup streams).
  • String Array - How to store lists of string variables (e.g. species with/without flow).
  • Array - How to store values and perform calculations such as summation (see Example 2).
  • String Functions - How to perform string comparisons (see Example 3).

Adding the PGM to a Project

  • These PGMs are generic and can be added to any project, with the check performed at the end of the project solve.
  • For large projects (e.g. thousands of pipes and hundreds of species), it may be necessary to increase the PlantModel - FlwSolve - PGM_ExecuteTimeout value to allow enough time for the calculations to complete.
  • The species list display length may need to be adjusted if the number of species (with or without flow) exceeds 100.

Example 1 - Species with Flow List

REMINDER: The used species list can also be found on the Plant Model - Species tab.

The following PGM can be used to check if a certain species is used by the project (has flow within the project):

  • The PGM will search through each species in the project, if the species has flow it will be added to the "SpeciesFlowList" array.
  • The "SpeciesFlowList" array can be used to create stream reports, as it will help shorten the list of species.
PageLabel("CheckSpecies")
    TextLabel(, "  --Time out value may need to be adjusted for large projects--" )
    Real  PGM_time_out*("Time", "s")<<300>>
    TextLabel(, "   -----------Species-----------",)
    TagSelect SpeciesListWithFlow
    StrArray  SpeciesFlowList
    integer   speciesCount@,  HasFlowCount, speciesWithFlowCount@, i, j
    Checkbox  CriteriaOK
    string    Criteria, SpeciesName
    ; The follow watch function is used to set the species list display length.  User can adjust the value as needed.
    Watch SpeciesFlowList[All,100]@
  
Sub InitialisePreStart() 
    ["PlantModel.PGM_ExecuteTimeout (s)"] = PGM_time_out
EndSub 
	
Sub CheckSpecies()
     While i < speciesCount
          SpeciesName = SDB.SpShortName(i)
          Criteria = Concatenate("[UnitGroup]=='Stream' AND [Qo.QM.", SpeciesName, " (g/h)]>1 ")
          CriteriaOK = SpeciesListWithFlow.Exec(Criteria, false, false)
          HasFlowCount= SpeciesListWithFlow.GetLen()
          If HasFlowCount > 0
              SpeciesFlowList.SetAt(j,SpeciesName)
              j = j + 1
          Endif	
          i = i + 1
     EndWhile
     speciesWithFlowCount = j
EndSub

Sub TerminateSolution() 
     speciesCount = SDB.SpeciesCount()
     SpeciesFlowList.SetLen(speciesCount)
     i = 0
     j = 0
     CheckSpecies()
     Lognote("Finished Species Check")
EndSub

$ ; --- end of file ---

Example 2 - Species with No Flow List

REMINDER: The unused species list can also be found on the Plant Model - Species tab.

The following PGM can be used to check if a certain species is not used by the project (has no flow within the project):

  • The PGM will search through the project, and streams with total flow > some specified flow will be analysed.
  • For each species, the species flow for each pipe meeting the search criteria will be temporarily stored in an array.
  • At the end, if the sum of the array equals 0, then the species has no flow in the project, it will be added to the "SpeciesNoFlowList" array.
PageLabel("CheckNoFlowSpecies")
    TextLabel(, "  --Time out value may need to be adjusted for large projects--" )
    Real  PGM_time_out*("Time", "s")<<300>>
    TextLabel(, "   -----------Species-----------",)
    Array     SpeciesFlow
    Real      SpeciesFlow_Sum
    TagSelect SpeciesListFlowCheck
    StrArray  SpeciesNoFlowList
    integer   speciesCount@,  speciesNoFlowCount@, i, j
    Checkbox  CriteriaOK
    string    SpeciesFlowTag, SpeciesName
    ; The follow watch function is used to set the species list display length.  User can adjust the value as needed.
    Watch SpeciesNoFlowList[All,100]@

Sub InitialisePreStart() 
    ["PlantModel.PGM_ExecuteTimeout (s)"] = PGM_time_out
EndSub 	
	
Sub CheckSpecies()
     While i < speciesCount
          SpeciesName = SDB.SpShortName(i)
          CriteriaOK = speciesListFlowCheck.Exec("[UnitGroup]=='Stream' AND [Qo.Qm (g/h)]>1 ", false, false)
          SpeciesFlowTag = Concatenate(".Qo.QM.", SpeciesName, " (t/h)")
          speciesListFlowCheck.GetValues(SpeciesFlowTag, SpeciesFlow)
          SpeciesFlow_Sum = SpeciesFlow.Sum()
          If SpeciesFlow_Sum == 0
              SpeciesNoFlowList.SetAt(j,SpeciesName)
              j = j + 1
          Endif	
          i = i + 1
     EndWhile
     speciesNoFlowCount = j
EndSub

Sub TerminateSolution() 
     speciesCount = SDB.SpeciesCount()
     SpeciesNoFlowList.SetLen(speciesCount)
     i = 0
     j = 0
     CheckSpecies()
     Lognote("Finished Species Check")
EndSub

$ ; --- end of file ---

Example 3 - Comparing Lists

The following PGM can be used to check if a certain species is used (has flow) or not used (with no flow) by the project:

  • The PGM will search through each species in the project, if the species has flow it will be added to the "SpeciesFlowList" list.
  • The "SpeciesFlowList" list is then compared to the full species list, thus creating a "SpeciesNoFlowList" list by difference.
PageLabel("CheckSpecies")
    TextLabel(, "  --Time out value may need to be adjusted for large projects--" )
    Real  PGM_time_out*("Time", "s")<<300>>
    TextLabel(, "   -----------Species-----------",)
    TagSelect SpeciesListWithFlow
    StrArray  SpeciesFlowList, SpeciesNoFlowList
    integer   speciesCount@,  HasFlowCount, speciesWithFlowCount@,  speciesNoFlowCount@, i, j, k
    Checkbox  CriteriaOK
    string    Criteria, SpeciesName
    ; The follow watch function is used to set the species list display length.  User can adjust the value as needed.
    Watch SpeciesFlowList[All,100]@, SpeciesNoFlowList[All,100]@
 
Sub InitialisePreStart() 
    ["PlantModel.PGM_ExecuteTimeout (s)"] = PGM_time_out
EndSub 
	
Sub CheckSpecies()
     While i < speciesCount
          SpeciesName = SDB.SpShortName(i)
          Criteria = Concatenate("[UnitGroup]=='Stream' AND [Qo.QM.", SpeciesName, " (g/h)]>1 ")
          CriteriaOK = SpeciesListWithFlow.Exec(Criteria, false, false)
          HasFlowCount= SpeciesListWithFlow.GetLen()
          If HasFlowCount > 0
              SpeciesFlowList.SetAt(j,SpeciesName)
              j = j + 1
          Endif	
          i = i + 1
     EndWhile
     speciesWithFlowCount = j
EndSub

Sub NoFlowList()
     While i < speciesCount
          SpeciesName = SDB.SpShortName(i)
          if (StrCmp(SpeciesName,SpeciesFlowList.GetAt(j)) == 0)
          ; species has flow, check next species
               j = j + 1
          else
          ; species has no flow, add to no flow list
               SpeciesNoFlowList.SetAt(k,SpeciesName)
               k = k + 1
          endif
          i = i + 1
     EndWhile
EndSub

Sub TerminateSolution() 
     speciesCount = SDB.SpeciesCount()
     SpeciesFlowList.SetLen(speciesCount)
     i = 0
     j = 0
     CheckSpecies()
     speciesNoFlowCount = speciesCount - speciesWithFlowCount
     if speciesNoFlowCount > 0
          SpeciesNoFlowList.SetLen(speciesNoFlowCount)
          i = 0
          j = 0
          k = 0
          NoFlowList()
     endif
     Lognote("Finished Species Check")
EndSub

$ ; --- end of file ---

Example 4 - Retrieving Species Property Data

;======================================================================================
; Multi-species Density vs Solute Mass Fraction
; Columns:
;   Col 0 = Mass Fraction
;   Col 1 = H2SO4(aq)
;   Col 2 = HCl(aq)
;======================================================================================
PageLabel "Density Export"
TextLabel "Generate CSV via Matrix Class", ""
;---------------- USER INPUTS ----------------
CheckBox RunNow*
Real MF_Min*<<0.00>><0,1>
Real MF_Max*<<0.60>><0,1>
Real MF_Step*<<0.05>><0.000001,1>
String OutputFileName@
;---------------- INTERNALS ----------------
Long SpIndex_H2SO4@, SpIndex_HCl@
Real T_K@, mf@, rho_H2SO4@, rho_HCl@, temp@
Long nRows@, iRow@

Matrix DataMatrix
;======================================================================================
; LOGIC
;======================================================================================
Sub InitialiseSolution()
	OutputFileName = "Acid_Density.csv"
    ;---------------- Find species ----------------
    SpIndex_H2SO4 = SDB.FindSpecies("H2SO4(aq)")
    SpIndex_HCl   = SDB.FindSpecies("HCl(aq)")
    If (SpIndex_H2SO4 < 0)
        LogError("Species not found: H2SO4(aq)")
        RunNow = 0
    ElseIf (SpIndex_HCl < 0)
        LogError("Species not found: HCl(aq)")
        RunNow = 0
	Endif
EndSub

If (RunNow)
    ;---------------- Count rows ----------------
    temp = (MF_Max - MF_Min) / MF_Step
    nRows = temp + 0.5
    nRows = nRows + 1
    ;---------------- Allocate matrix ----------------
    DataMatrix.SetSize(nRows, 3)
    ;---------------- Populate matrix ----------------
    mf = MF_Min
    iRow = 0
    While (iRow < nRows)
        rho_H2SO4 = SDB.SpDensityMf(SpIndex_H2SO4, 298.15, 101.325, mf)
        rho_HCl   = SDB.SpDensityMf(SpIndex_HCl,   298.15, 101.325, mf)
        DataMatrix.Setat(iRow,0, mf)
        DataMatrix.Setat(iRow,1, rho_H2SO4)
        DataMatrix.Setat(iRow,2, rho_HCl)
        mf = mf + MF_Step
        iRow = iRow + 1
    EndWhile
    ;---------------- Export ----------------
    DataMatrix.Save(OutputFileName)
    LogNote(Concatenate("CSV written to: ", OutputFileName))
    RunNow = 0
EndIf

$   ; End file