0% found this document useful (0 votes)
196 views7 pages

Process Simulation Using VBA Code With Variable Input Data: by Stephen M. Hall, PE

Uploaded by

Reda Arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views7 pages

Process Simulation Using VBA Code With Variable Input Data: by Stephen M. Hall, PE

Uploaded by

Reda Arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Reprinted from PHARMACEUTICAL ENGINEERING®

The Official Magazine of ISPE


September/October 2010, Vol. 30 No. 5 Process Simulation Methods
www.ISPE.org ©Copyright ISPE 2010

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.

September/October 2010 PHARMACEUTICAL ENGINEERING 1


Process Simulation Methods
Water Consumption batch use a precise quantity of water. the model for many simulated days,
Histogram – However, the number of batches per statistics are collected and analyzed.
An Example Problem day may vary from zero to many de- Then, using engineering judgment,
Problem statement: a purified water pending on production demand. And the Decision variables are adjusted to
system, consisting of generator and the quantity of water used for each test a different scenario. The process
storage tank, provides water for formu- batch is often very large compared is repeated until consensus is reached
lation and cleaning in a manufacturing with other water uses. for the tank size and water generation
plant. Size the generator and tank. • Incidental uses with manual control, rate. Along the way, different combina-
Discussion and solution: the primary such as rinsing parts in a sink or tions for the input parameters are tried
goal is to determine the optimal size for hosing out an open tank, cannot which provides data for a sensitivity
the water storage tank. Assuming the be precisely predicted. However, it analysis.
generation unit can operate at constant should be possible to understand the Now the process of developing the
volume around the clock, the problem duration of uses over a range (e.g., spreadsheet is explained. The main
reduces to an analysis of water usage. one to five minutes). lessons are to design the spreadsheet
Start the analysis by making a list for easy use, pay attention to format-
of usage points in the water system. For this example, it was decided that the ting and documentation, and to utilize
The water usage points are estimated usage durations may vary around an Visual Basic for Applications (VBA) to
in terms of the flow rate, and in some average value and that they will have a solve problems for which Excel lacks
cases, the duration of flow. For this ex- normal distribution with characteristic intrinsic functions.
ample, to characterize water demand bell-shaped curve. The engineer esti- All of the usage information was
on an hourly basis, the time of day mated the average number of usages assembled into a table in Excel as seen
that each usage point may demand per day, but in addition to a use point in Figure 2. Because consistent volume
water is generalized by specifying the that he thinks will usually be near units are used throughout the model,
operating shift(s) when the point may the estimate, other points will vary the Draw Rate units are not labeled
be active. Then, an estimate is made of uniformly over a range. In a uniform (e.g., l/min, gpm, m3/min). The mean
the number of times per day that each distribution that ranges from one use Duration of each use was estimated,
point draws water. per day to 10 uses per day, there is an along with a Standard Deviation (SD),
Notice that the system definition is equal (10%) probability that the number to define a bell-shaped normal distribu-
fuzzy. The duration of flow is known of uses on any particular day will be tion. If SD is zero, there is no distribu-
“in some cases.” And “an estimate is one, two, three, …, or 10. tion; every use has the same duration.
made” of the number of usages per day. Excel was used to model the water The range of Usage Starts and also a
If given precise values, it would be easy system. The model creates an hourly Distribution characteristic are listed.
work to size the generator and storage water consumption profile for one day, Next, a list of plant design decisions
tank. Contrarily, the usage is variable but since every day is different due to analyze was created. These are the
and can have wide ranges. to the variations discussed above, the water generation rate and tank size
Consider these typical water uses: model must be run many times to give which are the primary objective for the
a feel for how the tank level might vary model, plus the maximum total usage
• Clean-in-Place (CIP) systems and over time. The results from running rate (which affects sizing of the circula-
parts (washers) are programmed the model for four simulated days are tion pump and piping) and starting level
and use a precise quantity of water shown in Figure 1. The tank level is on in the tank. How full is the tank at the
for each recipe. However, there may the ordinate and time is on the abscissa. beginning of the day? The importance
be several different recipes, the On the fourth day, the tank runs dry. of this parameter is explored later.
recipes may be changed as cleaning Each day is characterized by the tank There is one more thing for this
verification data is collected, and the level increasing in the first hours; this example before doing any modeling.
frequency of use varies with produc- is due to our input assumptions that Assume that water circulates at ambi-
tion demand. place highest water demand on the ent temperature with heat exchangers
• Formulation tanks that are filled day shift (the charts begin at midnight at the beginning and end of the loop so
with water to make an aqueous which is the night shift). After running the tank remains hot. To maintain the

Figure 1. The simulation creates histograms showing tank level through one day.

2 PHARMACEUTICAL ENGINEERING September/October 2010


Process Simulation Methods

Figure 2. Initial input data collection.

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.

1. Calculate how many times each us-


age point is used in the day.
2. For each instance, calculate the
duration of the usage.
3. Assign each instance to a particular
hour of the day.
4. Calculate the material balance for
the tank for each hour of the day
and chart the results.

The intermediate calculations for Steps


1-3 are performed on separate work-
sheets. This way, the input parameters
and final charted results can be neatly
formatted together on the primary
worksheet.
Visual Basic for Applications (VBA)
is a great tool for solving this type of Figure 4. VBA subroutine flow diagram.

September/October 2010 PHARMACEUTICAL ENGINEERING 3


Process Simulation Methods
Engineers cite “ease of use” as one
of the strong advantages of Excel and
plant modeling software (such as Su-
perPro Designer). When the software’s
toolbox contains the function needed
and makes it accessible and intuitive,
then people are impressed with the
ease in using the software. However,
that accessible tool may not be the
best one for the job. Use engineering
judgment to assess results. It is up to
the user to ensure the work is easy for
others to understand by using good
development practices, including clear
documentation. In the author’s experi-
ence, at least 80% of the time spent
Listing A. Defining the variables used in the VBA subroutines. developing a spreadsheet is devoted to
formatting, testing, and documentation.
It’s interesting that this axiom was true
when mainframes were programmed
in FORTRAN using punch cards and
it still applies today with Excel and
VBA.
The VBA module contains five
subroutines and one function. It could
easily be combined into a single subrou-
tine, but it’s easier to develop, debug,
and maintain when divided into pieces.
Figure 4 shows the relationship of the
routines in the module.
Most of the variables that are used in
the code are dimensioned at the begin-
ning of the module. This makes them
available to all of the subroutines - see
Listing A.
The Initialize subroutine takes care
of all of the housekeeping needed to
facilitate the calculations. It clears vari-
ables and declares initial values. It also
erases old data from the worksheets,
where the calculation subroutines will
write new intermediate results.
The first two steps of the algorithm,
calculating the duration for each in-
stance of a use point utilization for a
day, are performed by the UsesPerDay
subroutine - Listing B. The array called
“UsePointParameters” holds data for
each of the use points as defined in the
remarks - Listing A.
Step 1 is to decide how many starts
to assign to each usage point for this
random day. From our input param-
eters, it is seen that the minimum and
maximum number of usages are known
and there is a distribution model (i.e.,
Listing B. Subroutine to perform Steps 1 and 2.
uniform or normal distribution). In

4 PHARMACEUTICAL ENGINEERING September/October 2010


Process Simulation Methods
Lines 30 to 40, variables Lower and range is reached, it wraps around to the worksheet using Excel formulas - Figure
Upper are assigned to the minimum beginning. If there are more usages than 7. Again, there is a button that executes
and maximum number of usages. Then, hours, the subroutine assigns multiple the model for one day. The tank level is
in Line 50, the kind of distribution to uses per hour. graphed over the simulated day.
apply is selected. At Line 500, water consumption is That completes the model for simu-
Function Gauss() returns a normally calculated using the Duration and Rate lating one random day. The final sub-
distributed random number - Listing C. of an instance. The results are compiled routine, RunXDays, runs the model for
Calculate the value by multiplying the on another intermediate results work- as many days as desired. In addition, it
standard distribution by the random sheet, called “UseAmounts” - Figure 6. selects between two cases: 1) start each
number (gauss * SD) then adding to Again, a button is provided that when day with the tank level set to a specific
the mean [(Lower+Upper)/2]. Round clicked, executes the model for a single value or 2) start each day with the
the result to the nearest integer since day. This facilitates reality checking the tank level equal to the last value from
there cannot be a partial usage. Then, results. the previous day. This is interesting
since about 5% of the values lie outside The tank level material balance is because the tank level change during
two standard deviations from the mean, compiled on the “Inputs and Results” any one day is limited. If it is always
move those outliers to the stated mini-
mum or maximum values.
Now that there is a count for the
number of times each usage point is
used in the day, turn to Step 2 which
determines the duration for each of
those usages. Line 160 begins a loop
through each of the usage points. Within
that loop, Line 180 cycles through the
uses for a usage point as determined
in Step 1. For each usage, calculate a Listing C. Function returns a normally distributed random number.
duration using the rules established by
the input parameters. It is convenient
to collect the total water consumption
for the day, which is done in Line 210.
When the UsesPerDay subroutine
executes, it saves the results on the
worksheet called “UseDurations” - Fig-
ure 5. Notice the button called “Run 1
Day.” Clicking it enters the VBA Module
at the WaterCalcs subroutine and ex-
ecutes the model once. It is placed here
to make it convenient to step through
the model and assess whether the in-
termediate results make sense. This is
part of the testing process. Having this
kind of visibility into the calculations is
extremely useful; in this case, it helps
to reality test the input parameters for
usage starts and variability.
These intermediate results are the
input values to Step 3, which deter-
mines the time of day each usage occurs
- Listing D.
As before, the subroutine cycles
through each of the use points. It takes
the number of uses determined above
and assigns them to a time of day. To
keep this relatively simple, it starts
at a random hour within the allowed
range (i.e., day, evening, anytime), then
just marches forward. If the end of the Listing D. Subroutine performs Step 3.

September/October 2010 PHARMACEUTICAL ENGINEERING 5


Process Simulation Methods
assumed that the level starts the day lated 30 days of running, also charted in size and generation rate without user
at 50% full, then the model indicates Figure 1. See also the inputs to control intervention, but this would take the
a tank size that accommodates that the simulation – tank level case selec- user one more step away from the deci-
limited level change. However, running tion and number of days. Clicking the sion process with consequent insulation
the model with the selected tank size, button initiates the run. This simula- of the user’s engineering judgment.
but with the level already at say, 20% tion requires the user to propose a The water tank example shows how
full may result in the simulated tank tank size and water generation rate. to create a model that incorporates
running dry. By stringing days together The results of those proposals are seen variable input data. Whenever input
(or alternatively, just starting with a when the simulation is run, and it is an data varies, consider whether it is im-
random tank level at the beginning of iterative process to adjust the decision portant to model it this way by doing
each day) the effect of the usage vari- inputs until the user is happy with the a sensitivity analysis. Build the model
ability is fully visualized. results. It is possible to write additional first assuming a fixed set of inputs.
Figure 8 shows the results for a simu- program steps to “optimize” the tank Then observe the effect on the results
by varying those inputs individually
or in groups. Consider input modeling
if a there is a significant change in the
results. Playing the “what if…” game
often gives new insight into a system’s
behavior and may lead to more robust
design decisions.
This simulation, using variable
inputs with statistical results, is an
example of a “stochastic” simulation,
also called a “Monte Carlo” simulation.
This type of simulation is very useful
for modeling pharmaceutical processes
and gives better understanding of the
process than a model that uses fixed
inputs (called “deterministic”).
It took about eight hours to fully
develop, debug, and test the example
spreadsheet. This included time to
formulate the problem, design the
algorithm, create the worksheets, and
Figure 5. Intermediate results worksheet for Steps 1 and 2. write the VBA subroutines. Tackling the
problem in steps and using a modular
approach eased the creative process.

Further Reading
1. Albright, S.C. VBA for Modelers:
Developing Decision Support Sys-
tems Using Microsoft® Excel. 2nd ed.
South-Western College Pub; 2006.

2. Anthony’s VBA Page provides help


for VBA programmers. http://www.
anthony-vba.kefra.com/

3. Powell S.B. and Baker K.R. Manage-


ment Science: The Art of Modeling
with Spreadsheets, Excel 2007 Up-
date. 2nd ed. Wiley; 2008.

4. Ragsdale C. Spreadsheet Modeling


and Decision Analysis: A Practical
Introduction to Management Sci-
Figure 6. Intermediate results worksheet for Step 3.

6 PHARMACEUTICAL ENGINEERING September/October 2010


Process Simulation Methods
ence. 5th ed. South-Western College http://www.oracle.com/appserver/ Monte Carlo Simulation and Risk
Pub; 2007. business-intelligence/crystalball/. Analysis in Spreadsheets,” free
download from http://home.uchi-
5. Savage, S.L. and Danziger, J. The 3. Ersatz, “A Bootstrap Add-in for cago.edu/~rmyerson/addins.htm.
Flaw of Averages: Why We Underesti- Microsoft Excel for Windows. It al-
mate Risk in the Face of Uncertainty. lows you to do uncertainty analysis 6. Simulación, “Fully Developed in
Wiley; 2009. (aka ‘risk analysis,’ ‘Monte Carlo VBA, Created to Bring a Flexible
simulation,’ ‘probabilistic sensitiv- and Easy to Use Simulation Tool
5. Winter Simulation Conference, www. ity analysis,’ ‘bootstrapping’) and to Excel,” English and Spanish ver-
wintersim.org. microsimulation in Excel,” http:// sions, free download from http://
www.epigear.com/index_files/Er- www.ucema.edu.ar/~jvarela/.
Excel Add-In Tools satz.htm.
for Simulation and Analysis 7. XLSim 3, “A Monte Carlo Simulation
1. @Risk, “Complete risk and decision 4. ModelRisk,“Best and Most Advanced Package,” http://probilitech.com/.
analysis toolkit,” http://probilitech. Spreadsheet Sim Currently on the
com/. Market,” http://www.vosesoftware. About the Author
com. Stephen M. Hall,
2. Crystal Ball, “Application Suite for PE is a chemical en-
Predictive Modeling, Forecasting, 5. SimTools, “Adds Statistical Func- gineer (University of
Simulation, and Optimization,” tions and Procedures for doing Pennsylvania, 1974)
with a Masters in
Business Administra-
tion (Drexel Univer-
sity). He started in the
Process Development Department at
Occidental Chemicals before joining
Corporate Engineering at SmithKline
Beckman (now GSK) in the 1980s. Hall
worked in the pharmaceutical engi-
neering consulting field for nearly 30
years as project manager and process
engineer. He’s been a speaker at ISPE
regional and national meetings and has
contributed papers to Pharmaceutical
Engineering, Chemical Engineering,
and Chemical Engineering Progress.
His spreadsheet templates are widely
used (www.chemengsoftware.com). Hall
is now Senior Director of Project Man-
agement at MannKind Corporation in
Figure 7. Results for one simulated day.
Danbury, Connecticut. He can be con-
tacted by telephone: +1-203-790-3085 or
by email: [email protected].
MannKind Corporation, 1 Casper
St., Danbury, Connecticut 06810-3930,
USA.

Figure 8. Results for many simulated days.

September/October 2010 PHARMACEUTICAL ENGINEERING 7

You might also like