User Variable Script Examples

  1. Getting and setting the value of a user variable
  2. Set the temperature of a stream three different ways
  3. Set the stream temperature using JavaScript instead of VBScript
  4. Dew point calculation
  5. Change the "Use Calibration Factors From" setting inside a DHTR reactor depending on the flow rate of the recycle stream
  6. Copy spreadsheet cells
  7. Get a spreadsheet cell value
  8. Sleeping
  9. WScript object
  10. Calculate a value upon change only

1 Getting and setting the value of a user variable

JavaScript:

//language="JavaScript"
var1 = GetUserVariable("MyUserVar1");
value = var1.Variable.Value;
value = value + 1;
result = value;
resultunits = "m3"

or VBScript:

Dim var
Set var = GetUserVariable("MyUserVar1")
Dim value
value = var.Variable.Value
value = value + 1
result = value

2 Set the temperature of a stream three different ways

language="JavaScript";
Temperature.Value = 45; //Set a stream temperature in Celcius

Temperature.SetValue(45,"C");

bd = MyBackDoor().BackDoorVariable(":Temperature.501.0").Variable;
bd.value = 45; //'Set it via the back door

or VBScript:

Temperature.Value = 45 'Set a stream temperature in Celcius

Temperature.SetValue 45,"C"

Dim bd
set bd = MyBackDoor.backdoorvariable(":Temperature.501.0").Variable
bd.Value = 45 'Set it via the back door

3 Set the stream temperature using JavaScript instead of VBScript

language="JavaScript";
Temperature.SetValue(47,"C");

4 Dew point calculation

language="JavaScript";
if (MolarEnthalpyValue != -32767) //Make sure the stream has solved
   {
   myFluid = DuplicateFluid();
   FS = myFluid.PVFlash(PressureValue, 1.0);
   if (FS == 0) //fsFlashOK
      {
      result = myFluid.Temperature.GetValue("C");
      resultunits = "C";
      }
   }

or VBScript:

if enthalpvalue <> -32767 then 'Make sure the stream has solved
dim myFluid
dim FS
set myFluid = DuplicateFluid
FS = myFluid.PVFlash(PressureValue, 1.0)
If FS = 0 Then ' fsFlashOK
 result = myFluid.Temperature.GetValue("C")
 resultunits = "C"
end if
end if

5 Change the "Use Calibration Factors From" setting inside a DHTR reactor depending on the flow rate of the recycle stream

Dim doc
Set doc = Application.ActiveDocument

Dim recStream
Set recStream = doc.GetObjectInCase("RecycleStream")
massflow = recStream.MassFlow.GetValue("t/h")

if massflow <> -32767 then  'Is the value known?

  Dim getFromObj
  if massflow > 100 then
    Set getFromObj = doc.GetObjectInCase(“Reactor A")
  else
    Set getFromObj = doc.GetObjectInCase("Reactor B")
  end if

  Dim bd
  Set bd = MyBackDoor

  Dim var
  Set var = bd.BackDoorVariable(":KHunitoperation.600").Variable

  if var.Object <> getFromObj then
    var.Object = getFromObj
  end if

end if

6 Copy spreadsheet cells

  dim acase, ss, i, c, cu, c2
  set acase = application.activedocument
  set ss = flowsheet.operations.item("SPRDSHT-1")
  for i = 3 to 51
    set c = ss.Cell("B" & i)
    cu = ss.Cell("B" & i).units
    application.trace cu, false
    set c2 = ss.cell("D" & i)
    c2.variabletype = -1
    c2.cellvalue = c.cellvalue
    c2.variabletype = c.variabletype
  next

7 Get a spreadsheet cell value

language="JavaScript";
//Get hold of the spreadsheet.
//If the spreadsheet is in the same flowsheet, do this:
sprd = FlowSheet.Operations.Item("Economics");
//Or this:
sprd = SimulationCase.GetObjectInCase("Economics");
value = sprd.Cell("A3").CellValue;
//Set the result:
result=value;

or VBScript:

'Get hold of the spreadsheet.
Dim sprd
'if the spreadsheet is in the same flowsheet, do this:
Set sprd = FlowSheet.Operations.Item("Economics")
'Or this:
Set sprd = SimulationCase.GetObjectInCase("Economics")
Dim value
value = sprd.Cell("A3").CellValue
'Set the result:
result=value

8 Sleeping

var sh = new ActiveXObject("WScript.Shell");
sh.Run("cmd /c ping -n 2 127.0.0.1 > nul", 0, true);

or VBScript:

CreateObject("WScript.Shell").Run "timeout /t 5", 0, True
CreateObject("WScript.Shell").Run "cmd /c ping -n 2 127.0.0.1 > nul", 0, True

This sleeps for roughly 1 second, because: ping -n 2 sends 2 pings → (n-1) = 1 second delay.

9 WScript object

Petro-SIM version 7.7 and later provide a WScript object that can be used in scripts inside Petro-SIM to access common functions like Sleep or Echo (which print to the trace window). The WScript object implements the same methods as the Windows Script Host WScript object. Echo sends information to the trace window.

VBScript:

WScript.echo WScript.Path
WScript.echo WScript.Name
WScript.echo WScript.FullName
WScript.echo WScript.Version

WScript.sleep 100
WScript.echo "Wow",2,"Two"

WScript.echo WScript.Path
WScript.echo WScript.Name
WScript.echo WScript.FullName
WScript.echo WScript.Version

Set fso = WScript.CreateObject("Scripting.FileSystemObject")
If Not fso Is Nothing Then
    WScript.Echo "Created FSO successfully"
    WScript.Echo "Windows folder path: " & fso.GetSpecialFolder(0)
Else
    WScript.Echo "Failed to create FSO"
End If

Set xl = WScript.GetObject(, "Excel.Application")   ' attach to running instance
If Err.Number <> 0 Then
    WScript.Echo "GetObject failed: " & Err.Description
    Err.Clear
Else
    WScript.Echo "Attached to Excel successfully"
    WScript.Echo "Excel Version: " & xl.Version
End If

10 Calculate a value upon change only

Inside the unit operation create two user variables one called say D12 of variable type Real, Unit Type say Ratio, set to trigger a solve. Have the spreadsheet export to this user variable value. Now create a second user variable which will compare the value in D12 with the one inside the unit operation and only update the value inside the unit operation if different.

The set up a second user variable to compare the value in D12 versus the one inside the unit operation and update the one inside the unit operation, only if different, trigger to run presolve. This avoids an infinite solve loop.

The code for it is as follows:

'Get the value of the D12 user var
Set d12 = GetUserVariable("D12")
value1 = d12.Variable.Value
'Get the current value
set bd = MyBackDoor.backdoorvariable(":Ratio.550.0").Variable
value2 = bd.Value
if value1<>value2 and value1<>-32767 and value2 <>-32767 then
   bd.Value = value1
end if