Skip to content

Instantly share code, notes, and snippets.

@DanWBR
Last active December 6, 2024 10:32
Show Gist options
  • Select an option

  • Save DanWBR/3cb00f7ca112e6bbeca3e986890cef7f to your computer and use it in GitHub Desktop.

Select an option

Save DanWBR/3cb00f7ca112e6bbeca3e986890cef7f to your computer and use it in GitHub Desktop.
Using DWSIM Standalone Thermodynamics Library in Python with Python.NET (Low-Level Flash API + CAPE-OPEN-style calls)
import clr
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
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
compprops = dtlc.GetCompoundConstPropList()
print("Ethanol constant properties:\n")
for prop in compprops:
pval = dtlc.GetCompoundConstProp("Ethanol", prop)
print(prop + "\t" + pval)
print()
compprops = dtlc.GetCompoundPDepPropList()
print()
print("Ethanol pressure-dependent properties at P = " + str(P) + " Pa:\n")
for prop in compprops:
pval = dtlc.GetCompoundPDepProp("Ethanol", prop, P)
print(prop + "\t" + pval)
print()
compprops = dtlc.GetCompoundTDepPropList()
print()
print("Ethanol temperature-dependent properties at T = " + str(T) + " K:\n")
for prop in compprops:
pval = dtlc.GetCompoundTDepProp("Ethanol", prop, T)
print(prop + "\t" + pval)
print()
print()
print("Water/Ethanol Interaction Parameters for NRTL model:")
print()
# uncheck this if you have a CUDA or OpenCL device to use
# dtlc.EnableGPUProcessing()
# dtlc.InitComputeDevice(Cudafy.eLanguage.Cuda, 0)
ip = dtlc.GetInteractionParameterSet("NRTL", "Water", "Ethanol")
print("A12 = " + str(ip.Parameters["A12"]) + " cal/mol")
print("A21 = " + str(ip.Parameters["A21"]) + " cal/mol")
print("alpha = " + str(ip.Parameters["alpha"]))
print("PT Flash of an equimolar mixture of Water and Ethanol at T = " + str(T) + " K and P = " + str(P) + " Pa:" + "\n")
print("Using NRTL model for equilibrim calculations.")
print()
carray = Array[String](["Water", "Ethanol"])
comparray = Array[Double]([0.5, 0.5])
result2 = dtlc.PTFlash(nrtl, 0, P, T, carray, comparray)
print()
print("Flash calculation results:")
print()
for i in range(0, result2.GetLength(0)):
if (i == 0):
line = "Phase Name" + "\t"
elif (i == 1):
line = "Phase Mole Fraction in Mixture" + "\t"
elif (i == 2):
line = "Water Mole Fraction in Phase" + "\t"
elif (i == 3):
line = "Ethanol Mole Fraction in Phase" + "\t"
else:
line = ""
for j in range(0, result2.GetLength(1)):
line += str(result2[i, j])
print(line)
print()
print("Vapor Phase Mixture Properties at T = " + str(T) + " K and P = " + str(P) + " Pa:" + "\n")
vcarray = Array[Double]([float(result2[2, 0]), float(result2[3, 0])])
compphaseprops = dtlc.GetPropList()
for prop in compphaseprops:
try:
values = dtlc.CalcProp(nrtl, prop, "Mole", "Vapor", carray, T, P, vcarray)
line = ""
for i in range(0, values.Length):
line += str(values[i]) + "\t"
print(prop + "\t" + line)
except CapeOpen.CapeThrmPropertyNotAvailableException as e:
print(prop + "\t" + "Property Not Available")
except CapeOpen.CapeComputationException as e:
print(prop + "\t" + "Error Calculating Property")
except Exception as e:
print(prop + "\t" + e.Message)
print()
print("Liquid Phase Mixture Properties at T = " + str(T) + " K and P = " + str(P) + " Pa:" + "\n")
lcarray = Array[Double]([float(result2[2, 1]), float(result2[3, 1])])
for prop in compphaseprops:
try:
values = dtlc.CalcProp(nrtl, prop, "Mole", "Liquid", carray, T, P, lcarray)
line = ""
for i in range(0, values.Length):
line += str(values[i]) + "\t"
print(prop + "\t" + line)
except CapeOpen.CapeThrmPropertyNotAvailableException as e:
print(prop + "\t" + "Property Not Available")
except CapeOpen.CapeComputationException as e:
print(prop + "\t" + "Error Calculating Property")
except Exception as e:
print(prop + "\t" + e.Message)
@kookma
Copy link

kookma commented Nov 7, 2020

Thank you Daniel!

@andr1976
Copy link

andr1976 commented Nov 8, 2020

One some instances (choice of Proppack/Flashalg) I get the follwoing exception:

Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'Ipopt39': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at Cureos.Numerics.Ipopt.FreeIpoptProblem(IntPtr ipopt_problem)
at Cureos.Numerics.Ipopt.Dispose(Boolean disposing) in C:\Users\Daniel\source\repos\DanWBR\dwsim6\DWSIM.Math.DotNumerics\LinearAlgebra\CSLapack\dscal.cs:line 0
at Cureos.Numerics.Ipopt.Finalize() in C:\Users\Daniel\source\repos\DanWBR\dwsim6\DWSIM.Math.DotNumerics\LinearAlgebra\CSLapack\dscal.cs:line 0

Ifs some path to a required DLL hardtyped somewhere?

@DanWBR
Copy link
Author

DanWBR commented Nov 8, 2020

Forgot to include IPOPT. Download it and put the DLLs on the same folder.

@lnix05
Copy link

lnix05 commented Sep 11, 2023

Hi Daniel,
Trying to execute this code I get the following error:

NullReferenceException Traceback (most recent call last)
in
35 print("Ethanol constant properties:\n")
36 for prop in compprops:
---> 37 pval = dtlc.GetCompoundConstProp("Ethanol", prop)
38 print(prop + "\t" + pval)
39

NullReferenceException: Object reference not set to an instance of an object.
at DWSIM.Thermodynamics.Streams.MaterialStream.get_PropertyPackage() in C:\Users\Daniel\source\repos\DanWBR\dwsim\DWSIM.Thermodynamics\MaterialStream\MaterialStream.vb:line 279
at DWSIM.Thermodynamics.Streams.MaterialStream.GetCompoundConstant(Object props, Object compIds) in C:\Users\Daniel\source\repos\DanWBR\dwsim\DWSIM.Thermodynamics\MaterialStream\MaterialStream.vb:line 4233
at DWSIM.Thermodynamics.CalculatorInterface.Calculator.GetCompoundConstProp(String compound, String prop) in C:\Users\Daniel\source\repos\DanWBR\dwsim\DWSIM.Thermodynamics\Interfaces\Thermodynamics.vb:line 541

Any idea on what might be causing it?

@wangyexiang
Copy link

Dear Sir, have you resolved this issue? @lnix05

@krishgokulv
Copy link

i am trying a custom model in using dwsim user python model, i want to get pure species properties at STP as well as different condition. please share fuctions for that

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