IronPython CAPE-OPEN Unit Operation tips

From DWSIM - Chemical Process Simulator
Jump to: navigation, search

The IronPython Script Unit Operation allows you to input your models as scripts into CAPE-OPEN compliant simulators. There is a sample of a Membrane model for the COCO Simulator available here.

The sample code contains the basic CAPE-OPEN material stream operations like GetProp and SetProp. If you want to calculate viscosity of the inlet stream vapor phase, you can do the following (calculations are not allowed in inlet streams, you have to copy them first):


 # copy the feed stream
 feedCopy = ICapeThermoMaterialObject.Duplicate(feed)

 # calculate vapor phase viscosity (properties and phases must be passed as string arrays)
 ICapeThermoMaterialObject.CalcProp(feedCopy, Array[str](["viscosity"]), Array[str](["Vapor"]), "Mixture")

 # retrieve vapor phase viscosity value
 visc = ICapeThermoMaterialObject.GetProp(feedCopy, "viscosity", "Vapor", None, "Mixture", "")

 # display viscosity value in a message box
 clr.AddReference("System.Windows.Forms")
 from System.Windows.Forms import MessageBox
 MessageBox.Show(str(visc[0]))

When you create material objects on the fly, you must release them to free the used memory space after the work is done. To release "feedCopy" you'll have to do the following:


 from System.Runtime.InteropServices import Marshal
 Marshal.FinalReleaseComObject(feedCopy)