Skip to content

Instantly share code, notes, and snippets.

@DanWBR
Created November 6, 2020 20:02
Show Gist options
  • Save DanWBR/4a803cf325ae89a2663d9cdd0e8a61a2 to your computer and use it in GitHub Desktop.
Save DanWBR/4a803cf325ae89a2663d9cdd0e8a61a2 to your computer and use it in GitHub Desktop.
Using DWSIM Standalone Thermodynamics Library in Python with Python.NET (High-Level API + Material Stream calls)
import clr, array
from System.IO import Directory, Path, File
from System import String, Double, Array, Reflection, Exception
dtlpath = "C:\\Users\\Daniel\\Source\\Repos\\DanWBR\\dwsim6\\DistPackages\\DTL\\"
clr.AddReference(dtlpath + "DWSIM.Thermodynamics.StandaloneLibrary.dll")
from DWSIM.Thermodynamics import Streams, PropertyPackages, CalculatorInterface
from DWSIM.Interfaces.Enums import FlashCalculationType
import CapeOpen
dtlc = CalculatorInterface.Calculator()
print(String.Format("DTL version: {0}", Reflection.Assembly.GetAssembly(dtlc.GetType()).GetName().Version))
print()
dtlc.Initialize()
nrtl = PropertyPackages.NRTLPropertyPackage(True)
dtlc.TransferCompounds(nrtl)
T = 355.0 #K
P = 101325.0 #Pa
carray = Array[String](["Water", "Ethanol"])
comparray = Array[Double]([0.5, 0.5])
ms = dtlc.CreateMaterialStream(carray, comparray)
print ("Calculating Material Stream Phase Equilibria and Properties at T = " + str(T) + " K and P = " + str(P) + " Pa")
print ("Mass Flow = 1000 kg/s, Water Molar Fraction = 0.5, Ethanol Molar Fraction = 0.5")
ms.SetPropertyPackage(nrtl)
ms.SetPressure(P)
ms.SetTemperature(T)
ms.SetMassFlow(1000.0) # kg/s
ms.SetFlashSpec("PT")
ms.Calculate()
print()
vaporflow = ms.GetPhase("Vapor").Properties.massflow
print("Vapor Phase Mass Flow: " + str(vaporflow) + " kg/s")
vapordensity = ms.GetPhase("Vapor").Properties.density
print("Vapor Phase Density: " + str(vapordensity) + " kg/m3")
print()
liquidflow = ms.GetPhase("Liquid1").Properties.massflow
print("Liquid Phase Mass Flow: " + str(liquidflow) + " kg/s")
liquiddensity = ms.GetPhase("Liquid1").Properties.density
print("Liquid Phase Density: " + str(liquiddensity) + " kg/m3")
print()
for comp in ms.GetPhase("Overall").Compounds.Values:
print(comp.ConstantProperties.Name + " Overall Molar Flow: " + str(comp.MolarFlow) + " mol/s")
print()
for comp in ms.GetPhase("Vapor").Compounds.Values:
print(comp.ConstantProperties.Name + " Molar Flow in Vapor Phase: " + str(comp.MolarFlow) + " mol/s")
print()
for comp in ms.GetPhase("Liquid1").Compounds.Values:
print(comp.ConstantProperties.Name + " Molar Flow in Liquid Phase: " + str(comp.MolarFlow) + " mol/s")
@DanWBR
Copy link
Author

DanWBR commented Nov 6, 2020

Output:

DTL version: 6.3.7615.25480

Calculating Material Stream Phase Equilibria and Properties at T = 355.0 K and P = 101325.0 Pa
Mass Flow = 1000 kg/s, Water Molar Fraction = 0.5, Ethanol Molar Fraction = 0.5

Vapor Phase Mass Flow: 802.6610877618008 kg/s
Vapor Phase Density: 1.1710912656326562 kg/m3

Liquid Phase Mass Flow: 197.33508206419936 kg/s
Liquid Phase Density: 890.3405299098417 kg/m3

Water Overall Molar Flow: 15604.587249304503 mol/s
Ethanol Overall Molar Flow: 15604.587249304503 mol/s

Water Molar Flow in Vapor Phase: 10028.237922940652 mol/s
Ethanol Molar Flow in Vapor Phase: 13501.64176762674 mol/s

Water Molar Flow in Liquid Phase: 5576.485859075312 mol/s
Ethanol Molar Flow in Liquid Phase: 2102.8089489663002 mol/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment