Difference between revisions of "Automation"
Line 1: | Line 1: | ||
− | Automation enables software packages to expose their unique features to scripting tools and other applications. Using Automation, you can: | + | <div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><p><b>Automation</b> enables software packages to expose their unique features to scripting tools and other applications. Using Automation, you can: |
+ | </p> | ||
+ | <ul><li> Create applications and programming tools that expose objects.</li> | ||
+ | <li> Create and manipulate objects exposed in one application from another application.</li> | ||
+ | <li> Create tools that access and manipulate objects. These tools can include embedded macro languages, external programming tools, object browsers, and compilers.</li></ul> | ||
+ | <p>On a Windows environment, the objects an application or programming tool exposes are called ActiveX objects. Applications and programming tools that access those objects are called ActiveX clients. ActiveX objects and clients interact as follows: Applications and other software packages that support ActiveX technology define and expose objects which can be acted on by ActiveX components. ActiveX components are physical files (for example .exe and .dll files) that contain classes, which are definitions of objects. Type information describes the exposed objects, and can be used by ActiveX components at either compile time or at run time. | ||
+ | </p> | ||
+ | <div id="toc" class="toc"><div id="toctitle"><h2>Contents</h2><span class="toctoggle"> [<a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#" id="togglelink">hide</a>] </span></div> | ||
+ | <ul> | ||
+ | <li class="toclevel-1 tocsection-1"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Automation_support_in_DWSIM"><span class="tocnumber">1</span> <span class="toctext">Automation support in DWSIM</span></a></li> | ||
+ | <li class="toclevel-1 tocsection-2"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Registering_DLLs_for_COM_Automation"><span class="tocnumber">2</span> <span class="toctext">Registering DLLs for COM Automation</span></a></li> | ||
+ | <li class="toclevel-1 tocsection-3"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Introduction_to_Interfaces"><span class="tocnumber">3</span> <span class="toctext">Introduction to Interfaces</span></a></li> | ||
+ | <li class="toclevel-1 tocsection-4"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#API_Reference_Documentation"><span class="tocnumber">4</span> <span class="toctext">API Reference Documentation</span></a></li> | ||
+ | <li class="toclevel-1 tocsection-5"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#DWSIM_Flowsheet_Class_Structure"><span class="tocnumber">5</span> <span class="toctext">DWSIM Flowsheet Class Structure</span></a></li> | ||
+ | <li class="toclevel-1 tocsection-6"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Sample_Automation"><span class="tocnumber">6</span> <span class="toctext">Sample Automation</span></a> | ||
+ | <ul> | ||
+ | <li class="toclevel-2 tocsection-7"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#About_Cavett.27s_Problem"><span class="tocnumber">6.1</span> <span class="toctext">About Cavett's Problem</span></a></li> | ||
+ | <li class="toclevel-2 tocsection-8"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Excel_VBA"><span class="tocnumber">6.2</span> <span class="toctext">Excel VBA</span></a> | ||
+ | <ul> | ||
+ | <li class="toclevel-3 tocsection-9"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Code"><span class="tocnumber">6.2.1</span> <span class="toctext">Code</span></a></li> | ||
+ | </ul> | ||
+ | </li> | ||
+ | <li class="toclevel-2 tocsection-10"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#VB.NET"><span class="tocnumber">6.3</span> <span class="toctext">VB.NET</span></a> | ||
+ | <ul> | ||
+ | <li class="toclevel-3 tocsection-11"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Code_2"><span class="tocnumber">6.3.1</span> <span class="toctext">Code</span></a></li> | ||
+ | </ul> | ||
+ | </li> | ||
+ | <li class="toclevel-2 tocsection-12"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#C.23"><span class="tocnumber">6.4</span> <span class="toctext">C#</span></a> | ||
+ | <ul> | ||
+ | <li class="toclevel-3 tocsection-13"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Code_3"><span class="tocnumber">6.4.1</span> <span class="toctext">Code</span></a></li> | ||
+ | </ul> | ||
+ | </li> | ||
+ | </ul> | ||
+ | </li> | ||
+ | </ul> | ||
+ | </div> | ||
− | + | <h1><span class="mw-headline" id="Automation_support_in_DWSIM">Automation support in DWSIM</span></h1> | |
− | + | <p>Starting from version 4.2, DWSIM exposes its main Classes and Interfaces to Automation via COM/.NET. Automating DWSIM enables you to manipulate flowsheets and run sensitivity/optimization studies directly from Microsoft Excel, for instance, without the need of opening DWSIM directly. Interfacing DWSIM with Excel through VBA macros results in a powerful tool in process engineering activities, such as design, optimization and process evaluation. | |
− | + | </p><p>The simulation results may be output to an Excel spreadsheet in the development of the Heat and Material Balance for the process design, enabling quick manipulation of the resulting data through a well-known tool for every Chemical Engineer. | |
− | + | </p> | |
− | + | <h1><span class="mw-headline" id="Registering_DLLs_for_COM_Automation">Registering DLLs for COM Automation</span></h1> | |
− | + | <p>You can register DWSIM DLLs for automation during the installation process. You can also run the <b>automation_reg.bat</b> batch file (located in DWSIM's current installation directory) with admin privileges to register. To de-register, run <b>automation_unreg.bat</b> also as admin. When you uninstall DWSIM, the DLLs are automatically deregistered. | |
− | + | </p><p>If your automation project is based on a .NET language, there's no need to register the DLLs. You'll only need to add a reference to them. | |
− | + | </p><p>Automating DWSIM through COM is limited to Windows, though .NET is recommended as the default mechanism. On a Linux environment, you can use Mono to create and/or run an automation project in C#. | |
− | + | </p> | |
− | + | <h1><span class="mw-headline" id="Introduction_to_Interfaces">Introduction to Interfaces</span></h1> | |
− | + | <p>Before proceeding, read this text to get used to Interfaces and their implementation in actual Classes: <a rel="nofollow" class="external text" href="http://www.cs.utah.edu/~germain/PPS/Topics/interfaces.html">Interfaces in Object-Oriented Programming</a> | |
− | + | </p> | |
− | + | <h1><span class="mw-headline" id="API_Reference_Documentation">API Reference Documentation</span></h1> | |
− | + | <ul><li> <b>Automation Class:</b> <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/N_DWSIM_Automation.htm">[1]</a></li> | |
− | + | <li> <b>Interface Definitions:</b> <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/G_DWSIM_Interfaces.htm">[2]</a></li> | |
− | + | <li> <b>Unit Operations:</b> <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/G_DWSIM_UnitOperations.htm">[3]</a></li> | |
− | + | <li> <b>Thermodynamics:</b> <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/G_DWSIM_Thermodynamics.htm">[4]</a></li> | |
− | + | <li> <b>Base Class Shared Library:</b> <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/G_DWSIM_SharedClasses.htm">[5]</a></li> | |
− | + | <li> <b>Flowsheet GUI and DWSIM main executable:</b> <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/N_DWSIM.htm">[6]</a></li></ul> | |
− | + | <ul><li> <b>CAPE-OPEN Reference:</b> <a rel="nofollow" class="external autonumber" href="http://www.colan.org/specifications/">[7]</a></li></ul> | |
− | + | <h1><span class="mw-headline" id="DWSIM_Flowsheet_Class_Structure">DWSIM Flowsheet Class Structure</span></h1> | |
− | + | <div class="fullwidth"><a href="http://dwsim.inforside.com.br/wiki/index.php?title=File:Flowsheet_class_structure.png" class="image"><img alt="Flowsheet class structure.png" src="./automation_files/Flowsheet_class_structure.png" width="1036" height="516"></a></div> | |
− | Automation Class: | + | <p>The Flowsheet class in DWSIM provides access to all objects in the simulation: |
− | + | </p> | |
− | + | <ul><li> <b>Thermodynamics Subsystem</b>: includes Compounds, Property Packages, Flash Algorithms and Reactions/Reaction Sets collections.</li> | |
− | + | <li> <b>Simulation Objects Subsystem</b>: includes Material & Energy Streams and Unit Operation blocks.</li> | |
− | + | <li> <b>Graphical User Interface</b>: provides access to the displayed objects in the flowsheet and the connections between them.</li> | |
− | + | <li> <b>Accessories</b>: includes added utilities, sensitivity & optimization studies, system of units definitions and other simulation definitions.</li></ul> | |
− | + | <p>The <a rel="nofollow" class="external text" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_FormFlowsheet.htm">Flowsheet</a> object in DWSIM implements various interfaces, including <b>IFlowsheet</b>, <b>IFlowsheetBag</b>, <b>IFlowsheetGUI</b> and <b>IFlowsheetOptions</b>. | |
− | + | </p><p><b>IFlowsheet</b>: this is the main interface implemented by the Flowsheet class. It provides direct access to the various flowsheet components and helper functions to manipulate objects. <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_Interfaces_IFlowsheet.htm">[8]</a> | |
− | + | </p><p><b>IFlowsheetBag</b>: provides direct access to collections of flowsheet objects. <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_Interfaces_IFlowsheetBag.htm">[9]</a> | |
− | + | </p><p><b>IFlowsheetGUI</b>: this is an interface which defines helper functions to a Flowsheet GUI implementation. <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_Interfaces_IFlowsheetGUI.htm">[10]</a> | |
− | + | </p><p><b>IFlowsheetOptions</b>: this interface defines the flowsheet settings and other properties. <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_Interfaces_IFlowsheetOptions.htm">[11]</a> | |
− | + | </p><p>When you use the <a rel="nofollow" class="external text" href="http://dwsim.inforside.com.br/api_help57/html/N_DWSIM_Automation.htm">Automation</a> class to load a simulation, an <b>IFlowsheet</b> object is returned, which is actually an instance of the <b>Flowsheet</b> class. You can cast the returned object to any of the interfaces implemented by the Flowsheet class to access all available functions, properties and procedures. | |
− | + | </p> | |
− | + | <h1><span class="mw-headline" id="Sample_Automation">Sample Automation</span></h1> | |
− | + | <p>This sample automation code will run Cavett's Problem (simulation file located in the <i>samples</i> folder) with four different feed mass flow values, check outlet mass flows and calculate the mass balance of the flowsheet, displaying the results to the user. | |
− | + | </p> | |
− | + | <h3><span class="mw-headline" id="About_Cavett.27s_Problem">About Cavett's Problem</span></h3> | |
− | + | <p>A simulation problem proposed by Cavett (1963) has been used to test various chemical engineering simulation programs. It provides a useful benchmark to compare and contrast various tear stream locations and convergence algorithms. The process is equivalent to a four theoretical stage near isothermal distillation flash tanks. | |
− | + | </p><p><a href="http://dwsim.inforside.com.br/wiki/index.php?title=File:Cavett.jpg" class="image"><img alt="Cavett.jpg" src="./automation_files/Cavett.jpg" width="636" height="650"></a> | |
− | + | </p> | |
− | + | <ul><li> Feed Stream: 2</li> | |
− | + | <li> Vapor Outlet Stream: 8</li> | |
− | + | <li> Liquid Outlet Stream: 18</li></ul> | |
− | + | <h2><span class="mw-headline" id="Excel_VBA">Excel VBA</span></h2> | |
− | + | <p>To run this sample, create a new Excel VBA project and add a reference to <b>CAPE-OPEN 1.1 Type Library</b> (<a rel="nofollow" class="external free" href="http://www.colan.org/software-tools/cape-open-type-libraries-and-primary-interop-assemblies/">http://www.colan.org/software-tools/cape-open-type-libraries-and-primary-interop-assemblies/</a>), <b>DWSIM Simulator Automation Interface</b> and <b>DWSIM Simulator Interface Definitions Library</b>. | |
− | + | </p> | |
− | + | <h3><span class="mw-headline" id="Code">Code</span></h3> | |
− | + | <div><div id="highlighter_906235" class="syntaxhighlighter vbnet"><div class="toolbar"><span><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div><div class="line number36 index35 alt1">36</div><div class="line number37 index36 alt2">37</div><div class="line number38 index37 alt1">38</div><div class="line number39 index38 alt2">39</div><div class="line number40 index39 alt1">40</div><div class="line number41 index40 alt2">41</div><div class="line number42 index41 alt1">42</div><div class="line number43 index42 alt2">43</div><div class="line number44 index43 alt1">44</div><div class="line number45 index44 alt2">45</div><div class="line number46 index45 alt1">46</div><div class="line number47 index46 alt2">47</div><div class="line number48 index47 alt1">48</div><div class="line number49 index48 alt2">49</div><div class="line number50 index49 alt1">50</div><div class="line number51 index50 alt2">51</div><div class="line number52 index51 alt1">52</div><div class="line number53 index52 alt2">53</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="vbnet keyword">Public</code> <code class="vbnet keyword">Sub</code> <code class="vbnet plain">Sub1()</code></div><div class="line number2 index1 alt1"><code class="vbnet spaces"> </code> </div><div class="line number3 index2 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'create automation manager</code></div><div class="line number4 index3 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">interf </code><code class="vbnet keyword">As</code> <code class="vbnet plain">DWSIM_Automation.Automation</code></div><div class="line number5 index4 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Set</code> <code class="vbnet plain">interf = </code><code class="vbnet keyword">New</code> <code class="vbnet plain">DWSIM_Automation.Automation</code></div><div class="line number6 index5 alt1"><code class="vbnet spaces"> </code> </div><div class="line number7 index6 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'declare the flowsheet variable</code></div><div class="line number8 index7 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">sim </code><code class="vbnet keyword">As</code> <code class="vbnet plain">DWSIM_Interfaces.IFlowsheet</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'load Cavett's Problem simulation file</code></div><div class="line number11 index10 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Set</code> <code class="vbnet plain">sim = interf.LoadFlowsheet(Application.ActiveWorkbook.Path & </code><code class="vbnet string">"\Cavett's Problem.dwxml"</code><code class="vbnet plain">)</code></div><div class="line number12 index11 alt1"><code class="vbnet spaces"> </code> </div><div class="line number13 index12 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'use CAPE-OPEN interfaces to manipulate objects</code></div><div class="line number14 index13 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">feed </code><code class="vbnet keyword">As</code> <code class="vbnet plain">CAPEOPEN110.ICapeThermoMaterialObject</code></div><div class="line number15 index14 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">vap_out </code><code class="vbnet keyword">As</code> <code class="vbnet plain">CAPEOPEN110.ICapeThermoMaterialObject</code></div><div class="line number16 index15 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">liq_out </code><code class="vbnet keyword">As</code> <code class="vbnet plain">CAPEOPEN110.ICapeThermoMaterialObject</code></div><div class="line number17 index16 alt2"><code class="vbnet spaces"> </code> </div><div class="line number18 index17 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Set</code> <code class="vbnet plain">feed = sim.GetFlowsheetSimulationObject(</code><code class="vbnet string">"2"</code><code class="vbnet plain">)</code></div><div class="line number19 index18 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Set</code> <code class="vbnet plain">vap_out = sim.GetFlowsheetSimulationObject(</code><code class="vbnet string">"8"</code><code class="vbnet plain">)</code></div><div class="line number20 index19 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Set</code> <code class="vbnet plain">liq_out = sim.GetFlowsheetSimulationObject(</code><code class="vbnet string">"18"</code><code class="vbnet plain">)</code></div><div class="line number21 index20 alt2"><code class="vbnet spaces"> </code> </div><div class="line number22 index21 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'mass flow rate values in kg/s</code></div><div class="line number23 index22 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">flows(4) </code><code class="vbnet keyword">As</code> <code class="vbnet keyword">Variant</code></div><div class="line number24 index23 alt1"><code class="vbnet spaces"> </code> </div><div class="line number25 index24 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">flows(0) = 170#</code></div><div class="line number26 index25 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">flows(1) = 180#</code></div><div class="line number27 index26 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">flows(2) = 190#</code></div><div class="line number28 index27 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">flows(3) = 200#</code></div><div class="line number29 index28 alt2"><code class="vbnet spaces"> </code> </div><div class="line number30 index29 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'vapor and liquid flows</code></div><div class="line number31 index30 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">vflow, lflow </code><code class="vbnet keyword">As</code> <code class="vbnet keyword">Double</code></div><div class="line number32 index31 alt1"><code class="vbnet spaces"> </code> </div><div class="line number33 index32 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">For</code> <code class="vbnet plain">i = 0 </code><code class="vbnet keyword">To</code> <code class="vbnet plain">3</code></div><div class="line number34 index33 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'set feed mass flow</code></div><div class="line number35 index34 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Call</code> <code class="vbnet plain">feed.SetProp(</code><code class="vbnet string">"totalflow"</code><code class="vbnet plain">, </code><code class="vbnet string">"overall"</code><code class="vbnet plain">, </code><code class="vbnet keyword">Nothing</code><code class="vbnet plain">, </code><code class="vbnet string">""</code><code class="vbnet plain">, </code><code class="vbnet string">"mass"</code><code class="vbnet plain">, Array(flows(i)))</code></div><div class="line number36 index35 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'calculate the flowsheet (run the simulation)</code></div><div class="line number37 index36 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">MsgBox </code><code class="vbnet string">"Running simulation with F = "</code> <code class="vbnet plain">& flows(i) & </code><code class="vbnet string">" kg/s, please wait..."</code></div><div class="line number38 index37 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Call</code> <code class="vbnet plain">interf.CalculateFlowsheet(sim, </code><code class="vbnet keyword">Nothing</code><code class="vbnet plain">)</code></div><div class="line number39 index38 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'check for errors during the last run</code></div><div class="line number40 index39 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">If</code> <code class="vbnet plain">sim.Solved = </code><code class="vbnet keyword">False</code> <code class="vbnet keyword">Then</code></div><div class="line number41 index40 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">MsgBox </code><code class="vbnet string">"Error solving flowsheet: "</code> <code class="vbnet plain">& sim.ErrorMessage</code></div><div class="line number42 index41 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">End</code> <code class="vbnet keyword">If</code></div><div class="line number43 index42 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'get vapor outlet mass flow value</code></div><div class="line number44 index43 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">vflow = vap_out.GetProp(</code><code class="vbnet string">"totalflow"</code><code class="vbnet plain">, </code><code class="vbnet string">"overall"</code><code class="vbnet plain">, </code><code class="vbnet keyword">Nothing</code><code class="vbnet plain">, </code><code class="vbnet string">""</code><code class="vbnet plain">, </code><code class="vbnet string">"mass"</code><code class="vbnet plain">)(0)</code></div><div class="line number45 index44 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'get liquid outlet mass flow value</code></div><div class="line number46 index45 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">lflow = liq_out.GetProp(</code><code class="vbnet string">"totalflow"</code><code class="vbnet plain">, </code><code class="vbnet string">"overall"</code><code class="vbnet plain">, </code><code class="vbnet keyword">Nothing</code><code class="vbnet plain">, </code><code class="vbnet string">""</code><code class="vbnet plain">, </code><code class="vbnet string">"mass"</code><code class="vbnet plain">)(0)</code></div><div class="line number47 index46 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'display results</code></div><div class="line number48 index47 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">MsgBox </code><code class="vbnet string">"Simulation run #"</code> <code class="vbnet plain">& (i + 1) & </code><code class="vbnet string">" results:"</code> <code class="vbnet plain">& vbCrLf & </code><code class="vbnet string">"Feed: "</code> <code class="vbnet plain">& flows(i) & </code><code class="vbnet string">", Vapor: "</code> <code class="vbnet plain">& vflow & </code><code class="vbnet string">", Liquid: "</code> <code class="vbnet plain">& lflow & </code><code class="vbnet string">" kg/s"</code> <code class="vbnet plain">& vbCrLf & </code><code class="vbnet string">"Mass balance error: "</code> <code class="vbnet plain">& (flows(i) - vflow - lflow) & </code><code class="vbnet string">" kg/s"</code></div><div class="line number49 index48 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Next</code></div><div class="line number50 index49 alt1"><code class="vbnet spaces"> </code> </div><div class="line number51 index50 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">MsgBox </code><code class="vbnet string">"Finished OK!"</code></div><div class="line number52 index51 alt1"><code class="vbnet spaces"> </code> </div><div class="line number53 index52 alt2"><code class="vbnet keyword">End</code> <code class="vbnet keyword">Sub</code></div></div></td></tr></tbody></table></div></div> | |
− | + | <h2><span class="mw-headline" id="VB.NET">VB.NET</span></h2> | |
− | + | <p>To run this sample, create a new VB.NET Console Application project and add a reference to <b>DWSIM.Automation.dll</b>, <b>DWSIM.Interfaces.dll</b> and <b>CapeOpen.dll</b>. | |
− | [ | + | </p> |
− | + | <h3><span class="mw-headline" id="Code_2">Code</span></h3> | |
− | + | <div><div id="highlighter_346470" class="syntaxhighlighter vbnet"><div class="toolbar"><span><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div><div class="line number36 index35 alt1">36</div><div class="line number37 index36 alt2">37</div><div class="line number38 index37 alt1">38</div><div class="line number39 index38 alt2">39</div><div class="line number40 index39 alt1">40</div><div class="line number41 index40 alt2">41</div><div class="line number42 index41 alt1">42</div><div class="line number43 index42 alt2">43</div><div class="line number44 index43 alt1">44</div><div class="line number45 index44 alt2">45</div><div class="line number46 index45 alt1">46</div><div class="line number47 index46 alt2">47</div><div class="line number48 index47 alt1">48</div><div class="line number49 index48 alt2">49</div><div class="line number50 index49 alt1">50</div><div class="line number51 index50 alt2">51</div><div class="line number52 index51 alt1">52</div><div class="line number53 index52 alt2">53</div><div class="line number54 index53 alt1">54</div><div class="line number55 index54 alt2">55</div><div class="line number56 index55 alt1">56</div><div class="line number57 index56 alt2">57</div><div class="line number58 index57 alt1">58</div><div class="line number59 index58 alt2">59</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="vbnet keyword">Module</code> <code class="vbnet plain">Module1</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Sub</code> <code class="vbnet plain">Main()</code></div><div class="line number4 index3 alt1"> </div><div class="line number5 index4 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'create automation manager</code></div><div class="line number6 index5 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">interf </code><code class="vbnet keyword">As</code> <code class="vbnet keyword">New</code> <code class="vbnet plain">DWSIM.Automation.Automation</code></div><div class="line number7 index6 alt2"> </div><div class="line number8 index7 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">sim </code><code class="vbnet keyword">As</code> <code class="vbnet plain">Interfaces.IFlowsheet</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'load Cavett's Problem simulation file</code></div><div class="line number11 index10 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">sim = interf.LoadFlowsheet(</code><code class="vbnet string">"samples"</code> <code class="vbnet plain">& IO.Path.DirectorySeparatorChar & </code><code class="vbnet string">"Cavett's Problem.dwxml"</code><code class="vbnet plain">)</code></div><div class="line number12 index11 alt1"> </div><div class="line number13 index12 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'(optional) set a listener to catch solver messages</code></div><div class="line number14 index13 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">sim.SetMessageListener(</code><code class="vbnet keyword">Sub</code><code class="vbnet plain">(msg </code><code class="vbnet keyword">As</code> <code class="vbnet keyword">String</code><code class="vbnet plain">)</code></div><div class="line number15 index14 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">Console.WriteLine(msg)</code></div><div class="line number16 index15 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">End</code> <code class="vbnet keyword">Sub</code><code class="vbnet plain">)</code></div><div class="line number17 index16 alt2"> </div><div class="line number18 index17 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'use CAPE-OPEN interfaces to manipulate objects</code></div><div class="line number19 index18 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">feed, vap_out, liq_out </code><code class="vbnet keyword">As</code> <code class="vbnet plain">CapeOpen.ICapeThermoMaterialObject</code></div><div class="line number20 index19 alt1"> </div><div class="line number21 index20 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">feed = sim.GetFlowsheetSimulationObject1(</code><code class="vbnet string">"2"</code><code class="vbnet plain">)</code></div><div class="line number22 index21 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">vap_out = sim.GetFlowsheetSimulationObject1(</code><code class="vbnet string">"8"</code><code class="vbnet plain">)</code></div><div class="line number23 index22 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">liq_out = sim.GetFlowsheetSimulationObject1(</code><code class="vbnet string">"18"</code><code class="vbnet plain">)</code></div><div class="line number24 index23 alt1"> </div><div class="line number25 index24 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'mass flow rate values in kg/s</code></div><div class="line number26 index25 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">flows(3) </code><code class="vbnet keyword">As</code> <code class="vbnet keyword">Double</code></div><div class="line number27 index26 alt2"> </div><div class="line number28 index27 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">flows(0) = 170.0#</code></div><div class="line number29 index28 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">flows(1) = 180.0#</code></div><div class="line number30 index29 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">flows(2) = 190.0#</code></div><div class="line number31 index30 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">flows(3) = 200.0#</code></div><div class="line number32 index31 alt1"> </div><div class="line number33 index32 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'vapor and liquid flows</code></div><div class="line number34 index33 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Dim</code> <code class="vbnet plain">vflow, lflow </code><code class="vbnet keyword">As</code> <code class="vbnet keyword">Double</code></div><div class="line number35 index34 alt2"> </div><div class="line number36 index35 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">For</code> <code class="vbnet plain">i = 0 </code><code class="vbnet keyword">To</code> <code class="vbnet plain">flows.Length - 1</code></div><div class="line number37 index36 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'set feed mass flow</code></div><div class="line number38 index37 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">feed.SetProp(</code><code class="vbnet string">"totalflow"</code><code class="vbnet plain">, </code><code class="vbnet string">"overall"</code><code class="vbnet plain">, </code><code class="vbnet keyword">Nothing</code><code class="vbnet plain">, </code><code class="vbnet string">""</code><code class="vbnet plain">, </code><code class="vbnet string">"mass"</code><code class="vbnet plain">, </code><code class="vbnet keyword">New</code> <code class="vbnet keyword">Double</code><code class="vbnet plain">() {flows(i)})</code></div><div class="line number39 index38 alt2"><code class="vbnet spaces"> </code><code class="vbnet comments">'calculate the flowsheet (run the simulation)</code></div><div class="line number40 index39 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">Console.WriteLine(</code><code class="vbnet string">"Running simulation with F = "</code> <code class="vbnet plain">& flows(i) & </code><code class="vbnet string">" kg/s, please wait..."</code><code class="vbnet plain">)</code></div><div class="line number41 index40 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">interf.CalculateFlowsheet(sim, </code><code class="vbnet keyword">Nothing</code><code class="vbnet plain">)</code></div><div class="line number42 index41 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'check for errors during the last run</code></div><div class="line number43 index42 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">If</code> <code class="vbnet plain">sim.Solved = </code><code class="vbnet keyword">False</code> <code class="vbnet keyword">Then</code></div><div class="line number44 index43 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">Console.WriteLine(</code><code class="vbnet string">"Error solving flowsheet: "</code> <code class="vbnet plain">& sim.ErrorMessage)</code></div><div class="line number45 index44 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">End</code> <code class="vbnet keyword">If</code></div><div class="line number46 index45 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'get vapor outlet mass flow value</code></div><div class="line number47 index46 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">vflow = vap_out.GetProp(</code><code class="vbnet string">"totalflow"</code><code class="vbnet plain">, </code><code class="vbnet string">"overall"</code><code class="vbnet plain">, </code><code class="vbnet keyword">Nothing</code><code class="vbnet plain">, </code><code class="vbnet string">""</code><code class="vbnet plain">, </code><code class="vbnet string">"mass"</code><code class="vbnet plain">)(0)</code></div><div class="line number48 index47 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'get liquid outlet mass flow value</code></div><div class="line number49 index48 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">lflow = liq_out.GetProp(</code><code class="vbnet string">"totalflow"</code><code class="vbnet plain">, </code><code class="vbnet string">"overall"</code><code class="vbnet plain">, </code><code class="vbnet keyword">Nothing</code><code class="vbnet plain">, </code><code class="vbnet string">""</code><code class="vbnet plain">, </code><code class="vbnet string">"mass"</code><code class="vbnet plain">)(0)</code></div><div class="line number50 index49 alt1"><code class="vbnet spaces"> </code><code class="vbnet comments">'display results</code></div><div class="line number51 index50 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">Console.WriteLine(</code><code class="vbnet string">"Simulation run #"</code> <code class="vbnet plain">& (i + 1) & </code><code class="vbnet string">" results:"</code> <code class="vbnet plain">& vbCrLf & </code><code class="vbnet string">"Feed: "</code> <code class="vbnet plain">& flows(i) & </code><code class="vbnet string">", Vapor: "</code> <code class="vbnet plain">& vflow & </code><code class="vbnet string">", Liquid: "</code> <code class="vbnet plain">& lflow & </code><code class="vbnet string">" kg/s"</code> <code class="vbnet plain">& vbCrLf & </code><code class="vbnet string">"Mass balance error: "</code> <code class="vbnet plain">& (flows(i) - vflow - lflow) & </code><code class="vbnet string">" kg/s"</code><code class="vbnet plain">)</code></div><div class="line number52 index51 alt1"><code class="vbnet spaces"> </code><code class="vbnet keyword">Next</code></div><div class="line number53 index52 alt2"> </div><div class="line number54 index53 alt1"><code class="vbnet spaces"> </code><code class="vbnet plain">Console.WriteLine(</code><code class="vbnet string">"Finished OK! Press any key to close."</code><code class="vbnet plain">)</code></div><div class="line number55 index54 alt2"><code class="vbnet spaces"> </code><code class="vbnet plain">Console.ReadKey()</code></div><div class="line number56 index55 alt1"> </div><div class="line number57 index56 alt2"><code class="vbnet spaces"> </code><code class="vbnet keyword">End</code> <code class="vbnet keyword">Sub</code></div><div class="line number58 index57 alt1"> </div><div class="line number59 index58 alt2"><code class="vbnet keyword">End</code> <code class="vbnet keyword">Module</code></div></div></td></tr></tbody></table></div></div> | |
− | + | <h2><span class="mw-headline" id="C.23">C#</span></h2> | |
− | + | <p>To run this sample, create a new C# Console Application project and add a reference to <b>DWSIM.Automation.dll</b>, <b>DWSIM.Interfaces.dll</b> and <b>CapeOpen.dll</b>. | |
− | + | </p> | |
− | + | <h3><span class="mw-headline" id="Code_3">Code</span></h3> | |
− | + | <div><div id="highlighter_705174" class="syntaxhighlighter csharp"><div class="toolbar"><span><a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div><div class="line number36 index35 alt1">36</div><div class="line number37 index36 alt2">37</div><div class="line number38 index37 alt1">38</div><div class="line number39 index38 alt2">39</div><div class="line number40 index39 alt1">40</div><div class="line number41 index40 alt2">41</div><div class="line number42 index41 alt1">42</div><div class="line number43 index42 alt2">43</div><div class="line number44 index43 alt1">44</div><div class="line number45 index44 alt2">45</div><div class="line number46 index45 alt1">46</div><div class="line number47 index46 alt2">47</div><div class="line number48 index47 alt1">48</div><div class="line number49 index48 alt2">49</div><div class="line number50 index49 alt1">50</div><div class="line number51 index50 alt2">51</div><div class="line number52 index51 alt1">52</div><div class="line number53 index52 alt2">53</div><div class="line number54 index53 alt1">54</div><div class="line number55 index54 alt2">55</div><div class="line number56 index55 alt1">56</div><div class="line number57 index56 alt2">57</div><div class="line number58 index57 alt1">58</div><div class="line number59 index58 alt2">59</div><div class="line number60 index59 alt1">60</div><div class="line number61 index60 alt2">61</div><div class="line number62 index61 alt1">62</div><div class="line number63 index62 alt2">63</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp keyword">using</code> <code class="csharp plain">System;</code></div><div class="line number2 index1 alt1"><code class="csharp keyword">using</code> <code class="csharp plain">System.Collections;</code></div><div class="line number3 index2 alt2"><code class="csharp keyword">using</code> <code class="csharp plain">System.Collections.Generic;</code></div><div class="line number4 index3 alt1"><code class="csharp keyword">using</code> <code class="csharp plain">System.Data;</code></div><div class="line number5 index4 alt2"><code class="csharp keyword">using</code> <code class="csharp plain">System.Diagnostics;</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="csharp keyword">static</code> <code class="csharp keyword">class</code> <code class="csharp plain">Module1</code></div><div class="line number8 index7 alt1"><code class="csharp plain">{</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp keyword">void</code> <code class="csharp plain">Main()</code></div><div class="line number12 index11 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number13 index12 alt2"><code class="csharp spaces"> </code><code class="csharp comments">//create automation manager</code></div><div class="line number14 index13 alt1"><code class="csharp spaces"> </code><code class="csharp plain">DWSIM.Automation.Automation interf = </code><code class="csharp keyword">new</code> <code class="csharp plain">DWSIM.Automation.Automation();</code></div><div class="line number15 index14 alt2"> </div><div class="line number16 index15 alt1"><code class="csharp spaces"> </code><code class="csharp plain">Interfaces.IFlowsheet sim;</code></div><div class="line number17 index16 alt2"> </div><div class="line number18 index17 alt1"><code class="csharp spaces"> </code><code class="csharp comments">//load Cavett's Problem simulation file</code></div><div class="line number19 index18 alt2"><code class="csharp spaces"> </code><code class="csharp plain">sim = interf.LoadFlowsheet(</code><code class="csharp string">"samples"</code> <code class="csharp plain">+ IO.Path.DirectorySeparatorChar + </code><code class="csharp string">"Cavett's Problem.dwxml"</code><code class="csharp plain">);</code></div><div class="line number20 index19 alt1"> </div><div class="line number21 index20 alt2"><code class="csharp spaces"> </code><code class="csharp comments">//use CAPE-OPEN interfaces to manipulate objects</code></div><div class="line number22 index21 alt1"><code class="csharp spaces"> </code><code class="csharp plain">CapeOpen.ICapeThermoMaterialObject feed, vap_out, liq_out;</code></div><div class="line number23 index22 alt2"> </div><div class="line number24 index23 alt1"><code class="csharp spaces"> </code><code class="csharp plain">feed = sim.GetFlowsheetSimulationObject1(</code><code class="csharp string">"2"</code><code class="csharp plain">);</code></div><div class="line number25 index24 alt2"><code class="csharp spaces"> </code><code class="csharp plain">vap_out = sim.GetFlowsheetSimulationObject1(</code><code class="csharp string">"8"</code><code class="csharp plain">);</code></div><div class="line number26 index25 alt1"><code class="csharp spaces"> </code><code class="csharp plain">liq_out = sim.GetFlowsheetSimulationObject1(</code><code class="csharp string">"18"</code><code class="csharp plain">);</code></div><div class="line number27 index26 alt2"> </div><div class="line number28 index27 alt1"><code class="csharp spaces"> </code><code class="csharp comments">//mass flow rate values in kg/s</code></div><div class="line number29 index28 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">double</code><code class="csharp plain">[] flows = </code><code class="csharp keyword">new</code> <code class="csharp keyword">double</code><code class="csharp plain">[4];</code></div><div class="line number30 index29 alt1"> </div><div class="line number31 index30 alt2"><code class="csharp spaces"> </code><code class="csharp plain">flows[0] = 170.0;</code></div><div class="line number32 index31 alt1"><code class="csharp spaces"> </code><code class="csharp plain">flows[1] = 180.0;</code></div><div class="line number33 index32 alt2"><code class="csharp spaces"> </code><code class="csharp plain">flows[2] = 190.0;</code></div><div class="line number34 index33 alt1"><code class="csharp spaces"> </code><code class="csharp plain">flows[3] = 200.0;</code></div><div class="line number35 index34 alt2"> </div><div class="line number36 index35 alt1"><code class="csharp spaces"> </code><code class="csharp comments">//vapor and liquid flows</code></div><div class="line number37 index36 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">double</code> <code class="csharp plain">vflow = 0;</code></div><div class="line number38 index37 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">double</code> <code class="csharp plain">lflow = 0;</code></div><div class="line number39 index38 alt2"> </div><div class="line number40 index39 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">for</code> <code class="csharp plain">(i = 0; i <= flows.Length - 1; i++) {</code></div><div class="line number41 index40 alt2"><code class="csharp spaces"> </code><code class="csharp comments">//set feed mass flow</code></div><div class="line number42 index41 alt1"><code class="csharp spaces"> </code><code class="csharp plain">feed.SetProp(</code><code class="csharp string">"totalflow"</code><code class="csharp plain">, </code><code class="csharp string">"overall"</code><code class="csharp plain">, </code><code class="csharp keyword">null</code><code class="csharp plain">, </code><code class="csharp string">""</code><code class="csharp plain">, </code><code class="csharp string">"mass"</code><code class="csharp plain">, </code><code class="csharp keyword">new</code> <code class="csharp keyword">double</code><code class="csharp plain">[] { flows(i) });</code></div><div class="line number43 index42 alt2"><code class="csharp spaces"> </code><code class="csharp comments">//calculate the flowsheet (run the simulation)</code></div><div class="line number44 index43 alt1"><code class="csharp spaces"> </code><code class="csharp plain">Console.WriteLine(</code><code class="csharp string">"Running simulation with F = "</code> <code class="csharp plain">+ flows(i) + </code><code class="csharp string">" kg/s, please wait..."</code><code class="csharp plain">);</code></div><div class="line number45 index44 alt2"><code class="csharp spaces"> </code><code class="csharp plain">interf.CalculateFlowsheet(sim, </code><code class="csharp keyword">null</code><code class="csharp plain">);</code></div><div class="line number46 index45 alt1"><code class="csharp spaces"> </code><code class="csharp comments">//check for errors during the last run</code></div><div class="line number47 index46 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">if</code> <code class="csharp plain">(sim.Solved == </code><code class="csharp keyword">false</code><code class="csharp plain">) {</code></div><div class="line number48 index47 alt1"><code class="csharp spaces"> </code><code class="csharp plain">Console.WriteLine(</code><code class="csharp string">"Error solving flowsheet: "</code> <code class="csharp plain">+ sim.ErrorMessage);</code></div><div class="line number49 index48 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div><div class="line number50 index49 alt1"><code class="csharp spaces"> </code><code class="csharp comments">//get vapor outlet mass flow value</code></div><div class="line number51 index50 alt2"><code class="csharp spaces"> </code><code class="csharp plain">vflow = vap_out.GetProp(</code><code class="csharp string">"totalflow"</code><code class="csharp plain">, </code><code class="csharp string">"overall"</code><code class="csharp plain">, </code><code class="csharp keyword">null</code><code class="csharp plain">, </code><code class="csharp string">""</code><code class="csharp plain">, </code><code class="csharp string">"mass"</code><code class="csharp plain">)(0);</code></div><div class="line number52 index51 alt1"><code class="csharp spaces"> </code><code class="csharp comments">//get liquid outlet mass flow value</code></div><div class="line number53 index52 alt2"><code class="csharp spaces"> </code><code class="csharp plain">lflow = liq_out.GetProp(</code><code class="csharp string">"totalflow"</code><code class="csharp plain">, </code><code class="csharp string">"overall"</code><code class="csharp plain">, </code><code class="csharp keyword">null</code><code class="csharp plain">, </code><code class="csharp string">""</code><code class="csharp plain">, </code><code class="csharp string">"mass"</code><code class="csharp plain">)(0);</code></div><div class="line number54 index53 alt1"><code class="csharp spaces"> </code><code class="csharp comments">//display results</code></div><div class="line number55 index54 alt2"><code class="csharp spaces"> </code><code class="csharp plain">Console.WriteLine(</code><code class="csharp string">"Simulation run #"</code> <code class="csharp plain">+ (i + 1) + </code><code class="csharp string">" results:\nFeed: "</code> <code class="csharp plain">+ flows(i) + </code><code class="csharp string">", Vapor: "</code> <code class="csharp plain">+ vflow + </code><code class="csharp string">", Liquid: "</code> <code class="csharp plain">+ lflow + </code><code class="csharp string">" kg/s\nMass balance error: "</code> <code class="csharp plain">+ (flows(i) - vflow - lflow) + </code><code class="csharp string">" kg/s"</code><code class="csharp plain">);</code></div><div class="line number56 index55 alt1"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div><div class="line number57 index56 alt2"> </div><div class="line number58 index57 alt1"><code class="csharp spaces"> </code><code class="csharp plain">Console.WriteLine(</code><code class="csharp string">"Finished OK! Press any key to close."</code><code class="csharp plain">);</code></div><div class="line number59 index58 alt2"><code class="csharp spaces"> </code><code class="csharp plain">Console.ReadKey();</code></div><div class="line number60 index59 alt1"> </div><div class="line number61 index60 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div><div class="line number62 index61 alt1"> </div><div class="line number63 index62 alt2"><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div> | |
− | [ | ||
− | |||
− | |||
− | [ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 20:02, 26 June 2019
Automation enables software packages to expose their unique features to scripting tools and other applications. Using Automation, you can:
- Create applications and programming tools that expose objects.
- Create and manipulate objects exposed in one application from another application.
- Create tools that access and manipulate objects. These tools can include embedded macro languages, external programming tools, object browsers, and compilers.
On a Windows environment, the objects an application or programming tool exposes are called ActiveX objects. Applications and programming tools that access those objects are called ActiveX clients. ActiveX objects and clients interact as follows: Applications and other software packages that support ActiveX technology define and expose objects which can be acted on by ActiveX components. ActiveX components are physical files (for example .exe and .dll files) that contain classes, which are definitions of objects. Type information describes the exposed objects, and can be used by ActiveX components at either compile time or at run time.
Contents
Contents
[<a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#" id="togglelink">hide</a>]- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Automation_support_in_DWSIM">1 Automation support in DWSIM</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Registering_DLLs_for_COM_Automation">2 Registering DLLs for COM Automation</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Introduction_to_Interfaces">3 Introduction to Interfaces</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#API_Reference_Documentation">4 API Reference Documentation</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#DWSIM_Flowsheet_Class_Structure">5 DWSIM Flowsheet Class Structure</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Sample_Automation">6 Sample Automation</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#About_Cavett.27s_Problem">6.1 About Cavett's Problem</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Excel_VBA">6.2 Excel VBA</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Code">6.2.1 Code</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#VB.NET">6.3 VB.NET</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Code_2">6.3.1 Code</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#C.23">6.4 C#</a>
- <a href="http://dwsim.inforside.com.br/wiki/index.php?title=Automation#Code_3">6.4.1 Code</a>
Automation support in DWSIM
Starting from version 4.2, DWSIM exposes its main Classes and Interfaces to Automation via COM/.NET. Automating DWSIM enables you to manipulate flowsheets and run sensitivity/optimization studies directly from Microsoft Excel, for instance, without the need of opening DWSIM directly. Interfacing DWSIM with Excel through VBA macros results in a powerful tool in process engineering activities, such as design, optimization and process evaluation.
The simulation results may be output to an Excel spreadsheet in the development of the Heat and Material Balance for the process design, enabling quick manipulation of the resulting data through a well-known tool for every Chemical Engineer.
Registering DLLs for COM Automation
You can register DWSIM DLLs for automation during the installation process. You can also run the automation_reg.bat batch file (located in DWSIM's current installation directory) with admin privileges to register. To de-register, run automation_unreg.bat also as admin. When you uninstall DWSIM, the DLLs are automatically deregistered.
If your automation project is based on a .NET language, there's no need to register the DLLs. You'll only need to add a reference to them.
Automating DWSIM through COM is limited to Windows, though .NET is recommended as the default mechanism. On a Linux environment, you can use Mono to create and/or run an automation project in C#.
Introduction to Interfaces
Before proceeding, read this text to get used to Interfaces and their implementation in actual Classes: <a rel="nofollow" class="external text" href="http://www.cs.utah.edu/~germain/PPS/Topics/interfaces.html">Interfaces in Object-Oriented Programming</a>
API Reference Documentation
- Automation Class: <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/N_DWSIM_Automation.htm">[1]</a>
- Interface Definitions: <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/G_DWSIM_Interfaces.htm">[2]</a>
- Unit Operations: <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/G_DWSIM_UnitOperations.htm">[3]</a>
- Thermodynamics: <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/G_DWSIM_Thermodynamics.htm">[4]</a>
- Base Class Shared Library: <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/G_DWSIM_SharedClasses.htm">[5]</a>
- Flowsheet GUI and DWSIM main executable: <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/N_DWSIM.htm">[6]</a>
- CAPE-OPEN Reference: <a rel="nofollow" class="external autonumber" href="http://www.colan.org/specifications/">[7]</a>
DWSIM Flowsheet Class Structure
The Flowsheet class in DWSIM provides access to all objects in the simulation:
- Thermodynamics Subsystem: includes Compounds, Property Packages, Flash Algorithms and Reactions/Reaction Sets collections.
- Simulation Objects Subsystem: includes Material & Energy Streams and Unit Operation blocks.
- Graphical User Interface: provides access to the displayed objects in the flowsheet and the connections between them.
- Accessories: includes added utilities, sensitivity & optimization studies, system of units definitions and other simulation definitions.
The <a rel="nofollow" class="external text" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_FormFlowsheet.htm">Flowsheet</a> object in DWSIM implements various interfaces, including IFlowsheet, IFlowsheetBag, IFlowsheetGUI and IFlowsheetOptions.
IFlowsheet: this is the main interface implemented by the Flowsheet class. It provides direct access to the various flowsheet components and helper functions to manipulate objects. <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_Interfaces_IFlowsheet.htm">[8]</a>
IFlowsheetBag: provides direct access to collections of flowsheet objects. <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_Interfaces_IFlowsheetBag.htm">[9]</a>
IFlowsheetGUI: this is an interface which defines helper functions to a Flowsheet GUI implementation. <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_Interfaces_IFlowsheetGUI.htm">[10]</a>
IFlowsheetOptions: this interface defines the flowsheet settings and other properties. <a rel="nofollow" class="external autonumber" href="http://dwsim.inforside.com.br/api_help57/html/T_DWSIM_Interfaces_IFlowsheetOptions.htm">[11]</a>
When you use the <a rel="nofollow" class="external text" href="http://dwsim.inforside.com.br/api_help57/html/N_DWSIM_Automation.htm">Automation</a> class to load a simulation, an IFlowsheet object is returned, which is actually an instance of the Flowsheet class. You can cast the returned object to any of the interfaces implemented by the Flowsheet class to access all available functions, properties and procedures.
Sample Automation
This sample automation code will run Cavett's Problem (simulation file located in the samples folder) with four different feed mass flow values, check outlet mass flows and calculate the mass balance of the flowsheet, displaying the results to the user.
About Cavett's Problem
A simulation problem proposed by Cavett (1963) has been used to test various chemical engineering simulation programs. It provides a useful benchmark to compare and contrast various tear stream locations and convergence algorithms. The process is equivalent to a four theoretical stage near isothermal distillation flash tanks.
<a href="http://dwsim.inforside.com.br/wiki/index.php?title=File:Cavett.jpg" class="image"><img alt="Cavett.jpg" src="./automation_files/Cavett.jpg" width="636" height="650"></a>
- Feed Stream: 2
- Vapor Outlet Stream: 8
- Liquid Outlet Stream: 18
Excel VBA
To run this sample, create a new Excel VBA project and add a reference to CAPE-OPEN 1.1 Type Library (<a rel="nofollow" class="external free" href="http://www.colan.org/software-tools/cape-open-type-libraries-and-primary-interop-assemblies/">http://www.colan.org/software-tools/cape-open-type-libraries-and-primary-interop-assemblies/</a>), DWSIM Simulator Automation Interface and DWSIM Simulator Interface Definitions Library.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | Public Sub Sub1() 'create automation manager Dim interf As DWSIM_Automation.Automation Set interf = New DWSIM_Automation.Automation 'declare the flowsheet variable Dim sim As DWSIM_Interfaces.IFlowsheet 'load Cavett's Problem simulation file Set sim = interf.LoadFlowsheet(Application.ActiveWorkbook.Path & "\Cavett's Problem.dwxml" ) 'use CAPE-OPEN interfaces to manipulate objects Dim feed As CAPEOPEN110.ICapeThermoMaterialObject Dim vap_out As CAPEOPEN110.ICapeThermoMaterialObject Dim liq_out As CAPEOPEN110.ICapeThermoMaterialObject Set feed = sim.GetFlowsheetSimulationObject( "2" ) Set vap_out = sim.GetFlowsheetSimulationObject( "8" ) Set liq_out = sim.GetFlowsheetSimulationObject( "18" ) 'mass flow rate values in kg/s Dim flows(4) As Variant flows(0) = 170# flows(1) = 180# flows(2) = 190# flows(3) = 200# 'vapor and liquid flows Dim vflow, lflow As Double For i = 0 To 3 'set feed mass flow Call feed.SetProp( "totalflow" , "overall" , Nothing , "" , "mass" , Array(flows(i))) 'calculate the flowsheet (run the simulation) MsgBox "Running simulation with F = " & flows(i) & " kg/s, please wait..." Call interf.CalculateFlowsheet(sim, Nothing ) 'check for errors during the last run If sim.Solved = False Then MsgBox "Error solving flowsheet: " & sim.ErrorMessage End If 'get vapor outlet mass flow value vflow = vap_out.GetProp( "totalflow" , "overall" , Nothing , "" , "mass" )(0) 'get liquid outlet mass flow value lflow = liq_out.GetProp( "totalflow" , "overall" , Nothing , "" , "mass" )(0) 'display results MsgBox "Simulation run #" & (i + 1) & " results:" & vbCrLf & "Feed: " & flows(i) & ", Vapor: " & vflow & ", Liquid: " & lflow & " kg/s" & vbCrLf & "Mass balance error: " & (flows(i) - vflow - lflow) & " kg/s" Next MsgBox "Finished OK!" End Sub |
VB.NET
To run this sample, create a new VB.NET Console Application project and add a reference to DWSIM.Automation.dll, DWSIM.Interfaces.dll and CapeOpen.dll.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | Module Module1 Sub Main() 'create automation manager Dim interf As New DWSIM.Automation.Automation Dim sim As Interfaces.IFlowsheet 'load Cavett's Problem simulation file sim = interf.LoadFlowsheet( "samples" & IO.Path.DirectorySeparatorChar & "Cavett's Problem.dwxml" ) '(optional) set a listener to catch solver messages sim.SetMessageListener( Sub (msg As String ) Console.WriteLine(msg) End Sub ) 'use CAPE-OPEN interfaces to manipulate objects Dim feed, vap_out, liq_out As CapeOpen.ICapeThermoMaterialObject feed = sim.GetFlowsheetSimulationObject1( "2" ) vap_out = sim.GetFlowsheetSimulationObject1( "8" ) liq_out = sim.GetFlowsheetSimulationObject1( "18" ) 'mass flow rate values in kg/s Dim flows(3) As Double flows(0) = 170.0# flows(1) = 180.0# flows(2) = 190.0# flows(3) = 200.0# 'vapor and liquid flows Dim vflow, lflow As Double For i = 0 To flows.Length - 1 'set feed mass flow feed.SetProp( "totalflow" , "overall" , Nothing , "" , "mass" , New Double () {flows(i)}) 'calculate the flowsheet (run the simulation) Console.WriteLine( "Running simulation with F = " & flows(i) & " kg/s, please wait..." ) interf.CalculateFlowsheet(sim, Nothing ) 'check for errors during the last run If sim.Solved = False Then Console.WriteLine( "Error solving flowsheet: " & sim.ErrorMessage) End If 'get vapor outlet mass flow value vflow = vap_out.GetProp( "totalflow" , "overall" , Nothing , "" , "mass" )(0) 'get liquid outlet mass flow value lflow = liq_out.GetProp( "totalflow" , "overall" , Nothing , "" , "mass" )(0) 'display results Console.WriteLine( "Simulation run #" & (i + 1) & " results:" & vbCrLf & "Feed: " & flows(i) & ", Vapor: " & vflow & ", Liquid: " & lflow & " kg/s" & vbCrLf & "Mass balance error: " & (flows(i) - vflow - lflow) & " kg/s" ) Next Console.WriteLine( "Finished OK! Press any key to close." ) Console.ReadKey() End Sub End Module |
C#
To run this sample, create a new C# Console Application project and add a reference to DWSIM.Automation.dll, DWSIM.Interfaces.dll and CapeOpen.dll.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; static class Module1 { public static void Main() { //create automation manager DWSIM.Automation.Automation interf = new DWSIM.Automation.Automation(); Interfaces.IFlowsheet sim; //load Cavett's Problem simulation file sim = interf.LoadFlowsheet( "samples" + IO.Path.DirectorySeparatorChar + "Cavett's Problem.dwxml" ); //use CAPE-OPEN interfaces to manipulate objects CapeOpen.ICapeThermoMaterialObject feed, vap_out, liq_out; feed = sim.GetFlowsheetSimulationObject1( "2" ); vap_out = sim.GetFlowsheetSimulationObject1( "8" ); liq_out = sim.GetFlowsheetSimulationObject1( "18" ); //mass flow rate values in kg/s double [] flows = new double [4]; flows[0] = 170.0; flows[1] = 180.0; flows[2] = 190.0; flows[3] = 200.0; //vapor and liquid flows double vflow = 0; double lflow = 0; for (i = 0; i <= flows.Length - 1; i++) { //set feed mass flow feed.SetProp( "totalflow" , "overall" , null , "" , "mass" , new double [] { flows(i) }); //calculate the flowsheet (run the simulation) Console.WriteLine( "Running simulation with F = " + flows(i) + " kg/s, please wait..." ); interf.CalculateFlowsheet(sim, null ); //check for errors during the last run if (sim.Solved == false ) { Console.WriteLine( "Error solving flowsheet: " + sim.ErrorMessage); } //get vapor outlet mass flow value vflow = vap_out.GetProp( "totalflow" , "overall" , null , "" , "mass" )(0); //get liquid outlet mass flow value lflow = liq_out.GetProp( "totalflow" , "overall" , null , "" , "mass" )(0); //display results Console.WriteLine( "Simulation run #" + (i + 1) + " results:\nFeed: " + flows(i) + ", Vapor: " + vflow + ", Liquid: " + lflow + " kg/s\nMass balance error: " + (flows(i) - vflow - lflow) + " kg/s" ); } Console.WriteLine( "Finished OK! Press any key to close." ); Console.ReadKey(); } } |