Name
Category1
Category2
Scope
Snippet
Hello World
IronPython
Basics
Flowsheet/User Model
print "Hello World!"
Multi-Line Statements
IronPython
Basics
Flowsheet/User Model
def add(a, b):
return a + b
x = add(3, 2)
print x
y = add("Iron", "Python")
print y
Using the standard .NET libraries
IronPython
.NET Integration
Flowsheet/User Model
import System
dir(System.Environment)
print System.Environment.OSVersion
print System.Environment.CommandLine
Imports content of a class
IronPython
.NET Integration
Flowsheet/User Model
from System.Math import *
print dir()
print Sin(PI/2)
Working with .NET classes
IronPython
.NET Integration
Flowsheet/User Model
from System.Collections import *
h = Hashtable()
print dir(h)
h["a"] = "IronPython"
h["b"] = "Tutorial"
print h["a"]
for e in h: print (e.Key + ": " + e.Value)
Initializing collections with Python lists
IronPython
.NET Integration
Flowsheet/User Model
from System.Collections import *
l = ArrayList([1,2,3])
for i in l: print i
s = Stack((1,2,3))
while s.Count: s.Pop()
Using Generics
IronPython
.NET Integration
Flowsheet/User Model
from System.Collections.Generic import *
l = List[str]()
l.Add("Hello")
l.Add("Hi")
for i in l: print i
Loading .NET libraries
IronPython
.NET Integration
Flowsheet/User Model
import clr
clr.AddReference("System.Xml")
from System.Xml import *
print dir()
Loading the Mapack library
IronPython
.NET Integration
Flowsheet/User Model
import clr
clr.AddReference("Mapack")
from Mapack import *
print dir()
m = Matrix(2, 2, 1.2)
n = Matrix(2,1)
n[0,0] = 4
print m
print n
print m * n
print n.Transpose() * m
print m * 3
Get Current Directory
IronPython
Using Python Standard Library
Flowsheet/User Model
from System.IO import Directory, Path
lpath = Path.Combine(Directory.GetCurrentDirectory(), "Lib")
import sys
sys.path.append(lpath)
import os
print os.getcwd()
Get Current Directory
IronPython
.NET Integration
Flowsheet/User Model
from System.IO import Directory
print Directory.GetCurrentDirectory()
Using Windows.Forms
IronPython
Advanced
Flowsheet/User Model
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import *
from System.Drawing import *
f = Form()
f.Text = "My First Interactive Application"
def click(f, a):
l = Label(Text = "Hello")
l.Location = a.Location
f.Controls.Add(l)
f.Click += click
f.Show()
Use Word for Spell Checking
IronPython
Advanced
Flowsheet/User Model
# Import clr module and add a reference to the Word COM interop assembly. Also, import System so that we can use a special value from it later.
import clr
clr.AddReferenceByPartialName("Microsoft.Office.Interop.Word")
from Microsoft.Office.Interop.Word import ApplicationClass
import System
# Start an instance of Word as a COM server running. You won't see it show up since it is hidden, but you can see it in the Windows Task Manager by typing ctrl-shift-escape and looking
# for the WINWORD.EXE process.
w = ApplicationClass()
# Define the following function to check the spelling. We have to build up an argument list so that we can supply the 12 optional arguments we do not care about (System.Type.Missing).
# We get the answer by taking the first (at index zero) of a few return values (out parameters in COM interop). Remember to indent the lines of the function's body extra spaces, and you
# have to hit an extra return or enter to complete the function's definition.
def check_word (word):
args = [word] + [System.Type.Missing]*12
return w.CheckSpelling(*args)
print check_word("foo")
print check_word("food")
# You can try that out on a couple of words, but now lets define a function that will suggest corrections for us. First, we need to add a document so that we can call GetSpellingSuggestions(),
# which gives a nice error message if you try to call it with no documents opened.
w.Documents.Add(*[System.Type.Missing]*4)
# The function we'll define builds an argument list just like check_word() did to supply several unneeded optional arguments. The first result of several return values from GetSpellingSuggestions()
# is a collection of items, each of which is a correction suggestion. We use a Python list comprehension to iterate through the COM collection object and call the Name property on each item object
# in the collection. Each item's Name property is a string that Word is suggesting as a correct spelling.
def suggestions (word):
args = [word] + [System.Type.Missing]*13
res_objects = w.GetSpellingSuggestions(*args)
return [x.Name for x in res_objects]
print suggestions("foo")
print suggestions("food")
# Now, let's shut down Word.
w.Quit(*[System.Type.Missing]*3)
Create, connect and manipulate objects
DWSIM
Advanced
Flowsheet
import clr
clr.AddReference('DWSIM.Interfaces')
from DWSIM import Interfaces
cooler = Flowsheet.AddObject(Interfaces.Enums.GraphicObjects.ObjectType.Cooler, 100, 100, 'COOLER-001')
heat_out = Flowsheet.AddObject(Interfaces.Enums.GraphicObjects.ObjectType.EnergyStream, 130, 150, 'HEAT_OUT')
inlet = Flowsheet.AddObject(Interfaces.Enums.GraphicObjects.ObjectType.MaterialStream, 50, 100, 'INLET')
outlet = Flowsheet.AddObject(Interfaces.Enums.GraphicObjects.ObjectType.MaterialStream, 150, 100, 'OUTLET')
cooler.GraphicObject.CreateConnectors(1, 1)
inlet.GraphicObject.CreateConnectors(1, 1)
outlet.GraphicObject.CreateConnectors(1, 1)
heat_out.GraphicObject.CreateConnectors(1, 1)
Flowsheet.ConnectObjects(inlet.GraphicObject, cooler.GraphicObject, 0, 0)
Flowsheet.ConnectObjects(cooler.GraphicObject, outlet.GraphicObject, 0, 0)
Flowsheet.ConnectObjects(cooler.GraphicObject, heat_out.GraphicObject, 0, 0)
# get inlet properties
inlet_properties = inlet.GetPhase('Overall').Properties
inlet_properties.temperature = 400 # K
inlet_properties.pressure = 1000000 # Pa
inlet_properties.massflow = 30 # kg/s
# the following will define all compound mole fractions to the same value so the sum is equal to 1
inlet.EqualizeOverallComposition()
# set the cooler's outlet temperature to 300 K
# http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_UnitOperations_UnitOperations_Cooler.htm
cooler.OutletTemperature = 300
# set the cooler's calculation mode to 'outlet temperature'
# http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_UnitOperations_UnitOperations_Cooler_CalculationMode.htm
clr.AddReference('DWSIM.UnitOperations')
from DWSIM import UnitOperations
cooler.CalcMode = UnitOperations.UnitOperations.Cooler.CalculationMode.OutletTemperature
#calculate the flowsheet
Flowsheet.RequestCalculation(None, False)
#get the outlet stream temperature and cooler's temperature decrease
deltat = cooler.DeltaT
heat_flow = heat_out.EnergyFlow
print('Cooler Temperature Drop (K):'+ str(deltat))
print('Heat Flow (kW): ' + str(heat_flow))
Getting a reference to a Compound in the simulation
DWSIM
Advanced
Flowsheet/User Model
mycompound = Flowsheet.SelectedCompounds['Methane']
mycompound2 = Flowsheet.GetSimulationObject['MSTR-001'].Phases[0].Compounds['Methane']
Executing a script from another tab/section
DWSIM
Advanced
Flowsheet
import clr
import System
from System import *
clr.AddReference('System.Core')
clr.ImportExtensions(System.Linq)
# get the script text from "Functions" using LINQ
source = Flowsheet.Scripts.Values.Where(lambda x: x.Title == 'Functions').FirstOrDefault().ScriptText.replace('\r', '')
# execute the script
exec(source)
Setting the properties of a Material Stream
DWSIM
Advanced
Flowsheet/User Model
ms1 = Flowsheet.GetFlowsheetSimulationObject('MSTR-001')
overall = ms1.GetPhase('Overall')
overall.Properties.temperature = 200 # set temperature to 200 K
overall.Properties.pressure = 101325 # set pressure to 101325 Pa
overall.Properties.massflow = 14 # set mass flow to 14 kg/s
Getting Surface Tension and Diffusion Coefficients from a Material Stream
DWSIM
Advanced
Flowsheet/User Model
import clr
import System
# get feed's interfacial tension - method 1
mixphase = feed.GetPhase("Mixture")
sftens = mixphase.Properties.surfaceTension
print str(sftens) + " N/m"
# get feed's interfacial tension - method 2
sftens2 = clr.Reference[System.Object]()
feed.GetTwoPhaseProp("surfacetension", None, "", sftens2)
print str(sftens2.Value[0]) + " N/m"
# diffusion coefficients
phase = feed.GetPhase("Vapor")
compound = phase.Compounds["Methane"]
difc = compound.DiffusionCoefficient
print str(difc) + " m2/s"
Override Mixer Model
DWSIM
Model Customization
Flowsheet
# for more details, go to https://dwsim.org/wiki/index.php?title=Model_Customization
import clr
clr.AddReference('DWSIM.MathOps')
clr.AddReference('DWSIM.UnitOperations')
clr.AddReference('DWSIM.Interfaces')
from DWSIM import *
from DWSIM.Thermodynamics.Streams import *
from DWSIM.UnitOperations import *
from DWSIM.MathOps.MathEx import *
from System import *
from System.Collections.Generic import *
# gets the mixer object
mixer = Flowsheet.GetFlowsheetSimulationObject('MIX-004')
def CalcMixer():
ms = MaterialStream()
P = 0.0
W = 0.0
H = 0.0
i = 1
for cp in mixer.GraphicObject.InputConnectors:
if cp.IsAttached:
ms = Flowsheet.SimulationObjects[cp.AttachedConnector.AttachedFrom.Name]
ms.Validate()
if mixer.PressureCalculation == UnitOperations.Mixer.PressureBehavior.Minimum:
print '[' + mixer.GraphicObject.Tag + '] ' + 'Mixer Mode: Outlet Pressure = Minimum Inlet Pressure'
if ms.Phases[0].Properties.pressure < P:
P = ms.Phases[0].Properties.pressure
elif P == 0.0:
P = ms.Phases[0].Properties.pressure
elif mixer.PressureCalculation == UnitOperations.Mixer.PressureBehavior.Maximum:
print '[' + mixer.GraphicObject.Tag + '] ' + 'Mixer Mode: Outlet Pressure = Maximum Inlet Pressure'
if ms.Phases[0].Properties.pressure > P:
P = ms.Phases[0].Properties.pressure
elif P == 0:
P = ms.Phases[0].Properties.pressure
else:
print '[' + mixer.GraphicObject.Tag + '] ' + 'Mixer Mode: Outlet Pressure = Inlet Average'
P += ms.Phases[0].Properties.pressure
i += 1
We = ms.Phases[0].Properties.massflow
W += We
if not Double.IsNaN(ms.Phases[0].Properties.enthalpy): H += We * ms.Phases[0].Properties.enthalpy
if W != 0.0:
Hs = H / W
else:
Hs = 0.0
if mixer.PressureCalculation == UnitOperations.Mixer.PressureBehavior.Average: P = P / (i - 1)
print '[' + mixer.GraphicObject.Tag + '] ' + 'Mixture Pressure (Pa): ' + str(P)
print '[' + mixer.GraphicObject.Tag + '] ' + 'Mixture Mass Flow (kg/s): ' + str(W)
print '[' + mixer.GraphicObject.Tag + '] ' + 'Mixture Enthalpy (kJ/kg): ' + str(Hs)
T = 0.0
n = Flowsheet.SelectedCompounds.Count
Vw = Dictionary[String, Double]()
for cp in mixer.GraphicObject.InputConnectors:
if cp.IsAttached:
ms = Flowsheet.SimulationObjects[cp.AttachedConnector.AttachedFrom.Name]
for comp in ms.Phases[0].Compounds.Values:
if not Vw.ContainsKey(comp.Name):
Vw.Add(comp.Name, 0)
Vw[comp.Name] += comp.MassFraction * ms.Phases[0].Properties.massflow
if W != 0.0: T += ms.Phases[0].Properties.massflow / W * ms.Phases[0].Properties.temperature
if W == 0.0: T = 273.15
print '[' + mixer.GraphicObject.Tag + '] ' + 'Mixture Temperature Estimate (K): ' + str(T)
omstr = Flowsheet.SimulationObjects[mixer.GraphicObject.OutputConnectors[0].AttachedConnector.AttachedTo.Name]
omstr.Clear()
omstr.ClearAllProps()
if W != 0.0: omstr.Phases[0].Properties.enthalpy = Hs
omstr.Phases[0].Properties.pressure = P
omstr.Phases[0].Properties.massflow = W
omstr.Phases[0].Properties.molarfraction = 1
omstr.Phases[0].Properties.massfraction = 1
for comp in omstr.Phases[0].Compounds.Values:
if W != 0.0: comp.MassFraction = Vw[comp.Name] / W
mass_div_mm = 0.0
for sub1 in omstr.Phases[0].Compounds.Values:
mass_div_mm += sub1.MassFraction / sub1.ConstantProperties.Molar_Weight
for sub1 in omstr.Phases[0].Compounds.Values:
if W != 0.0:
sub1.MoleFraction = sub1.MassFraction / sub1.ConstantProperties.Molar_Weight / mass_div_mm
else:
sub1.MoleFraction = 0.0
print '[' + mixer.GraphicObject.Tag + '] ' + sub1.Name + ' outlet molar fraction: ' + str(sub1.MoleFraction)
omstr.Phases[0].Properties.temperature = T
omstr.SpecType = Interfaces.Enums.StreamSpec.Pressure_and_Enthalpy
print '[' + mixer.GraphicObject.Tag + '] ' + 'Outlet Stream variables set successfully.'
return None
mixer.OverrideCalculationRoutine = True
mixer.CalculationRoutineOverride = CalcMixer
Override Property Package Fugacity Coefficients Calculation
DWSIM
Model Customization
Flowsheet
# for more details, go to https://dwsim.org/wiki/index.php?title=Model_Customization
import clr
clr.AddReference('DWSIM.MathOps')
from DWSIM import *
from DWSIM.MathOps.MathEx import *
from DWSIM.Thermodynamics.PropertyPackages import *
from System import *
# gets the first Property Package added to the the simulation
pp = Flowsheet.PropertyPackagesArray[0]
def calcroots(coeffs):
# auxiliary function
# calculates the roots of of a cubic polynomial and returns only the real ones
a = coeffs[0]
b = coeffs[1]
c = coeffs[2]
d = coeffs[3]
# uses DWSIM's internal 'CalcRoots' function to calculate roots
# https://github.com/DanWBR/dwsim5/blob/windows/DWSIM.Math/MATRIX2.vb#L29
res = PolySolve.CalcRoots(a, b, c, d)
roots = [[0] * 2 for i in range(3)]
roots[0][0] = res[0, 0]
roots[0][1] = res[0, 1]
roots[1][0] = res[1, 0]
roots[1][1] = res[1, 1]
roots[2][0] = res[2, 0]
roots[2][1] = res[2, 1]
# orders the roots
if roots[0][0] > roots[1][0]:
tv = roots[1][0]
roots[1][0] = roots[0][0]
roots[0][0] = tv
tv2 = roots[1][1]
roots[1][1] = roots[0][1]
roots[0][1] = tv2
if roots[0][0] > roots[2][0]:
tv = roots[2][0]
roots[2][0] = roots[0][0]
roots[0][0] = tv
tv2 = roots[2][1]
roots[2][1] = roots[0][1]
roots[0][1] = tv2
if roots[1][0] > roots[2][0]:
tv = roots[2][0]
roots[2][0] = roots[1][0]
roots[1][0] = tv
tv2 = roots[2][1]
roots[2][1] = roots[1][1]
roots[1][1] = tv2
validroots = []
if roots[0][1] == 0 and roots[0][0] > 0.0: validroots.append(roots[0][0])
if roots[1][1] == 0 and roots[1][0] > 0.0: validroots.append(roots[1][0])
if roots[2][1] == 0 and roots[2][0] > 0.0: validroots.append(roots[2][0])
# returns only valid real roots
return validroots
def fugcoeff(Vz, T, P, state):
# calculates fugacity coefficients using PR EOS
# Vx = composition vector in molar fractions
# T = temperature in K
# P = Pressure in Pa
# state = mixture state (Liquid, Vapor or Solid)
R = 8.314
n = len(Vz)
Tc = pp.RET_VTC() # critical temperatures
Pc = pp.RET_VPC() # critical pressures
w = pp.RET_VW() # acentric factors
alpha = [0] * n
ai = [0] * n
bi = [0] * n
for i in range(n):
alpha[i] = (1 + (0.37464 + 1.54226 * w[i] - 0.26992 * w[i] ** 2) * (1 - (T / Tc[i]) ** 0.5)) ** 2
ai[i] = 0.45724 * alpha[i] * R ** 2 * Tc[i] ** 2 / Pc[i]
bi[i] = 0.0778 * R * Tc[i] / Pc[i]
a = [[0] * n for i in range(n)]
# get binary interaction parameters (BIPs/kijs) from PR Property Package
kij = [[0] * n for i in range(n)]
vkij = pp.RET_VKij()
for i in range(n):
for j in range(n):
kij[i][j] = vkij[i, j]
a[i][j] = (ai[i] * ai[j]) ** 0.5 * (1 - kij[i][j]) # <- default mixing rule for amix
amix = 0.0
bmix = 0.0
amix2 = [0] * n
for i in range(n):
for j in range(n):
amix += Vz[i] * Vz[j] * a[i][j] # <- default mixing rule for amix
amix2[i] += Vz[j] * a[j][i]
for i in range(n):
bmix += Vz[i] * bi[i] # <- default mixing rule - no interaction parameters for bmix
AG = amix * P / (R * T) ** 2
BG = bmix * P / (R * T)
coeff = [0] * 4
coeff[0] = 1
coeff[1] = BG - 1
coeff[2] = AG - 3 * BG ** 2 - 2 * BG
coeff[3] = -AG * BG + BG ** 2 + BG ** 3
roots = calcroots(coeff) # <- get the real roots of the cubic equation
# compressibility factor = cubic equation's root
if state == State.Liquid:
# liquid
Z = min(roots)
else:
# vapor
Z = max(roots)
# gets a special zeroed vector from the property package because DWSIM requires a
# .NET array as the returning value, not a Python one
fugcoeff = pp.RET_NullVector()
for i in range(n):
t1 = bi[i] * (Z - 1) / bmix
t2 = -Math.Log(Z - BG)
t3 = AG * (2 * amix2[i] / amix - bi[i] / bmix)
t4 = Math.Log((Z + (1 + 2 ** 0.5) * BG) / (Z + (1 - 2 ** 0.5) * BG))
t5 = 2 * 2 ** 0.5 * BG
fugcoeff[i] = Math.Exp(t1 + t2 - (t3 * t4 / t5))
# returns calculated fugacity coefficients
print 'calculated fugacities = ' + str(fugcoeff) + ' (' + str(state) + ')'
return fugcoeff
# activate fugacity calculation override on PR Property Package
pp.OverrideKvalFugCoeff = True
# set the function that calculates the fugacity coefficient
pp.KvalFugacityCoefficientOverride = fugcoeff
Override Property Package Enthalpy Calculation
DWSIM
Model Customization
Flowsheet
# for more details, go to https://dwsim.org/wiki/index.php?title=Model_Customization
import clr
clr.AddReference('DWSIM.MathOps')
from DWSIM import *
from DWSIM.MathOps.MathEx import *
from DWSIM.Thermodynamics.PropertyPackages import *
from System import *
# gets the first Property Package added to the the simulation
pp = Flowsheet.PropertyPackagesArray[0]
def calcroots(coeffs):
# auxiliary function
# calculates the roots of of a cubic polynomial and returns only the real ones
a = coeffs[0]
b = coeffs[1]
c = coeffs[2]
d = coeffs[3]
# uses DWSIM's internal 'CalcRoots' function to calculate roots
# https://github.com/DanWBR/dwsim5/blob/windows/DWSIM.Math/MATRIX2.vb#L29
res = PolySolve.CalcRoots(a, b, c, d)
roots = [[0] * 2 for i in range(3)]
roots[0][0] = res[0, 0]
roots[0][1] = res[0, 1]
roots[1][0] = res[1, 0]
roots[1][1] = res[1, 1]
roots[2][0] = res[2, 0]
roots[2][1] = res[2, 1]
# orders the roots
if roots[0][0] > roots[1][0]:
tv = roots[1][0]
roots[1][0] = roots[0][0]
roots[0][0] = tv
tv2 = roots[1][1]
roots[1][1] = roots[0][1]
roots[0][1] = tv2
if roots[0][0] > roots[2][0]:
tv = roots[2][0]
roots[2][0] = roots[0][0]
roots[0][0] = tv
tv2 = roots[2][1]
roots[2][1] = roots[0][1]
roots[0][1] = tv2
if roots[1][0] > roots[2][0]:
tv = roots[2][0]
roots[2][0] = roots[1][0]
roots[1][0] = tv
tv2 = roots[2][1]
roots[2][1] = roots[1][1]
roots[1][1] = tv2
validroots = []
if roots[0][1] == 0 and roots[0][0] > 0.0: validroots.append(roots[0][0])
if roots[1][1] == 0 and roots[1][0] > 0.0: validroots.append(roots[1][0])
if roots[2][1] == 0 and roots[2][0] > 0.0: validroots.append(roots[2][0])
# returns only valid real roots
return validroots
def enthalpy(Vz, T, P, state):
# calculates enthalpy using PR EOS
# Vx = composition vector in molar fractions
# T = temperature in K
# P = Pressure in Pa
# state = mixture state (Liquid, Vapor or Solid)
# ideal gas enthalpy contribution
Hid = pp.RET_Hid(298.15, T, Vz)
R = 8.314
n = len(Vz)
Tc = pp.RET_VTC() # critical temperatures
Pc = pp.RET_VPC() # critical pressures
w = pp.RET_VW() # acentric factors
alpha = [0] * n
ai = [0] * n
bi = [0] * n
ci = [0] * n
for i in range(n):
alpha[i] = (1 + (0.37464 + 1.54226 * w[i] - 0.26992 * w[i] ** 2) * (1 - (T / Tc[i]) ** 0.5)) ** 2
ai[i] = 0.45724 * alpha[i] * R ** 2 * Tc[i] ** 2 / Pc[i]
bi[i] = 0.0778 * R * Tc[i] / Pc[i]
ci[i] = 0.37464 + 1.54226 * w[i] - 0.26992 * w[i] ** 2
a = [[0] * n for i in range(n)]
# get binary interaction parameters (BIPs/kijs) from PR Property Package
kij = [[0] * n for i in range(n)]
vkij = pp.RET_VKij()
for i in range(n):
for j in range(n):
kij[i][j] = vkij[i, j]
a[i][j] = (ai[i] * ai[j]) ** 0.5 * (1 - kij[i][j]) # <- default mixing rule for amix
amix = 0.0
bmix = 0.0
amix2 = [0] * n
for i in range(n):
for j in range(n):
amix += Vz[i] * Vz[j] * a[i][j] # <- default mixing rule for amix
amix2[i] += Vz[j] * a[j][i]
for i in range(n):
bmix += Vz[i] * bi[i] # <- default mixing rule - no interaction parameters for bmix
AG = amix * P / (R * T) ** 2
BG = bmix * P / (R * T)
coeff = [0] * 4
coeff[0] = 1
coeff[1] = BG - 1
coeff[2] = AG - 3 * BG ** 2 - 2 * BG
coeff[3] = -AG * BG + BG ** 2 + BG ** 3
roots = calcroots(coeff) # <- get the real roots of the cubic equation
# compressibility factor = cubic equation's root
if state == State.Liquid:
# liquid
Z = min(roots)
else:
# vapor
Z = max(roots)
# amix temperature derivative
dadT1 = -8.314 / 2 * (0.45724 / T) ** 0.5
dadT2 = 0.0#
for i in range(n):
j = 0
for j in range(n):
dadT2 += Vz[i] * Vz[j] * (1 - kij[i][j]) * (ci[j] * (ai[i] * Tc[j] / Pc[j]) ** 0.5 + ci[i] * (ai[j] * Tc[i] / Pc[i]) ** 0.5)
dadT = dadT1 * dadT2
uu = 2
ww = -1
DAres = amix / (bmix * (uu ** 2 - 4 * ww) ** 0.5) * Math.Log((2 * Z + BG * (uu - (uu ** 2 - 4 * ww) ** 0.5)) / (2 * Z + BG * (uu + (uu ** 2 - 4 * ww) ** 0.5))) - R * T * Math.Log((Z - BG) / Z) - R * T * Math.Log(Z)
DSres = R * Math.Log((Z - BG) / Z) + R * Math.Log(Z) - 1 / (8 ** 0.5 * bmix) * dadT * Math.Log((2 * Z + BG * (2 - 8 ** 0.5)) / (2 * Z + BG * (2 + 8 ** 0.5)))
DHres = DAres + T * (DSres) + R * T * (Z - 1)
# mixture molar weight (MW)
MW = pp.AUX_MMM(Vz)
print 'calculated enthalpy = ' + str(Hid + DHres/ MW) + ' kJ/kg (' + str(state) + ')'
return Hid + DHres / MW # kJ/kg
# activate enthalpy calculation override on PR Property Package
pp.OverrideEnthalpyCalculation = True
# set the function that calculates the enthalpy
pp.EnthalpyCalculationOverride = enthalpy
Add a New Property to a Valve Object
DWSIM
Dynamic Properties
Flowsheet
# The Dynamic Properties feature in DWSIM allows you to add new properties to flowsheet objects, which will persist between simulation file saving/opening cycles.
# These properties can be used by scripts to perform additional calculations, or even override the actual models.
# For instance, you can add a new property to a valve object on the flowsheet called "Cv", setting a value for it for later use on an additional calculation step:
valve = Flowsheet.GetFlowsheetSimulationObject("FV-01")
valve.ExtraProperties.Cv = 3102.78
props = valve.ExtraProperties
# Cv = 11.6 Q (SG / dp)^0.5
# dp = SG/(Cv/11.6Q)^2
# where
# q = water flow (m3/hr)
# SG = specific gravity (1 for water)
# dp = pressure drop (kPa)
DP = valve.DeltaP / 1000
SG = inlet.Phases[0].Properties.density / 1000
Q = props.Cv / 11.6 / (SG/DP) ** 0.5 / 60 / 60 # m3/s
Create an Excel Spreadsheet
IronPython
Advanced
Flowsheet
import clr
import sys
clr.AddReferenceByName('Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c')
from Microsoft.Office.Interop import Excel
ex = Excel.ApplicationClass()
ex.Visible = False
ex.DisplayAlerts = True
workbook = ex.Workbooks.Add()
workbook.Worksheets.Add()
ws1 = workbook.Worksheets[1]
ws1.UsedRange.Cells[1, 1].Value2 = "time"
ws1.UsedRange.Cells[1, 2].Value2 = "source_level"
ws1.UsedRange.Cells[1, 3].Value2 = "sink_level"
ws1.UsedRange.Cells[1, 4].Value2 = "hot_water_flow"
ws1.UsedRange.Cells[1, 5].Value2 = "hot_water_temp"
ws1.UsedRange.Cells[1, 6].Value2 = "cooling_water_flow"
ws1.UsedRange.Cells[1, 7].Value2 = "cooling_water_temp"
ws1.UsedRange.Cells[1, 8].Value2 = "valve_opening"
ex.Visible = True
Get Feed Stream Properties
DWSIM
Basics
User Model
import math
from System import Array
# Set the feed stream
feed = ims1
# Get temperature and pressure of the feed
# Notice that the values returned are one-element vectors, not scalars
T = feed.GetProp("temperature", "Overall", None, "", "") # K
P = feed.GetProp("pressure", "Overall", None, "", "") # Pa
# Get the number of components in the feed stream
n = int(feed.GetNumCompounds())
# Get compound IDs in the feed stream
ids = feed.ComponentIds
Set Product Stream Properties
DWSIM
Basics
User Model
# Set properties in the overflow stream (T, P, xmo, xwo, to)
# CAPE-OPEN's "SetProp" function expects you to provide a vector containing
# the property values, even if it is only one value that you're trying to set.
# Notice the "[]" enclosure around the qt variable in the last SetProp call.
overflow = oms1
overflow.Clear()
overflow.SetProp("temperature", "Overall", None, "", "", T) # K
overflow.SetProp("pressure", "Overall", None, "", "", P) # Pa
Calculate Equilibrium, Get and Set Properties
DWSIM
Advanced
User Model
import math
from System import Array
# Set the streams
inlet1 = ims1
inlet2 = ims2
inlet3 = ims3
outlet1 = oms1
outlet2 = oms2
outlet3 = oms3
# Get streams' enthalpies and mass flows, set specified outlet temperatures
if inlet1 <> None:
mixphase = inlet1.GetPhase("Mixture")
Hin1 = mixphase.Properties.enthalpy
Win1 = mixphase.Properties.massflow
outlet1.CopyFromMaterial(inlet1)
else:
Hin1 = 0.0
Win1 = 0.0
if inlet2 <> None:
mixphase = inlet2.GetPhase("Mixture")
Hin2 = mixphase.Properties.enthalpy
Win2 = mixphase.Properties.massflow
outlet2.CopyFromMaterial(inlet2)
outlet2.GetPhase("Mixture").Properties.temperature = 20 + 273.15
else:
Hin2 = 0.0
Win2 = 0.0
if inlet3 <> None:
Hin3 = mixphase.Properties.enthalpy
Win3 = mixphase.Properties.massflow
outlet3.CopyFromMaterial(inlet3)
outlet3.GetPhase("Mixture").Properties.temperature = 13 + 273.15
else:
Hin3 = 0.0
Win3 = 0.0
if outlet2 <> None:
outlet2.CalcEquilibrium("TP",None)
Hout2 = outlet2.GetPhase("Mixture").Properties.enthalpy
else:
Hout2 = 0.0
if outlet3 <> None:
outlet3.CalcEquilibrium("TP",None)
Hout3 = outlet3.GetPhase("Mixture").Properties.enthalpy
else:
Hout3 = 0.0
Calculate PH Flash
DWSIM
Advanced
User Model
outlet1.GetPhase("Mixture").Properties.enthalpy = 0.0 # kJ/kg
# calculate outlet temperature
outlet1.CalcEquilibrium("PH",None)
print outlet1.GetPhase("Mixture").Properties.temperature
Get Ideal Gas Heat Capacity from a Compound
DWSIM
Basics
Flowsheet/User Model
compIds = ['Argon']
props = ['idealgasheatcapacity']
values = feed.GetTDependentProperty(props, 298.15, compIds, None)
Flowsheet.ShowMessage(str(values[0]), 0, "")
Clone a Material Stream
DWSIM
Basics
Flowsheet/User Model
oms1 = ims1.Duplicate()
Copy Properties between Material Streams
DWSIM
Basics
Flowsheet/User Model
i_solid = Flowsheet.GetFlowsheetSimulationObject('i_solid')
o_solid = Flowsheet.GetFlowsheetSimulationObject('o_solid')
o_solid.Assign(i_solid)
Working with the CoolProp library
DWSIM
Basics
Flowsheet/User Model
import clr
clr.AddReference("DWSIM.Thermodynamics.CoolPropInterface")
import CoolProp
tcrit = CoolProp.Props1SI("Water", "TCRIT")
print tcrit
Calculate Latent Heat of Vaporization
DWSIM
Advanced
Flowsheet/User Model
import clr
import System
import DWSIM
from System import *
clr.AddReference("System.Core")
clr.ImportExtensions(System.Linq)
ms = Flowsheet.GetFlowsheetSimulationObject("MSTR-000")
pp = ms.PropertyPackage
pp.CurrentMaterialStream = ms
# get mixture composition
Vz = ms.Phases[0].Compounds.Values.Select(lambda x: x.MoleFraction).Cast[Double]().ToArray()
# get temperature
T = ms.Phases[0].Properties.temperature
# get pressure
P = ms.Phases[0].Properties.pressure
# calculate mixture enthalpy as vapor
hv = pp.DW_CalcEnthalpy(Vz, T, P, DWSIM.Thermodynamics.PropertyPackages.State.Vapor)
# calculate mixture enthalpy as liquid
hl = pp.DW_CalcEnthalpy(Vz, T, P, DWSIM.Thermodynamics.PropertyPackages.State.Liquid)
# enthalpy of vaporization
hvap = hv-hl
# kJ/kg
print str(hvap)
Get the Critical Temperature of a Compound
DWSIM
Basics
Flowsheet/User Model
air_stream = Flowsheet.GetFlowsheetSimulationObject('MSTR-000')
air_compound = air_stream.GetPhase('Mixture').Compounds['Air']
crit_temp = air_compound.ConstantProperties.Critical_Temperature
print crit_temp