I have a Python script that iteratively computes permeate and retentate flows for a membrane separation unit. When I run this code in a Jupyter Notebook—where all variables (total flow, pressure, membrane area/thickness, permeabilities, etc.) are defined directly—it executes flawlessly and produces the expected membrane separation results. However, when I paste the same code into DWSIM’s Python Script Unit Operation, it immediately throws an Index was outside the bounds of the array error.
The code in “Membrane” can run in Jupyter notebook. Here is the code I use in DWSIM:
import math
from DWSIM import *
feed = ims1
T_list = list(feed.GetProp("temperature", "Overall", None, "", ""))
P_list = list(feed.GetProp("pressure", "Overall", None, "", ""))
T = T_list[0]
P = P_list[0]
n = int(feed.GetNumCompounds())
ids = feed.ComponentIds
for i, comp in enumerate(ids):
nm = comp.lower()
if "methane" in nm:
pmi[i] = 1 * 3.348e-10 # 1 GPU → 3.348×10⁻¹⁰ mol/(m²·s·Pa) :contentReference[oaicite:5]{index=5}
elif "carbon dioxide" in nm:
pmi[i] = 60 * 3.348e-10 # 60 GPU
for i in range(n):
px1[i] = infractions[i] * P # p₁ᵢ = xᵢ·P
px2[i] = px1[i] / 2.0 # 初始 p₂ᵢ = p₁ᵢ/2
error, count = 1.0, 0
while error > 1e-6 and count < 1000:
qt = 0.0
for i in range(n):
qi0[i] = qi[i]
qi[i] = pmi[i] * A / L * Nu * (px1[i] - px2[i]) / math.log(px1[i] / px2[i])
qt += qi[i]
for i in range(n):
yi[i] = qi[i] / qt
px2[i] = yi[i] * P
error = sum(abs(qi[i] - qi0[i]) for i in range(n))
count += 1
qtr = 0.0
for i in range(n):
qir[i] = inflows[i] - qi[i]
qtr += qir[i]
for i in range(n):
yir[i] = qir[i] / qtr
Hello,
I have a Python script that iteratively computes permeate and retentate flows for a membrane separation unit. When I run this code in a Jupyter Notebook—where all variables (total flow, pressure, membrane area/thickness, permeabilities, etc.) are defined directly—it executes flawlessly and produces the expected membrane separation results. However, when I paste the same code into DWSIM’s Python Script Unit Operation, it immediately throws an
Index was outside the bounds of the array
error.The code in “Membrane” can run in Jupyter notebook. Here is the code I use in DWSIM:
import math
from DWSIM import *
feed = ims1
T_list = list(feed.GetProp("temperature", "Overall", None, "", ""))
P_list = list(feed.GetProp("pressure", "Overall", None, "", ""))
T = T_list[0]
P = P_list[0]
n = int(feed.GetNumCompounds())
ids = feed.ComponentIds
raw_frac = list(feed.GetProp("fraction", "Overall", None, "", "mole"))
infractions = raw_frac[:n]
flow_tf = list(feed.GetProp("totalFlow", "Overall", None, "", "mole"))
if len(flow_tf) >= n:
inflows = flow_tf[:n]
else:
total_flow = flow_tf[0]
inflows = [ total_flow * infractions[i] for i in range(n)
px1 = [0.0]n
px2 = [0.0]n
qi = [0.0]n
qi0 = [0.0]n
yi = [0.0]n
qir = [0.0]n
yir = [0.0]n
pmi = [0.0]n
for i, comp in enumerate(ids):
nm = comp.lower()
if "methane" in nm:
pmi[i] = 1 * 3.348e-10 # 1 GPU → 3.348×10⁻¹⁰ mol/(m²·s·Pa) :contentReference[oaicite:5]{index=5}
elif "carbon dioxide" in nm:
pmi[i] = 60 * 3.348e-10 # 60 GPU
for i in range(n):
px1[i] = infractions[i] * P # p₁ᵢ = xᵢ·P
px2[i] = px1[i] / 2.0 # 初始 p₂ᵢ = p₁ᵢ/2
error, count = 1.0, 0
while error > 1e-6 and count < 1000:
qt = 0.0
for i in range(n):
qi0[i] = qi[i]
qi[i] = pmi[i] * A / L * Nu * (px1[i] - px2[i]) / math.log(px1[i] / px2[i])
qt += qi[i]
for i in range(n):
yi[i] = qi[i] / qt
px2[i] = yi[i] * P
error = sum(abs(qi[i] - qi0[i]) for i in range(n))
count += 1
qtr = 0.0
for i in range(n):
qir[i] = inflows[i] - qi[i]
qtr += qir[i]
for i in range(n):
yir[i] = qir[i] / qtr
per = oms1
per.Clear()
per.SetProp("temperature","Overall",None,"","", [T])
per.SetProp("pressure", "Overall",None,"","", [P])
per.SetProp("fraction", "Overall",None,"","mole", yi)
per.SetProp("totalFlow", "Overall",None,"","mole", [qt])
raf = oms2
raf.Clear()
raf.SetProp("temperature","Overall",None,"","", [T])
raf.SetProp("pressure", "Overall",None,"","", [P])
raf.SetProp("fraction", "Overall",None,"","mole", yir)
raf.SetProp("totalFlow", "Overall",None,"","mole", [qtr])