7  Reactor Modeling Functions

To complete common reaction engineering tasks, reactor models must be generated and the equations in them must be solved. Chapter 6 showed how to generate reactor models from the reactor design equations for the four ideal reactor types. This very brief chapter presents the strategy used in Reaction Engineering Basics for modeling ideal reactors.

7.1 Analytical vs. Numerical Solution of the Reactor Model Equations

In some scenarios the reactor model equations can be solved analytically. Most of these scenarios involve isothermal reactor operation with one reaction taking place and with a rate expression that is fairly simple. In a few instances the reactor can be adiabatic or there can be more than one reaction occurring.

Still, the scenarios where the reactor model equations can be solved analytically do not include many important, real-world situations that involve adiabatic operation, multiple reactions, complex rate expressions, non-isothermal operation involving heat exchange, and others. In all of these scenarios the reactor model equations can be solved numerically. Of course in the scenarios where analytical solution is possible, the reactor design equations can also be solved numerically. Therefore, in Reaction Engineering Basics the reactor design equations are always solved numerically.

Chapter 6 showed that for ideal BSTRs, SBSTRs, CSTRs, and PFRs, the reactor models will be some combination of algebraic-transcendental equations and differential equations. Fortunately, there are many software packages that provide tools for numerical solution of the sets of equations that make up reactor models. However, from the perspective of a book like this, that presents two challenges.

First, different readers of this book will want, or need, to use different software packages. If one software package is selected and used throughout, the book will be less useful to readers who do not use that software. At the same time, there are too many different software packages to try to show how to use all of them.

A second challenge that arises from the decision to always solve the reactor model equations numerically is one of focus. This book is about reaction engineering, not computer programming or numerical methods. Including extended discussions on the computational aspects of solving the reactor model equations might obfuscate the reaction engineering.

To address these challenges, in Reaction Engineering Basics, all discussion of how software equation solvers work and how they are used appears in Appendices I, J, K, and M. The presentation in those appendices is general and not specific any one software package. Fortunately, all software solvers for a given type of equation (ATE, IVODE, etc.) require essentially same inputs and return essentially the same results. It is mainly the programming language used and the details of how to access the equation solving software, how to provide the inputs, and how the results are returned that differ from one software package to the next. Appendices I, J, K, and M provide some basic understanding of how the software solves the equations, with sufficient information for readers to follow Reaction Engineering Basics example assignments and to implement solutions using the software they prefer. It is left to the reader to consult the documentation for the software they choose to learn the details for using it.

7.2 Ideal Reactor Analysis

In Reaction Engineering Basics, when an assignment involves analysis of an ideal reactor, the assignment is completed in four basic steps. These steps provide a good framework for readers to use when they perform ideal reactor analyses, as well.

  1. Summarize the information provided in the assignment.
  2. Formulate the solution mathematically.
  3. Implement the solution numerically.
  4. Report and comment upon the results.

7.2.1 Summarizing the Information Provided in the Assignment

In a book like this, assignments take the form of a few paragraphs that provide data and then assign tasks or ask questions. (In practice, a reaction engineering assignment might define tasks that the engineer must do or questions the engineer must answer, but leave it to the engineer to find the necessary data.) A good way to start the analysis of an ideal reactor involves reading through the assignment.

During the first reading of the assignment, each time a numeric value is encountered, an appropriate variable should be chosen to represent it, and it should be added to a list of given constants. Then the assignment should be read a second time to identify the type of reactor and details about how it is being operated, and this information should be written down. Finally, the assignment should be read a third time to identify all of the quantities of interest, that is, the things that need to be calculated in order to accomplish the assigned task or answer the questions posed in the assignment. In doing this, an appropriate variable should be chosen to represent each quantity of interest, and it should be added to a listing of the quantities of interest.

The result of this step is a concise summary of the assignment that provides (a) given constants, (b) the reactor being used and how it is being operated, and (c) the quantities of interest that need to be calculated. As this process becomes familiar, it may not be necessary to read the assignment three times to complete the assignment summary.

7.2.2 Formulating the Solution Mathematically

In essence, “formulating the solution mathematically” means writing all of the equations that will be needed to complete the assignment and briefly describing how to use them. The formulation is written in anticipation of implementing the solution numerically. The mathematical formulation always begins by generating the reactor design equations need to model the reactor. Chapter 6 describes how to do this.

In most cases, additional equations are needed in order to solve the reactor design equations. The nature of these ancillary equations depends, in part, upon whether the reactor design equations are ATEs, IVODEs, DAEs, or BVODEs. Details are provided in Appendices I, J, K, and M. The next step in formulating the solution is to write the ancillary equations that are needed in order to solve the reactor design equations. There are three kinds of ancillary equations: those needed to calculate quantities that appear in the design equations, those that are needed to calculate initial and final values (only if the design equations are IVODEs), and those needed to calculate quantities of interest that are not found directly by solving the design equations.

Once the design equations and all of the necessary ancillary equations have been written, a brief summary of how to use them should be presented. More specifically, during numerical solution of the design equations certain quantities must be calculated. For each such quantity, the summary should identify the necessary equations and describe how to use them to calculate the quantity.

7.2.3 Implementing the Solution Numerically

Once the solution has been formulated mathematically, the next step is to implement it numerically. This involves writing computer code that makes use of the equations presented in the mathematical formulation. There isn’t one single way to do this. Depending upon the software being used and the programmer’s preferences, the code can often be structured in a number of different ways. The next section presents the approach to structuring the code that will be used in Reaction Engineering Basics.

7.2.4 Reporting the Results and Commenting

Finally, the results should be presented and discussed. Any graphs or tables requested in the assignment should be included, and all questions asked in the assignment should be answered. In Reaction Engineering Basics, this section will additionally include pertinent comments and discussion of interesting or important aspects of the assignment and its solution.

7.3 Reactor Model Functions

As noted above, there are many ways to structure the computer code used to perform the calculations for a given reactor analysis assignment. The approach outlined here parallels the approach described above for formulating the solution. The code for implementation is broken into four component types, each of which serves a different purpose. Some of the calculataions will be performed by a mathematics software package. This section describes the code a reaaction engineer needs to write in order to use such packages.

7.3.1 Making Variables Available Where Needed

The mathematical formulation described above begins by summarizing the information provided in the assignment. Part of that summary is a list of variables being used to represent quantities that are constant. It is usually necessary to use those constants at several points in the analysis. Rather than having to enter them each time they are needed, it is convenient to define them one time, in one place, and then make them available to all of the other code.

Similarly, in some analyses the current value of a variable quantity may need to be known at several places in the code. Most commonly this is accomplished by passing the variable as an argument to a computer function that needs it. In some situations, such as when using equation solvers from a mathematics software package, it isn’t possible to add that variable as a argument to a function that needs it. The software package dictates what is passed to the function as an argument, so a different way of supplying the variable must be implemented.

Consequently, one part of the numerical implementation, often the first part, is responsible for making constants and, when necessary, the current values of variables available wherever they are needed in the numerical implementation code. The descriptions of numerical implementations in Reaction Engineering Basics will simply say something like “make the known constants and the current value of [name of variable] available for use in all equations.”

7.3.2 Solving the Reactor Design Equations

After the assignment summary, the first thing presented in a numerical formulation is the set of reactor design equations needed to model the reactor. Code that solves the reactor design equations is another component of the numerical implementation of the solution. In Reaction Engineering Basics, the code that solves the reactor design equations is referred to as the reactor model function.

Depending upon the nature of the reactor analysis, variables may or may not be passed to the reactor model function as arguments. The reactor model function will use an equation solver from a mathematics software package to solve the equations. It must first calculate any quantities that the solver needs as input. For example, when solving ATEs, an initial guess for the solution must be provided, and when solving IVODEs, initial values and a stopping criterion must be provided.

The reactor model function would then call the appropriate solver, passing the required input variables to it. In addition to those variables, the reactor model function must pass the name of a function that the solver will use (see Appendices I, J, K, and M). For example, when solving ATEs, the reactor model function must pass the name of a function that calculates the residuals to the solver (Appendix I), and when solving IVODEs, the reactor model function must pass the name of a function that evaluates the derivatives to the solver (Appendix J).

In total, the responsibilities of the reactor model function are to calculate variables that the solver requires, call the solver, check that the solver succeeded in solving the design equations, and return the results. In Reaction Engineering Basics, the description of the numerical implementation of a solution will say that a reactor model function should be written. It will then list the arguments passed to it, if any, describe the calculations it performs, name the kind of equation solver it calls, list the arguments it passes to the solver (including the function name argument), and list what it returns.

7.3.3 Calculating Quantities Used by Equation Solvers

As noted above, when the reactor model function calls a solver, one of the arguments it must provide is the name of a computer function. Those computer functions are a third component of a numerical implementation. When an ATE solver is called, the function whose name is provided to it will be referred to as a residuals function (see Appendix I). When an IVODE solver is called, the name of a derivatives function (Appendix J) is passed to it.

The arguments that a residuals function is passed and the values it returns are specified by the software package being used. The order in which the arguments are listed, the variable types of the arguments (scalar, vectors, etc.), the order in which the return variables appear, and the types of the return values are all specified by the software package being used. The same is true for derivatives functions. If the residuals function or the derivatives function needs other variables in order to perform its calculations, those variables must be provided in some other way than as an argument. The first part of a numerical solution, described above, takes care of this.

So basically, the reaction engineer must write residuals functions and derivatives functions that conform to the specifications of the software package they are using. Similar to reactor model functions, descriptions of numerical implementation in Reaction Engineering Basics will indicate that a residuals or derivatives function should be written, list the arguments passed to it, list variables available to it (as above), describe the calculations it performs, and list what it returns.

7.3.4 Analyzing the Process

The remaining necessity in the numerical implementation is to complete the analysis. In essence, the reactor model function only solves the reactor design equations, and the other components described above simply support the reactor model function. Each analysis will be different. In one, the reactor model might need to be called multiple times to generate data for a graph. In another, the quantities of interest might still need to be calculated after the reactor design equations have been solved. In descriptions of numerical implementations in Reaction Engineering Basics, these actions will be listed after all of the functions that need to be written have been described.