三标识符变量访问:反应系数

pfdtut 模拟中 RStoic 反应器模块 B2 的 Variable Explorer 树形视图如图所示:

image.png

【中文翻译】

在 RStoic 反应器模型中,反应的化学计量系数保存在输入变量 COEF(反应物系数)和 COEF1(产物系数)里。

这两个节点各有一组子节点,每个子节点代表一条反应方程式。 由于该模块只有一条反应,COEFCOEF1 均仅含一个子节点,标签为 "1",即反应编号 1。

该反应节点具有 二维Dimension = 2),因此需要两个标识符才能定位具体系数:

  1. 第一维标识符:组分(reactant component)

  2. 第二维标识符:子物流(此处为 MIXED

这是 成对滚动(paired scrolling) 的典型例子: 只有两维行号相同的节点才有效,其余为空。 是否采用成对滚动可通过属性 .AttributeValue(HAP_FIRSTPAIR) 判断——若返回 ≥1 的索引,即表示启用成对滚动。

下方示例代码演示如何在 COEF 节点下读取系数及其对应标识符; 因使用成对滚动,仅访问两维 Location 相同 的节点。

Sub ReacCoeffExample(ByVal ihAPsim As Happ.IHapp)
' This example illustrates retrieving values for a non-scalar variable
' with three identifiers
Dim ihReacNode As Happ.IHNode
Dim ihCoeffNode As Happ.IHNode
Dim intOff As Long
Dim strHeading As String
Dim strTable As String
Dim nReacCoeff As Integer
On Error GoTo ErrorHandler
strTable=""
ihCoeffNode = ihAPsim.Tree.Data.Blocks.B2.Input.COEF
' loop through reaction nodes
For Each ihReacNode In ihCoeffNode.Elements
  strHeading = ihCoeffNode.Elements.DimensionName(0) _
    & Chr(9) & ihReacNode.Elements.DimensionName(0) _
    & Chr(9) & ihReacNode.Elements.DimensionName(1)
  nReacCoeff = ihReacNode.Elements.RowCount(0)
  ' loop through coefficient nodes retrieving component and substream
  ' identifiers and coefficient values
  For intOff = 0 To nReacCoeff - 1
    strTable = strTable & Chr(13) & ihReacNode.Name & Chr(9) _
      & Chr(9) & ihReacNode.Elements.Label(0, intOff) & Chr(9) _
      & Chr(9) & ihReacNode.Elements.Label(1, intOff) & Chr(9) _
      & Chr(9) & ihReacNode.Elements.Item(intOff, intOff).Value
  Next intOff
  MsgBox(strHeading & strTable, , "ReacCoeffExample")
Next ihReacNode
Exit Sub
ErrorHandler:
MsgBox("ReacCoeffExample raised error " & Err.Description)
End Sub

Python翻译

import win32com.client as win32
from tkinter import Tk, messagebox

def reac_coeff_example(ih_ap_sim):
    """与 VB ReacCoeffExample 完全一致:三维非标量变量"""
    coeff_node = ih_ap_sim.Tree.Data.Blocks.B2.Input.COEF   # 反应物系数根节点

    for ih_reac_node in coeff_node.Elements:               # 通常只有 1 条反应
        # 表头:维度名称
        heading = "\t".join([
            coeff_node.Elements.DimensionName(0),
            ih_reac_node.Elements.DimensionName(0),
            ih_reac_node.Elements.DimensionName(1)
        ])

        n_coeff = ih_reac_node.Elements.RowCount(0)        # 第1维行数
        table_rows = []
        # 成对滚动:只取两维同序
        for off in range(n_coeff):
            comp_lbl   = ih_reac_node.Elements.Label(0, off)  # 组分
            subst_lbl  = ih_reac_node.Elements.Label(1, off)  # 子物流
            coeff_val  = ih_reac_node.Elements.Item(off, off).Value
            table_rows.append(f"{ih_reac_node.Name}\t\t{comp_lbl}\t\t{subst_lbl}\t\t{coeff_val}")

        # 弹窗显示
        root = Tk(); root.withdraw()
        messagebox.showinfo("ReacCoeffExample", f"{heading}\n" + "\n".join(table_rows))
        root.destroy()


# ---------------- 调用示例 ----------------
if __name__ == "__main__":
    ap = win32.Dispatch("AspenPlus.Document.34.0")
    ap.InitNew2()                 # 或 GetObject 打开已有文件
    reac_coeff_example(ap)
    ap.Quit()