Difference between revisions of "IronPython Script Snippets"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
− | = | + | = Create and Connect Objects = |
Assuming that you're running a simulation with defined compound(s) and property package(s), this will create and connect a cooler and its connections: | Assuming that you're running a simulation with defined compound(s) and property package(s), this will create and connect a cooler and its connections: | ||
Line 31: | Line 31: | ||
</source> | </source> | ||
− | = | + | = Getting a reference to a Compound in the simulation = |
<source lang=python> | <source lang=python> | ||
Line 43: | Line 43: | ||
'''mycompound''' and '''mycompound2''' are referencing the same object in memory. | '''mycompound''' and '''mycompound2''' are referencing the same object in memory. | ||
− | = | + | = Executing a script from another tab/section = |
<source lang=python> | <source lang=python> | ||
Line 62: | Line 62: | ||
</source> | </source> | ||
− | = | + | = Setting the properties of a Material Stream = |
<source lang=python> | <source lang=python> | ||
Line 74: | Line 74: | ||
</source> | </source> | ||
+ | |||
+ | = Getting Surface Tension and Diffusion Coefficients from a Material Stream = | ||
+ | |||
+ | <source lang=python> | ||
+ | |||
+ | import clr | ||
+ | import System | ||
+ | |||
+ | # get feed's interfacial tension - method 1 | ||
+ | |||
+ | mixphase = feed.GetPhase("Mixture") | ||
+ | |||
+ | sftens = mixphase.Properties.surfaceTension | ||
+ | |||
+ | print str(sftens) + " N/m" | ||
+ | |||
+ | # get feed's interfacial tension - method 2 | ||
+ | |||
+ | sftens2 = clr.Reference[System.Object]() | ||
+ | |||
+ | feed.GetTwoPhaseProp("surfacetension", None, "", sftens2) | ||
+ | |||
+ | print str(sftens2.Value[0]) + " N/m" | ||
+ | |||
+ | # diffusion coefficients | ||
+ | |||
+ | phase = feed.GetPhase("Vapor") | ||
+ | |||
+ | compound = phase.Compounds["Methane"] | ||
+ | |||
+ | difc = compound.DiffusionCoefficient | ||
+ | |||
+ | print str(difc) + " m2/s" | ||
+ | |||
+ | </source> | ||
+ | |||
+ | = Other Snippets = | ||
+ | |||
+ | For more code snippets, go to [https://sourceforge.net/p/dwsim/discussion/scripting/ https://sourceforge.net/p/dwsim/discussion/scripting/]. |
Revision as of 17:36, 2 July 2019
Contents
Create and Connect Objects
Assuming that you're running a simulation with defined compound(s) and property package(s), this will create and connect a cooler and its connections:
import clr clr.AddReference('DWSIM.Interfaces') from DWSIM import Interfaces # http://dwsim.inforside.com.br/api_help57/html/M_DWSIM_Interfaces_IFlowsheet_AddObject.htm # http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_Interfaces_Enums_GraphicObjects_ObjectType.htm cooler = Flowsheet.AddObject(Interfaces.Enums.GraphicObjects.ObjectType.Cooler, 100, 100, 'COOLER-001') heat_out = Flowsheet.AddObject(Interfaces.Enums.GraphicObjects.ObjectType.EnergyStream, 130, 150, 'HEAT_OUT') inlet = Flowsheet.AddObject(Interfaces.Enums.GraphicObjects.ObjectType.MaterialStream, 50, 100, 'INLET') outlet = Flowsheet.AddObject(Interfaces.Enums.GraphicObjects.ObjectType.MaterialStream, 150, 100, 'OUTLET') cooler.GraphicObject.CreateConnectors(1, 1) inlet.GraphicObject.CreateConnectors(1, 1) outlet.GraphicObject.CreateConnectors(1, 1) heat_out.GraphicObject.CreateConnectors(1, 1) # http://dwsim.inforside.com.br/api_help57/html/M_DWSIM_Interfaces_IFlowsheet_ConnectObjects.htm Flowsheet.ConnectObjects(inlet.GraphicObject, cooler.GraphicObject, 0, 0) Flowsheet.ConnectObjects(cooler.GraphicObject, outlet.GraphicObject, 0, 0) Flowsheet.ConnectObjects(cooler.GraphicObject, heat_out.GraphicObject, 0, 0)
Getting a reference to a Compound in the simulation
mycompound = Flowsheet.SelectedCompounds['Methane'] mycompound2 = Flowsheet.GetSimulationObject['MSTR-001'].Phases[0].Compounds['Methane']
mycompound and mycompound2 are referencing the same object in memory.
Executing a script from another tab/section
import clr import System from System import * clr.AddReference('System.Core') clr.ImportExtensions(System.Linq) # get the script text from "Functions" using LINQ source = Flowsheet.Scripts.Values.Where(lambda x: x.Title == 'Functions').FirstOrDefault().ScriptText.replace('\r', '') # execute the script exec(source)
Setting the properties of a Material Stream
ms1 = Flowsheet.GetFlowsheetSimulationObject('MSTR-001') overall = ms1.GetPhase('Overall') overall.Properties.temperature = 200 # set temperature to 200 K overall.Properties.pressure = 101325 # set pressure to 101325 Pa overall.Properties.massflow = 14 # set mass flow to 14 kg/s
Getting Surface Tension and Diffusion Coefficients from a Material Stream
import clr import System # get feed's interfacial tension - method 1 mixphase = feed.GetPhase("Mixture") sftens = mixphase.Properties.surfaceTension print str(sftens) + " N/m" # get feed's interfacial tension - method 2 sftens2 = clr.Reference[System.Object]() feed.GetTwoPhaseProp("surfacetension", None, "", sftens2) print str(sftens2.Value[0]) + " N/m" # diffusion coefficients phase = feed.GetPhase("Vapor") compound = phase.Compounds["Methane"] difc = compound.DiffusionCoefficient print str(difc) + " m2/s"
Other Snippets
For more code snippets, go to https://sourceforge.net/p/dwsim/discussion/scripting/.