Difference between revisions of "Dynamic Simulation Tutorial with DWSIM and Python, Part 2: Building the Dynamic Model"

From DWSIM - Chemical Process Simulator
Jump to navigation Jump to search
Line 9: Line 9:
 
</td></tr>
 
</td></tr>
 
</table>
 
</table>
 +
<br/>
 
<h1><span class="mw-headline" id="Introduction">Introduction</span></h1>
 
<h1><span class="mw-headline" id="Introduction">Introduction</span></h1>
 
<p>In order to make our model dynamic, we need to upgrade the valve model so it can set the cold water flow rate based on a given opening. Also, we need to add some 'delay' to heat exchanger model so the hot water doesn't instantaneously cools down upon a flow rate change. This is what happens in the real world, since both fluids have a residence time inside the exchanger. Any change in temperature and/or flow rate at the inlets take some time to "appear" at the outlets.  
 
<p>In order to make our model dynamic, we need to upgrade the valve model so it can set the cold water flow rate based on a given opening. Also, we need to add some 'delay' to heat exchanger model so the hot water doesn't instantaneously cools down upon a flow rate change. This is what happens in the real world, since both fluids have a residence time inside the exchanger. Any change in temperature and/or flow rate at the inlets take some time to "appear" at the outlets.  
Line 15: Line 16:
 
<p>Go to the Script Manager and add a new script. Name it "InitVars". Add the following lines to the top of it:
 
<p>Go to the Script Manager and add a new script. Name it "InitVars". Add the following lines to the top of it:
 
</p>
 
</p>
 +
 +
<source>
 +
import System
 +
from System import *
 +
</source>
  
 
<p>We need to add a few new variables to our flowsheet so we can keep track of the current time step during the dynamic simulation, record the time where a perturbation was added to the system, how much we will perturb the hot water flow rate and at which time we will do it:
 
<p>We need to add a few new variables to our flowsheet so we can keep track of the current time step during the dynamic simulation, record the time where a perturbation was added to the system, how much we will perturb the hot water flow rate and at which time we will do it:

Revision as of 19:05, 1 July 2019

Dialog-warning.png This tutorial requires advanced or above average Python programming skills.
Dialog-information.png You'll need at least DWSIM v5.1 or newer on Windows, Linux or macOS to follow/reproduce the tasks within this tutorial.


Introduction

In order to make our model dynamic, we need to upgrade the valve model so it can set the cold water flow rate based on a given opening. Also, we need to add some 'delay' to heat exchanger model so the hot water doesn't instantaneously cools down upon a flow rate change. This is what happens in the real world, since both fluids have a residence time inside the exchanger. Any change in temperature and/or flow rate at the inlets take some time to "appear" at the outlets.

Creating the Dynamic Model

Go to the Script Manager and add a new script. Name it "InitVars". Add the following lines to the top of it:

import System
from System import *

We need to add a few new variables to our flowsheet so we can keep track of the current time step during the dynamic simulation, record the time where a perturbation was added to the system, how much we will perturb the hot water flow rate and at which time we will do it:

We will run our model from 0 to 400 s, at 1s-time steps. The hot water flow rate will be perturbed at 50 s, and its value will be set to 13 kg/s and remain at this value until the end of the simulation.

Valve

Our dynamic valve model will convert an opening value (from 0 to 100) to the actual flow rate through it. At the end of the calculation, we will set the dummy_in mass flow rate to the calculated value.

To convert the opening to flow rate, we will need a few new variables (add the following to the 'InitVars' script):

For the actual dynamic valve model, create a new script and name it 'UpdateValve'. Associate this script to the 'Object Calculation Finished' event of the 'FV-01' object. This is the complete model:

The above model will convert a given opening to the actual flow coefficient by using the equal percentage conversion equation.

Heat Exchanger

Our dynamic heat exchanger model will take into account the fluid residence time on both sides (cold and hot) and the calculated steady-state temperature value, applying a quadratic delay to the steady-state calculated value which will only be achieved after the fluid has exited the exchanger.

For our dynamic model to work, we need to keep track of the current time and the last 'perturbation' time. We need to add the following variables to the 'InitVars' script:

For the actual dynamic heat exchanger model, create a new script and name it 'UpdateExchanger'. Associate this script to the 'Object Calculation Finished' event of the 'HX-01' object. This is the complete model:

Tanks

For the source and sink tanks, we will use simple dynamic models to keep track of the current water level based on the inlet and outlet flows.

Add to 'InitVars':

Helper Variables

Add the following to 'InitVars':

Helper Functions

Create a new script and name it 'Functions'. Add the following content to it:

Running the Dynamic Model

Create a new script and name it 'RunDynamicProcess_OpenLoop'. Add the following content to it:

With the above commands, we get what we typed in the 'Functions' and 'InitVars' scripts using a LINQ/lambda command (Where(lambda x:...)) and append it to our current script, so they can be executed sequentially. This will initialize our variables and add our helpers functions to the memory. We also set our time variable, which will range from 0 to 400 at unit steps.

Run the Script

Our actual full dynamic process model is:

Add the above code to 'RunDynamicProcess_OpenLoop' and click on "Update and Run (Async)".

This will run our simulation at each time step, keeping track of the calculated values and storing it on python lists. The Valve and Heat Exchanger dynamic models will be ran automatically by the flowsheet solver, since you've linked them and told the solver to run them after the corresponding objects are calculated.

Viewing Results in Excel

To view the results in a Excel spreadsheet, add a new script and name it 'SaveToExcel'. Add the following content:

Add the following to 'RunDynamicProcess_OpenLoop' and run it again:

You'll get this:

Dynamic excel.jpg

Viewing Results in Charts

To view the results in built-in DWSIM charts, add a new script and name it 'GenerateCharts'. Add the following content:

Add the following to 'RunDynamicProcess_OpenLoop' and run it again:

You'll get this:

Dynamic charts.jpg

Download File

Download the simulation file with what has been done so far: dynamic_part2.dwxmz

Return to Dynamic_Simulation_Tutorial_with_DWSIM_and_Python,_Part_1:_Concepts_and_Steady-State_Model

Proceed to Dynamic_Simulation_Tutorial_with_DWSIM_and_Python,_Part_3:_Adding_a_PID_Controller