Skip to content

Instantly share code, notes, and snippets.

@andr1976
Created November 10, 2020 12:40
Show Gist options
  • Save andr1976/1a2c86dfb8a00f8acd759590244c55ea to your computer and use it in GitHub Desktop.
Save andr1976/1a2c86dfb8a00f8acd759590244c55ea to your computer and use it in GitHub Desktop.
Using DWSIM Standalone Thermodynamics Library in Python with Python.NET - Peng-Robinson
import clr, array
from System import String, Double, Array, Reflection, Exception
dtlpath = "C:\\Users\name\\Folder\\"
clr.AddReference(dtlpath + "DWSIM.Thermodynamics.StandaloneLibrary.dll")
from DWSIM.Thermodynamics import Streams, PropertyPackages, CalculatorInterface
from DWSIM.Interfaces.Enums import FlashCalculationType
#from DWSIM.Thermodynamics.PropertyPackages.Auxiliary.FlashAlgorithms import *
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)
pr = PropertyPackages.PengRobinsonPropertyPackage(True)
dtlc.TransferCompounds(pr)
T = 94+273.15 #K
P = 25*101325.0 #Pa
carray = Array[String](["Water", "Methane", "Propane", "Isobutane", "N-butane", "N-decane"])
comparray = Array[Double]([0.2, 0.2, 0.1, 0.1, 0.1, 0.3])
#result2 = dtlc.PTFlash(pr,3,P,T,carray,comparray)
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")
ms.SetPropertyPackage(pr)
ms.SetPressure(P)
ms.SetTemperature(T)
ms.SetMolarFlow(1.0) # mol/s
#nlvlle=NestedLoops #Immiscible
#nlvlle.StabSearchCompIDs = String(r"Water")
#nlvlle.StabSearchSeverity = 0
#ms.FlashAlgorithm = nlvlle
ms.SetFlashSpec("PT")
ms.Calculate()
vapordensity = ms.GetPhase("Vapor").Properties.density
print("Vapor Phase Density: " + str(vapordensity) + " kg/m3")
print("Vapour molecular weight: " + str(ms.GetPhase("Vapor").Properties.molecularWeight)+ " kg/kmole")
liquiddensity = ms.GetPhase("Liquid1").Properties.density
print("Liquid1 Phase Density: " + str(liquiddensity) + " kg/m3")
print("Liquid1 molecular weight: " + str(ms.GetPhase("Liquid1").Properties.molecularWeight) + " kg/kmole")
liquid2density = ms.GetPhase("Liquid2").Properties.density
print("Liquid 2 Phase Density: " + str(liquid2density) + " kg/m3")
print("Liquid2 molecular weight: " + str(ms.GetPhase("Liquid2").Properties.molecularWeight) + " kg/kmole")
for comp in ms.GetPhase("Overall").Compounds.Values:
print(comp.ConstantProperties.Name + " Overall Molar Flow: " + str(comp.MolarFlow) + " mol/s")
vaporflow = ms.GetPhase("Vapor").Properties.molarflow
print("Vapor Phase Molar Flow: " + str(vaporflow) + " mol/s")
liquidflow = ms.GetPhase("Liquid1").Properties.molarflow
print("Liquid Phase Molar Flow: " + str(liquidflow) + " mol/s")
liquid2flow = ms.GetPhase("Liquid2").Properties.molarflow
print("Liquid Phase Molar Flow: " + str(liquid2flow) + " mol/s")
@kookma
Copy link

kookma commented Nov 10, 2020

It would be nice to add a small description, tell readers what this code does! Also it is nice to have an example with its results.

@andr1976
Copy link
Author

kookma, I am not sure I can explain every bit, but generally it is a python way of using the DTL, via the pythonnet package (to call .Net). It defines the property package Peng-Robinson with whatever default methods are set (density, viscocity, ....etc). The components considered are defined by name and composition. A material stream is defined with the specified composition, flow, T and P. A PT-flash is solved by using the stream method (can be done differently also as shown by Daniel). Once solved some phase properties are printed. You have to study the interface description for DWSIM/DTL or other examles if you what other properties.

@kookma
Copy link

kookma commented Nov 10, 2020

Thank you Anders!

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