Table lookup using Array and Matrix 9.2

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: PGMs - Example PGM Files 9.2

Related Links: Matrix Class, Array Class

This Page uses SysCAD 9.2 Syntax, for SysCAD 9.3 Syntax, please see Table lookup using Array and Matrix

PGM FILE - Look up Gypsum Solubility from a Table using SysCAD 9.2 Syntax

This example shows how to look up the Gypsum solubility from a table based on Temperature and acid concentration.

A example table may look like this, note this data is for PGM demonstration purpose only, please use with caution.

Gypsum Solubility (g/L) Temperature (C)
Acid Conc (g/L) 25 30 35
0 2.12 2.11 2.10
9.8 1.77 1.82 1.88

If we have a solution at 28 dC, and acid concentration at 5 g/L, we want to look up the gypsum solubility from the above table.

  • The above table only gives temperature range in 5 deg increments, so we will use the 30 dC column. At 30 dC, we have values for gypsum solubility for acid concentration of 0 g/L and 9.8 g/L. We will use linear interpolation to obtain the solubility value at acid concentration at 5 g/L.
  • The result is therefore 2.11 + (1.82 - 2.11) * (5 - 0)/(9.8-0) = 1.96 g/L

To automate the reading of the table and interpreting data in a SysCAD PGM, we can do the following:

  1. Create a data file to represent the temperature segments. Eg: Temperature.csv (This file will be stored in the project folder)
  2. Create a data file to represent the acid concentration segments. Eg: Acid.csv (This file will be stored in the project folder)
  3. Create a data file to represent the gypsum solubility table. Eg: Gypsum solubility model.csv (This file will be stored in the project folder)
  4. Create two arrays and read in the Row heading (acid concentration) and column heading (temperature segments)
  5. Create a matrix to read in the Gypsum solubility values.
  6. Match the current/required temperature to the nearest temperature segment
  7. Match the current/required acid concentration to obtain the gypsum solubility values from the table.
  8. Perform linear interpolation to obtain the gypsum solubility.

The three csv file may look like this:

Temperature.csv Acid.csv Gypsum solubility model.csv
25
30
35
0
9.8
2.12 , 2.11 , 2.10
1.77 , 1.82 , 1.88

EXAMPLE PGM file that does the above is listed below:

;--- SysCAD General Controller (PGM) file ---
; Revision: 1     Date:  29 May 2014   Author: Kenwalt

;--- variable declarations ---

REAL    CurrentTemp@("T", "C"), TemperatureSegment@("T", "C")
REAL    CurrentAcidConc@("Conc", "g/L"), AcidConcPt1@("Conc", "g/L"), AcidConcPt2@("Conc", "g/L")
REAL    GypsumSol@("Conc", "g/L"), GypsumSolPt1@("Conc", "g/L"), GypsumSolPt2@("Conc", "g/L")

Array	Temperature, AcidConc
matrix  GypsumSolubility

LONG    RowCount@, ColCount@, TemperatureIndex@, AcidIndex@
BIT	MatrixOK@, TemperatureOK@, AcidConcOK@
BYTE    i, j 

REAL    modTemp, temp, temp1

if (OnInitialise)
 ;Loads in the Temperature Range (Column Heading), Acid Range (Row Heading) as arrays
 TemperatureOK = Temperature.Load("$Prj\Temperature.csv")
 AcidConcOK = AcidConc.Load("$Prj\Acid.csv")
 ;Loads in the Gypsum solubility table (less column and row headings), number of rows and columns should match the two arrays above.
 MatrixOK = GypsumSolubility.Load("$Prj\Gypsum solubility model.csv")
 RowCount = GypsumSolubility.GetRowCount()
 ColCount = GypsumSolubility.GetColCount()
  
else
 ; Get the current temperature and round it to the closest value of multiple 5
 CurrentTemp = GetTag("P_003.To (C)")
 modTemp = MOD(CurrentTemp, 5)
 TemperatureSegment= IIF(modTemp>=2.500001, ROUND(CurrentTemp+(5-modTemp)), ROUND(CurrentTemp-modTemp))
 ;limits the temperature to the maximum T defined in the array
 TemperatureSegment= MIN(Temperature.GetAt(ColCount-1), TemperatureSegment) 
 
 ;Match the current temperature to the temperature index number (column number)
 i = 0
 While i < ColCount
       temp = Temperature.GetAt(i)
       If temp == TemperatureSegment
          TemperatureIndex = i
          i=ColCount
       endif	
       i = i + 1
 Endwhile

 ;Match the current acid concentration to the acid index number (row number)
 CurrentAcidConc = GetTag("P_003.Qo.CMC:L.H2SO4 (g/L)")
 j = 0
 While j < RowCount
       temp1 = AcidConc.GetAt(j)
        If temp1 >= CurrentAcidConc
           AcidIndex = j
           j=RowCount
        endif	
       j = j + 1
 Endwhile

 ;Get Gypsum Solubility using the Temperature and acid Index, and apply interpolation
 GypsumSolPt1 = GypsumSolubility.GetAt(AcidIndex-1, TemperatureIndex)
 GypsumSolPt2 = GypsumSolubility.GetAt(AcidIndex, TemperatureIndex)
  
 AcidConcPt1 = AcidConc.GetAt(AcidIndex-1)
 AcidConcPt2 = AcidConc.GetAt(AcidIndex)
  
 GypsumSol = GypsumSolPt1 + (GypsumSolPt2 - GypsumSolPt1) * ((CurrentAcidConc - AcidConcPt1)/(AcidConcPt2-AcidConcPt1))
  
endif
$  ; --- end of file ---