Process Simulation Using VBA Code With Variable Input Data: by Stephen M. Hall, PE
Process Simulation Using VBA Code With Variable Input Data: by Stephen M. Hall, PE
This article
explains how to Process Simulation Using VBA Code
use Visual Basic
for Applications with Variable Input Data
(VBA) code
with variable
input data to by Stephen M. Hall, PE
create a process
simulation.
T
he adage, “garbage in, garbage out,” de- 4. Build the simulation. Start simple and then
scribes a common problem with process build to the necessary complexity.
models. Whether elegant or simple, plant 5. Verify the simulation model. Ensure the
simulations rely on the assumptions computer code generates results that are
that are provided. They require a set of inputs consistent with the problem objectives and
before they can give outputs. While the modeling validate the model against the real world.
activity itself may be interesting and challeng- 6. Run design cases. Decide which sets of input
ing, the computer reports flawed results if the data to use, what output to collect, and how
underlying data is poorly characterized. to report the results; document both the
This article shows how to use Microsoft computer code and the input/results.
Excel and Visual Basic for Applications (VBA)
to create a process simulation. An example To decide the appropriate size for a purified
problem is defined and solved with step-by-step water storage and distribution vessel, the water
instructions. Follow the thought process as a generation and usage rates must be known.
simple simulation is built. The example should To optimize the tank size, the time-of-day us-
be familiar: sizing a purified water storage tank. age profile and knowledge about sanitization
However, the tank is a prop, used to illustrate practices are important. The model uses these
the model building process. The input data and input data and constructs a graph that depicts
assumptions become crucially important. The the water level in the tank over the course of a
variability of the input data is especially critical. day. The graph changes whenever an assump-
Due to many factors, such as production schedul- tion is changed.
ing and random events, the input data change When inputs are treated as fixed values, the
each time the model is run. The model’s output resultant model is deterministic. Results are
changes in response to the variable inputs. calculated from the input data. The calculations
The ability to incorporate randomness can be checked for their sensitivity to changes
defines the difference between a model and a to the assumptions; this improves process un-
simulation. Successful simulations are often derstanding and might signal areas that could
built with the following steps: benefit from further optimization.
The breakthrough in process understanding
1. Formulate the problem. Set objectives, plan comes when it’s recognized that certain inputs
the effort, and conceptualize a simulation to the model vary in accordance with probability
model. distributions. For the water tank example, the
2. Collect data. Determine the key assumptions user points are unlikely to draw water on the
and inputs; collect real-world data if possible, exact same schedule day after day. The number of
qualify the data. times per day that water is used may vary as well
3. Analyze the data and model the inputs to as the time of day and duration of a particular
the simulation. Determine how the input usage. Statistical variation of input variables,
data will interact with the simulation and when incorporated into a process model, result
perform sensitivity analyses. For data that in a stochastic simulation. Results from such
have a relatively big impact on the simula- models are typically presented as frequency
tion, model the inputs using an appropriate histograms or opinionated consensus.
statistical correlation.
Figure 1. The simulation creates histograms showing tank level through one day.
loop, the heat exchangers are turned off problem. VBA sits beneath the Excel simulations, optimizations, and other
for a period of time every night and the spreadsheet. It is ideally suited for statistical analyses. Several of these
loop gets hot. The model should address iterative and repetitive problems that are listed at the end of the article. The
whether the duration of the sanitization are difficult or confusing to perform advantage to using an Add-In is that
cycle affects the tank sizing. In other within the Excel environment. The the work is done completely within
words, provide the model with a way entire spreadsheet may be downloaded Excel with Add-In environment. Disad-
to block out a time when no water is for free from http://www.pipesizingsoft- vantages include a new learning curve,
available to the use points. Figure 3 ware.com/PW/CaseStudy.xls. inability to share models with those
shows how all of the input data looks An alternative to VBA is to use an who lack the Add-In, and limitations
in Excel. Excel Add-In program that contains will still exist that may require VBA
The goal is to decide on a tank size tools for performing Monte Carlo anyway.
and water generation rate. If the input
parameters were fixed, the calcula-
tions are easy: add up the daily uses
and divide by 24 to get the generation
rate per hour; chart the hourly usage
variation and pick a tank size that will
operate between 20% and 80% full. But
the parameters are assumed to be vari-
able. Therefore, to solve the riddle, the
steps listed below are followed, applied
to a random day. Then the steps are
repeated many times to simulate the
plant operation over a period of time.
As the simulation proceeds, statistics
are collected, and finally, engineering Figure 3. Input data grouped together for ease of use.
judgment is applied to decide on a tank
size and generation rate.
Further Reading
1. Albright, S.C. VBA for Modelers:
Developing Decision Support Sys-
tems Using Microsoft® Excel. 2nd ed.
South-Western College Pub; 2006.