Python Script Unit Operation¶
![]()
Represents a Python Script Unit Operation that executes user-supplied Python code (IronPython or Python.NET) as part of the flowsheet calculation sequence.
DWSIM.UnitOperations.UnitOperations.CustomUO
Assembly DWSIM.UnitOperations.dll · Object ← BaseClass ← UnitOpBaseClass ← CustomUO
At a glance¶

| Port | Index | Connected in the example |
|---|---|---|
| Inlet, material | 0 |
Feed |
| Inlet, material | 1 |
|
| Inlet, material | 2 |
|
| Inlet, energy | 3 |
|
| Inlet, material | 4 |
|
| Inlet, material | 5 |
|
| Inlet, material | 6 |
|
| Outlet, material | 0 |
Product |
| Outlet, material | 1 |
|
| Outlet, material | 2 |
|
| Outlet, energy | 3 |
Duty |
| Outlet, material | 4 |
|
| Outlet, material | 5 |
|
| Outlet, material | 6 |
Example¶
This code runs on every build of this site, and the output below is what it printed.
fs = (Flowsheet.Create("ScriptUOExample")
.WithCompounds("N-hexane", "N-heptane")
.WithPropertyPackage(PropertyPackages.PengRobinson))
feed = (fs.AddMaterialStream("Feed")
.At(Q.Celsius(25.0), Q.Bar(2.0))
.SetCompoundMassFlow("N-hexane", 0.9) # kg/s
.SetCompoundMassFlow("N-heptane", 0.6))
product = fs.AddMaterialStream("Product")
duty = fs.AddEnergyStream("Duty")
# The script runs in IronPython. ims1..ims6 and oms1..oms6 are the material streams on
# ports 0-2 and 4-6, ies1 and oes1 the energy streams on port 3. Every input variable
# becomes a script variable, and every number left in the script becomes an output.
script = """
oms1.Assign(ims1)
oms1.SetTemperature(ims1.GetTemperature() + DeltaT)
oms1.SetFlashSpec("PT")
oms1.Calculate()
Q_kW = ims1.GetMassFlow() * (oms1.GetMassEnthalpy() - ims1.GetMassEnthalpy())
oes1.EnergyFlow = Q_kW
"""
uo = (fs.AddUnitOperation(ObjectType.CustomUO, "SCRIPT-1")
.ConnectFeed(feed, 0)
.ConnectProduct(product, 0))
# the duty leaves through outlet port 3
uo.ConnectEnergyProduct(duty, 3)
s = uo.Object.__implementation__ # the PythonScriptUO behind the generic builder
s.ScriptText = script
s.InputVariables["DeltaT"] = 40.0 # K
fs.AutoLayout()
fs.Solve()
print(f"Outlet T = {product.TemperatureK - 273.15:.2f} C")
print(f"Duty (script) = {s.OutputVariables['Q_kW']:.2f} kW")
print(f"Energy stream = {duty.Object.EnergyFlow:.2f} kW")
print(f"Mean cp = {s.OutputVariables['Q_kW'] / (1.5 * 40.0):.3f} kJ/(kg.K)")
Output
DWSIM 10.2.11.0, generated 2026-10-08.
Properties¶
IDs accepted by GetPropertyValue, SetPropertyValue, the sensitivity analysis, the optimizer, the Adjust block and dynamic events. Units are SI; pass another unit system to GetPropertyValue to get them converted.
| ID | Name | Unit (SI) | Input |
|---|---|---|---|
DeltaT |
Temperature Difference | yes | |
Q_kW |
result |
Learn more¶
-
User guide
API members¶
Public members declared by this class. Inherited members are documented on the base classes.
Constructors¶
CustomUO(): Initializes a new default instance.
Initializes a new default instance.
CustomUO(string, string): Initializes a new instance with a name and description.
Initializes a new instance with a name and description.
| Parameter | Type | Description |
|---|---|---|
name |
String |
|
description |
String |
Properties¶
EmbeddedImageData: Gets or sets the Base-64 encoded embedded image data for a custom icon.
Gets or sets the Base-64 encoded embedded image data for a custom icon.
ExecutionEngine: Gets or sets the Python execution engine (IronPython or Python.NET).
Gets or sets the Python execution engine (IronPython or Python.NET).
FontName: Gets or sets the font name used in the script editor.
Gets or sets the font name used in the script editor.
FontSize: Gets or sets the font size used in the script editor.
Gets or sets the font size used in the script editor.
HasPropertiesForDynamicMode: Gets a value indicating this unit operation has no specific dynamic-mode properties.
Gets a value indicating this unit operation has no specific dynamic-mode properties.
HighlightSpaces: Gets or sets whether spaces are highlighted in the script editor.
Gets or sets whether spaces are highlighted in the script editor.
HighlightTabs: Gets or sets whether tabs are highlighted in the script editor.
Gets or sets whether tabs are highlighted in the script editor.
Includes: Gets or sets the list of additional Python script files to include.
Gets or sets the list of additional Python script files to include.
InputStringVariables: Gets or sets the dictionary of string-typed input variables exposed to the script.
Gets or sets the dictionary of string-typed input variables exposed to the script.
InputVariables: Gets or sets the dictionary of numeric input variables exposed to the script.
Gets or sets the dictionary of numeric input variables exposed to the script.
MobileCompatible: Gets a value indicating whether this unit operation is compatible with mobile interfaces.
Gets a value indicating whether this unit operation is compatible with mobile interfaces.
ObjectClass: Gets or sets the simulation object class category (UserModels).
Gets or sets the simulation object class category (UserModels).
OutputVariables: Gets or sets the dictionary of numeric output variables produced by the script.
Gets or sets the dictionary of numeric output variables produced by the script.
ScriptText: Gets or sets the Python script text to execute during calculation.
Gets or sets the Python script text to execute during calculation.
SupportsDynamicMode: Gets a value indicating this unit operation supports dynamic mode.
Gets a value indicating this unit operation supports dynamic mode.
UseEmbeddedImage: Gets or sets whether the embedded image is used as the graphic object icon.
Gets or sets whether the embedded image is used as the graphic object icon.
Methods¶
Calculate(object): Executes the Python script, passing flowsheet objects and variables into the scope.
Executes the Python script, passing flowsheet objects and variables into the scope.
| Parameter | Type | Description |
|---|---|---|
args |
Object |
CloneXML(): Creates a deep copy via XML serialization.
Creates a deep copy via XML serialization.
CloseEditForm(): Closes and disposes the editing form.
Closes and disposes the editing form.
DeCalculate(): Clears all calculated results.
Clears all calculated results.
DisplayEditForm(): Opens or activates the editing form.
Opens or activates the editing form.
DisplayScriptEditorForm(): Opens the script editor window.
Opens the script editor window. Does nothing where the WinForms editors are not built.
GetDisplayDescription(): Returns the localised display description.
Returns the localised display description.
GetDisplayName(): Returns the localised display name.
Returns the localised display name.
GetIconBitmapBytes(): Returns the icon bitmap as a byte array.
Returns the icon bitmap as a byte array.
GetProperties(PropertyType): Returns an array of property identifiers for the specified property type.
Returns an array of property identifiers for the specified property type.
| Parameter | Type | Description |
|---|---|---|
proptype |
PropertyType |
GetPropertyUnit(string, IUnitsOfMeasure): Returns the unit string for the specified property.
Returns the unit string for the specified property.
| Parameter | Type | Description |
|---|---|---|
prop |
String |
|
su |
IUnitsOfMeasure |
GetPropertyValue(string, IUnitsOfMeasure): Returns the value of the specified property.
Returns the value of the specified property.
| Parameter | Type | Description |
|---|---|---|
prop |
String |
|
su |
IUnitsOfMeasure |
GetReport(IUnitsOfMeasure, CultureInfo, string): Generates a plain-text report of the unit operation results.
Generates a plain-text report of the unit operation results.
| Parameter | Type | Description |
|---|---|---|
su |
IUnitsOfMeasure |
|
ci |
CultureInfo |
|
numberformat |
String |
LoadData(List<XElement>): Restores the unit operation state from XML, including variable dictionaries.
Restores the unit operation state from XML, including variable dictionaries.
| Parameter | Type | Description |
|---|---|---|
data |
List<XElement> |
RunDynamicModel(): Executes the script in dynamic simulation mode.
Executes the script in dynamic simulation mode.
SaveData(): Serializes the unit operation state to XML, including variable dictionaries.
Serializes the unit operation state to XML, including variable dictionaries.
SetPropertyValue(string, object, IUnitsOfMeasure): Sets the value of the specified property.
Sets the value of the specified property.
| Parameter | Type | Description |
|---|---|---|
prop |
String |
|
propval |
Object |
|
su |
IUnitsOfMeasure |
UpdateEditForm(): Refreshes the editing form with updated data.
Refreshes the editing form with updated data.
Validate(): Validates the unit operation configuration.
Validates the unit operation configuration.
Fields¶
f: The classic (WinForms) editor window open for this unit operation, if any.
The classic (WinForms) editor window open for this unit operation, if any. Not saved with the flowsheet.
fs: Reserved handle for a script editor window; not assigned or read by the current code.
Reserved handle for a script editor window; not assigned or read by the current code. Not saved with the flowsheet.