DWSIM/C3/Custom-Unit-Operation-using-Python/English

From Script | Spoken-Tutorial
Revision as of 16:50, 12 July 2019 by Kaushik Datta (Talk | contribs)

Jump to: navigation, search
Time Narration
Slide Number 1

Title Slide

Welcome to this tutorial on simulating a Custom Unit Operation using Python.
Slide Number 2

Learning Objective

In this tutorial, we will learn to:
  • Create a custom unit operation using Python
  • Calculate Overall mass and mole flow rate
  • Calculate Enthalpy and outlet pressure

Slide Number 3

System Requirements

To record this tutorial, I am using
  • DWSIM 5. 6 update 8 (Classic UI) and
  • Windows 10

But, this process is identical in Linux, Mac OS X or FOSSEE OS on ARM.

Slide Number 4

Pre-requisites

To practice this tutorial, you should know to-
  • Add components to a flowsheet
  • Select thermodynamic packages
  • Add material streams and specify their properties
Slide Number 5

Prerequisite Tutorials and Files

https://spoken-tutorial.org

The prerequisite tutorials are available on our website, spoken-tutorial.org.


You can access these tutorials and all the associated files from this site.

Slide Number 6

Components: Methanol and Water


Property Package: Raoult’s Law

We will develop a flowsheet to determine the product stream temperature, pressure.

Molar flowrate, compositions and molar enthalpy from a mixer model created using Python.

Slide Number 7


Inlet Stream 1:


Component: Pure Methanol

Temperature: 300 K

Pressure: 101325 Pa

Mass Flow: 20 kg/s


Inlet Stream 2:


Component: Pure Water

Temperature: 320 K

Pressure: 202650 Pa

Mass Flow: 10 kg/s

Outlet Pressure: Average of Inlet Stream

Here we give Inlet Stream Conditions and Pressure calculation parameter of the product stream.
Slide Number 8


Code Files

DWSIM-Python file used in the tutorial is provided as a code file on our site.

Download the files from Code Files link.

I have already opened DWSIM on my machine.
Please ensure that DWSIM opens as an administrator.

Please note:

If DWSIM is not opened with administrator privileges, the Python unit operation will not work.

File >> Open File


Go to File menu and click on Open File.
Point to the file on the Desktop.

Select the file from the Desktop.

I have already downloaded and extracted the file on my Desktop.

Select the file from the Desktop.

This file already contains all the material streams necessary for this tutorial.

Click on Maximize button. Let us maximize the simulation window.
Cursor to User Models tab Now at the bottom of the main simulation window, go to User Models tab.
Here we will add the Python script in the flowsheet.
Click and drag Python Script to the flowsheet Drag and drop Python Script to the flowsheet area.
Type Custom Mixer Let us change the name of this Unit Operation to Custom Mixer.
Cursor on the interface. Now, we will connect material streams to inlet and outlet ports of the Custom Mixer.
Click Custom Mixer For this click on Custom Mixer.
Go to Connections

Click on drop down arrow against Inlet Stream 1

Select Methanol.

Under Connections, click on the drop-down against Inlet Stream 1.

And select Methanol.

Click on drop down arrow against Inlet Stream 2

Select Water.

Next, click on the drop-down against Inlet Stream 2 and select Water.
Click on drop down arrow against Outlet Stream 1


Select Mixed Product.

Then, click on the drop-down against Outlet Stream 1 and select Mixed Product.
Point to all the connections. All the connections are now complete.

Now we will write the code to perform the computation.

Point to the left of the window.


Click on Open Python Script Editor

On the left of the window, click on Open Python Script Editor button.
Cursor to Custom Mixer - Script Editor tab Custom Mixer - Script Editor tab opens.
Here we will type the Python script to perform the mixer operations.
First, we will import the thermodynamics package and its related functions to the script.
Type:

from DWSIM.Thermodynamics import *

Type this code in the script.


This is to import the Thermodynamics Packages and related functions to Python.

Now we will extract the input from the feed streams.
Type:


feed1=ims1

feed2=ims2

Type this code in the script.


ims1 and ims2 are the reference to material streams that are connected in Inlet Stream 1 and Inlet Stream 2.


They get recorded as feed1 and feed2.

Type:


P_1 = feed1.GetProp("pressure", "Overall", None, "", "")

P_2 = feed2.GetProp("pressure", "Overall", None, "", "")

Type this code in the script.


This is to read the pressure from inlet material streams.


Here, the variables P_1 and P_2 store the pressures of Methanol and Water streams.

Cursor to GetProp command GetProp is a function which is used to extract information from the inlet material stream.
Point to the arguments. GetProp function has 5 arguments.


These arguments have to be specified according to the information that needs to be extracted.

Cursor to first argument "pressure" keyword First argument is the property name.


This is to specify the property which has to be stored in the variable.


Here pressure is specified.

Cursor to second argument "Overall" keyword Second argument is the phase.


This is to indicate the phase for which the property specified in the first argument has to be stored.


Here Overall is specified.

Cursor to third argument "None" keyword. Third argument is the component ID.


Since we are storing the pressure of overall stream and not individual components, None is specified.

Cursor to fourth argument. The fourth argument is the calculation type.


Here we have to enter Pure or Mixture.


Since the property calculation type doesn’t apply to pressure, we will leave blank here.

Cursor to fifth argument The fifth argument is the basis.


Mole or mass has to be entered here to specify the property basis.


Since the property basis doesn’t apply to pressure, we will leave it blank here.

Type:


massflow_1 = feed1.GetProp("totalFlow" ,"Overall", None, "", "mass")

massflow_2 = feed2.GetProp("totalFlow" ,"Overall", None, "", "mass")

Type this code in the script.


This is to read the overall mass flow rates from the inlet material streams.


variables massflow_1 and massflow_2 store total mass flow rates of Methanol and Water streams.

Type:


molfrac_1 = feed1.GetProp("fraction", "Overall", None, "", "mole")molfrac_2 = feed2.GetProp("fraction", "Overall", None, "", "mole")

Type this code in the script.


This code will read the mole fractions of the components from the inlet material streams.


Here, we see the variables molefrac_1 and molefrac_2.


They store mole fractions of the components from the streams in form of 1-D array.

Type:


molflow_1 = feed1.GetProp("totalFlow" ,"Overall", None, "", "mole")

molflow_2 = feed2.GetProp("totalFlow" ,"Overall", None, "", "mole")

Type this code in the script.

Here, variables moleflow_1 and moleflow_2 store total mole flow rates of the streams.

Type:

enthalpy_1 = feed1.GetProp("enthalpy" ,"Overall", None, "Mixture", "mass")

enthalpy_2 = feed2.GetProp("enthalpy" ,"Overall", None, "Mixture", "mass")

Type this code in the script.

This is to read the specific enthalpy from the inlet material streams.

Variables enthalpy_1 and enthalpy_2 store the specific enthalpies of the streams.

Cursor to fourth argument “Mixture” Please note that the fourth argument is specified as a Mixture.

Here we are storing the specific enthalpy of the overall stream.

So, calculation type is specified as Mixture.

Type:


P_3=[0]

massflow_3 = [0]

molflow_3 = [0]

enthalpy_3= [0]

Type this code in the script.

All the required properties for the outlet stream are initialized here.

The variables are initialized as vectors.

This makes it easy to set the values in the outlet stream using the SetProp function.

Now we will code the calculation routine.
First we will calculate the overall mass flow rate of the outlet stream.
Type:

massflow_3[0] = massflow_1[0] + massflow_2[0]

Type this code in the script.

Total mass flow rate of the outlet stream is calculated and stored in the variable massflow_3.

Now we will calculate the overall mole flow rate of outlet stream.
Type:

molflow_3[0] = molflow_1[0] + molflow_2[0]

Type this code in the script.

Total mole flow rate of outlet stream is calculated and stored in the variable moleflow_3.

Next we will calculate the specific enthalpy of outlet stream.
Type:

totalenthalpy = (massflow_1[0] * enthalpy_1[0]) + (massflow_2[0] * enthalpy_2[0])

enthalpy_3[0] = totalenthalpy/massflow_3[0]

Type this code in the script.

Total enthalpy of the outlet stream is calculated and stored in the variable totalenthalpy.

Similarly, specific enthalpy of the outlet stream is calculated and stored in the variable enthalpy_3.

Next we will calculate the individual component mole flow rates in the outlet stream.
Type:


totalmolflow_comp1= (molfrac_1[0] * molflow_1[0]) + (molfrac_2[0] * molflow_2[0])

totalmolflow_comp2= (molfrac_1[1] * molflow_1[0]) + (molfrac_2[1] * molflow_2[0])

Type this code in the script.

Mole flow rates of component 1 and 2 are calculated and stored in totalmolflow_comp1 and totalmolflow_comp2.

Now we will calculate the mole fraction of components in the outlet stream.
Type:


molfrac_3 = [totalmolflow_comp1 / molflow_3[0],totalmolflow_comp2 / molflow_3[0]]

Type this code in the script.

Component mole fractions are calculated and stored in the variable molfrac_3.

Now we will calculate the outlet pressure.
Type:


P_3[0] = (P_1[0] + P_2[0]) * 0.5

Type this code in the script.

Outlet pressure is calculated and stored in the variable P_3.

Point to MixedProduct. Now we will pass the calculated output variables to the Mixed Product material stream.
Type:

out = oms1

Type this code in the script.


oms1 is the reference to the material stream connected to the Outlet Stream 1.

Type:

out.Clear()

Type this code in the script.

This is to clear any calculated values stored during previous runs.

Now we will pass the calculated output variables to the Mixed Product material stream.
Type:

out.SetProp("enthalpy", "Overall", None, "", "mass",enthalpy_3)

Type this code in the script.


This is to pass the calculated specific enthalpy to the Outlet Stream 1.

Cursor to SetProp keyword SetProp is a function used to assign values to the output stream.

It is a similar function to that of GetProp.

SetProp function has six arguments.

First five arguments are same as that of GetProp function.

Cursor to “enthalpy_3” keyword The sixth argument is the output variable.

This is to specify the vector variable that we want to assign to that particular property.

We want to assign the vector enthalpy_3 to enthalpy of the Outlet Stream 1.

Similarly we will pass the remaining calculated variables to the Outlet Stream 1.
Type:

out.SetProp("pressure", "Overall", None, "", "", P_3)

out.SetProp("fraction", "Overall", None, "", "mole", molfrac_3)

out.SetProp("totalFlow", "Overall", None, "", "mass", massflow_3)

Type this code in the script.

This is to pass the calculated pressure, component mole fractions and total mass flowrate to the Outlet Stream 1.

Type:

out.PropertyPackage.DW_CalcEquilibrium(PropertyPackages.FlashSpec.P, PropertyPackages.FlashSpec.H)

Type this code in the script.


This is to set the flash calculation method for the Outlet Stream 1.

Cursor on out.PropertyPackage.DW_CalcEquilibrium keyword out.PropertyPackage.DW_CalcEquilibrium is a function used to set the method of flash calculation used.

The default flash calculation method is done with the Temperature and Pressure (TP Flash).

Here we do not know the outlet temperature.

So, we set the flash type to Pressure and Enthalpy Flash (PH Flash).

Cursor on PropertyPackages.FlashSpec.P, PropertyPackages.FlashSpec.H keyword The variable at the end of the PropertyPackages.FlashSpec signifies which variable is used for flash calculation.

Here in the first argument, we pass the Pressure and in the next we pass the Enthalpy.

With this, the Python script is completed.
Now we will run the simulation.
Click Solve Flowsheet So, from the toolbar, click on Solve Flowsheet button.
Point to Flowsheet tab. Switch to Flowsheet tab.
Click Mixed Product When the calculations are completed, click on the Mixed Product in the flowsheet.
Point to Property Editor Window

Hover mouse at Input Data

Under Stream Conditions, check Temperature, Pressure and Molar Flow.
Let's summarize.
Slide Number 7


Summary

In this tutorial, we have learnt to
  • Create a custom unit operation using Python
  • Calculate Overall mass and mole flow rate
  • Calculate Enthalpy and outlet pressure
Slide Number 8


Assignment


Compounds:

Methanol (CH3OH)

Water (H2O)

Property Package: Raoult’s Law

Inlet stream:

Mass Flow: 1000 kg/h

Temperature: 50 degree C

Pressure: 1 bar


Separation Pressure: 0.075 bar

As an assignment,



Create a custom model for a flash column to separate the gas-liquid phase for a mixture of compounds.



Calculate the amount of liquid and vapour generated at 0.075 bar.



Compare the results with in-built Gas-Liquid Separator available.

Slide Number 9

About the Spoken Tutorial Project

Watch the video available at the following link.

https://spoken-tutorial.org/

It summarizes the Spoken Tutorial project.

Slide Number 10Spoken Tutorial Workshops The Spoken Tutorial Project Team
  • Conducts workshops and
  • Gives certificates.
  • For more details, please write to us.
Slide Number 11

Forum Slide

Do you have questions in this Spoken Tutorial?

Please visit this site

Choose the minute and second where you have the question.

Explain your question briefly.

Someone from the FOSSEE team will answer them.

Please post your times queries in this forum.
Slide Number 12

DWSIM Flowsheeting Project

The FOSSEE team coordinates conversion of existing flow sheets into DWSIM.

We give honorarium and certificates.

For more details, please visit this site.

Slide Number 13

TextBook Companion Project

The FOSSEE team coordinates coding of solved examples of popular books.

We give honorarium and certificates.

For more details, please visit this site.

Slide Number 14

Lab Migration Project

The FOSSEE team helps migrate commercial simulator labs to DWSIM.

We give honorarium and certificates.

For more details, please visit this site.

Slide Number 15

Acknowledgements

Spoken Tutorial and FOSSEE projects are funded by NMEICT, MHRD, Government of India.
Slide Number 16

Thanks

This tutorial is contributed by Kaushik Datta and Priyam Nayak.

Thanks for joining.

Contributors and Content Editors

Kaushik Datta, Nancyvarkey