Engineering With Python Rotating Header Image

Reversible, Exothermic, Gas Phase Reaction in a Catalytic Reactor – Programming Approach

Plan your solution:

  • Draw a picture, in this case, list all of your data and equations
  • Remember the fundamentals (material and energy balances, equations of state) and apply
  1. Draw your material or energy balance envelope (catalytic reactor)
  2. Remember [Accumulation = In – Out + Source/Sink]
  • Think about what you need to do and the answer you want
  • You need to solve a set of ordinary differentials equations with supporting algebraic equations
  • You’ll want to plot your results

How to start your program (we do this every time…):

  • With Ipython open, open your editor
  • Label your program (you’ll never remember it, go ahead and label it!)
  • Import the packages you’ll need for solving

Import the packages and define the arrays for the material balances (note the pattern earlier…):


I know, I know… I always import too much and ‘in a sloppy fashion’.  I’m an engineer first and a programmer… well, I won’t insult programmers, you guys do stuff that is amazing.  Let’s just say I’m an engineer and leave it at that.

Define your constants, your initial conditions and the time interval for your ODE solver:


Note the initial conditions (f_int) are x(0) = 0.0, T(0) = 450.0 and y(0) = 1.0.  We are coming up to the tricky part, defining the function for the ODE’s.  Usually, constants and initial conditions are fairly straightforward (not always, but usually…)

Define the function for the ODE solver:


Now, when you look at this you should see that I did the ‘lazy’ thing and did not define the parameters in the section above.  Why not do what I did?  Well, when you’re troubleshooting your program or have to come back to it weeks or months later, it is a lot harder to use.  Or if someone else wants to use it, and so on and so forth.  So, like your parents always said, “Don’t do as I do, do as I say…”.  Take the time and define them out, with units.  You’ll thank yourself later.   Note, that in Python, the equations look pretty much as they do on paper.  We need the conversion, temperature and pressure (via y).  This is one of the nice features of the language, among many.

Set up the odeint solver and plotting:


In the next post, we’ll show the’ final solution’.  Stay tuned, same bat channel.  I know, I’ve used that one before, but it sounds cool.

Leave a Reply