## USING PYTHON TO SOLVE CHEMICAL ENGINEERING PROBLEMS

This pdf illustrates how to use the programming language Python to solve the problems posed in the book Introduction to Chemical Engineering Computing, Bruce A. Finlayson, Wiley (2006-2014). The material mirrors the use of MATLAB in the book, and solves the examples in Chapters 2, 3, 4, and 8. In addition, a new Appendix F gives summaries of many Python commands and examples. Together with the book, chemical engineering applications are illustrated using Microsoft Excel®, MATLAB®, Aspen Plus®, Comsol Multiphysics®, and Python. This pdf is intended to be used in conjunction with the book, and the treatment using Python mirrors that using MATLAB. Appendix F includes information for setting up Python on your computer. To get the pdf, go to http://www.chemecomp.com.

## Error Bounds using the Green’s Function

November 18, 2014

One of the fundamental concepts when solving partial differential equations is the Green’s function. This function allows you to represent the exact solution as an integral. It may be hard to find, and if found, may be hard to evaluate (needing many terms to converge). But, there are problems for which one is known. Problem 10 from my website (www.ChemEComp.com) shows how to use the Green’s function to provide error bounds for a numerical solution. In this case the problem is simple: flow in a square or rectangular duct. An example of the Green’s function is shown in the figure as a function of x and y, for a particular value of zeta and eta, (0.7, 0.3). The figure becomes sharper as more and more terms are kept in the infinite series, but the world does march on! The problem statement gives some mathematical background for deriving the error bounds, and the solutions (obtained through SIAM) gives the answers.

## September 2, 2014 – Numerical Results with Rigorous Error Bounds

Have you ever solved a problem numerically and wondered: How accurate is my answer? Oftentimes the only clue you have is how the solution changes (hopefully not much) as you refine the mesh and solve it with more degrees of freedom. I have posted some new problems and a few solutions that help with this problem.

The method is to use mathematical theorems that bound the error. Sometimes the error can only be bounded in an average sense – called the mean square error, which is the error squared, integrated over the domain, and then take the square root. The best situation, though, is when you can bound the difference between the exact solution and the approximate solution you’ve found at every point in the domain. That is called the point-wise error.

The book Method of Weighted Residuals and Variational Principles discusses these points in Chapter 11. While the book was first published in 1972, it has been republished by the Society for Industrial and Applied Mathematics, http://www.ChemEComp.com/MWR. Of course the mathematical proofs are still valid. But, back in 1972, you couldn’t solve many problems very accurately. However, now you can. Thus, the problems I’ve posted on the website use the theorems given or mentioned in the book and solve the problems using modern technology, and then compute the mean-square error bound and sometimes the point-wise error bound for the approximate solution. The solutions are sometimes found using MATLAB and sometimes found with Comsol Multiphysics.

Problems 7, 8, and 9 have just been posted. These refer to a problem called ‘effectiveness factor’ in chemical engineering. They compute the average reaction rate in a porous catalyst that is undergoing both diffusion and reaction. This is a classic problem in chemical reaction engineering. Problem 7 sets the stage – gives the theorems and equations that aren’t already in the book and solves the problem for a plane geometry using Hermite cubic polynomials in Comsol Multiphysics. Other parts use Lagrange quadratic polynomials and solve for both plane geometry and a spherical catalyst pellet. Problem 9 is for a non-linear problem. (You thought no error bounds would be available didn’t you!)

Posted in Chemical engineering computing | 1 Comment

## Source Code for Examples is now Available

July 18, 2014

The source code for all the examples in the book has recently been made available by Wiley. They total 26 MB and can be downloaded by going to booksupport.wiley.com and searching on author: Finlayson (or, title or ISBN number). These would allow you to work all the examples in the book just by loading the source code into the requisite Excel, Matlab, Aspen, or Comsol. The problems at the end of the chapter you still must do yourself!

When I first wrote the book I thought that it was important for students to start from nothing and learn each program. However, I’ve realized that even if you do that, you learn only the things relevant to that example, and still have to learn some more. Now, by starting with an example, whose solution is in front of you, you can learn that problem faster, and, hopefully, continue that learning process when you tackle new problems.

The book web site, http://www.ChemEComp.com has links to the same file stored by Wiley. It has other downloads, as well, that might be useful. Don’t ignore the material on The Method of Weighted Residuals and Variational Principles, which is there, too. It deals with error bounds, and you may be willing to just accept your answer, but the bounds do give you confirmation and useful insight into the method of solution.

April 4, 2014

I’ve added a problem to the website http://www.ChemEComp.com for electrochemical polymerization in a series of stirred tanks. Solutions are given as derived in Excel and MATLAB. It is a simple problem with important insight and comes from the book Electrochemical Reaction Engineering, by K. Scott, Academic Press, New York (1991). The plot of conversion versus number of stirred tanks is shown here from the Excel solution. I have also collected the figures from my book, Introduction to Chemical Engineering Computing. They are available to instructors who use the book as a text in class. Shortly I will have the programs that were used for illustration in the book so that instructors can run them without much hassle. You get these by contacting Wiley. The following web sites can be used to obtain material from Wiley. It includes the keys to the problems (useful to your Teaching Assistant), and the figures in the book (useful to you when you go to class). Go here to learn what is in the book:

http://www.wiley.com/WileyCDA/WileyTitle/productCd-1118888316.html

and here to fill out the form

http://www.wiley.com/WileyCDA/Section/id-821455.html

The updated version of the 2nd edition should be available in the next few days. The parts on Aspen Plus have been updated to be consistent with Aspen Plus 8, which comes in Aspen Suite 8.4. The windows are the same, but how you get to them is different. The figures available from Wiley have the updated windows. This is the only textbook available now that has been updated for Aspen Plus 8.

## More problems/solutions with error bounds and help integrating in time in Comsol Multiphysics March 3, 2014

I’ve added some more problems to the website www.ChemEComp.com/MWR that goes along with the book pictured above.  Now there are six problems, for eigenvalue problems, boundary value problems in 1D, elliptic boundary value problems, and initial value partial differential equations. All of these first six problems have solutions with them, that can be downloaded from the website. They illustrate how variational principles are used to calculate error bounds, or sometimes just trends (always going up or always going down as the approximation improves).  See additional description in the Jan. 15th blog below.

I’ve corrected the page proofs for the revision of Introduction to Chemical Engineering Computing. It now is based on Aspen Plus User Interface 8.0, which comes with the Aspen Suite 8.4, the latest one. See the Jan. 15th blog below for further information. The new printing should be in April. If you order it, be sure to specify that the ISBN number is

ISBN 978-1-118-88831-5

After Aspen Tech made the changes in the Aspen Plus User Interface, there are a lot of books being revised!

Hints when the time integration is having difficulty getting started in Comsol Multiphysics. (I supplied these on LinkedIn in response to a query.)

1. If it is natural convection in an enclosed space, be sure you set the pressure at one point on the boundary. Otherwise the pressure will not be defined and the simulation won’t start.

2. Next, if some of your equations are not time dependent, it is possible for Comsol not to take those variables into account when it makes the error estimates in the time step. That is appropriate because those variables would adjust instantly to the others, and might change a lot in one time step. Otherwise you will have to take very small time steps. These problems are called quasi-static. To do this, unclick them in the “Equations to be solved for”.

3. You can set the initial time step to a smaller value. Then, in desperation, you can set the Absolute and/or Relative Tolerance to higher values.

4. Also, it might help, depending on your problem, if you could supply an initial guess of the solution – it doesn’t have to be right, or even close, but something non-zero sometimes helps. Be sure to check the box for Values of Dependent Values Solved for. Good luck – this is a common problem and not easy to solve (in my experience).

5. You can also set the error criterion for the error in a time step (or the changes from one time to another) to a larger value. This is called Scaling; the default is 0.01. For kicks, try something very much larger (like 1 or 10).

6. There are a variety of other options that have not always worked well for me, but they are in the Time-Dependent Solver: increase MUMPS (this has worked for me, under Direct – when you start, it may be changed by the program and listed on the Log or Progress, and if you see it increasing it is easier to just use the larger number when you start),. Also, you can change the way the Jacobian is solved (has never worked for me). Scaling of the variables differently permits you to essentially apply different error criteria to different variables, based on your expectation of which variables are the most important to have correct. This program has lots of options, and it is confusing even to one who has programmed many of them back in the days of FORTRAN. Another one that sometimes works is to clear the solution, clear the Study and recreate the Study. I don’t have to do that often (1/10,000) but it has worked for me (and was suggested by Comsol).

7. Finally, of course, is to send it to Comsol for help. I probably should have recommended that first!