This example is interactive. Click the play button on any cell to execute it, or run all cells in sequence.

Chemical Reactor

Simulation of a continuous stirred tank reactor (CSTR) with consecutive exothermic reactions.

You can also find this example as a single file in the GitHub repository.

Chemical Reaction System

The reactor contains two consecutive reactions:

MATHDISPLAY0ENDMATH

Both reactions are:

  • Exothermic (release heat)
  • Temperature-dependent (Arrhenius kinetics)
  • Highly nonlinear

Mathematical Model

The system is described by three ODEs:

Concentration of A:

MATHDISPLAY1ENDMATH

Concentration of B:

MATHDISPLAY2ENDMATH

Reactor Temperature:

MATHDISPLAY3ENDMATH

Why is this System Stiff?

The system is stiff because:

  1. Exponential temperature dependence creates vastly different timescales
  2. Fast reactions (high MATHINLINE4ENDMATH values) vs. slow heat transfer
  3. Tight coupling between concentration and temperature

Standard explicit solvers would require extremely small timesteps. We use GEAR52A, an implicit solver designed for stiff systems.

This stiff ODE system uses the ODE block with the GEAR52A solver, designed specifically for stiff differential equations.

Python
Loading...

System Parameters

We define the physical and chemical parameters for the reactor.

Python
Loading...

Input Signals

The feed concentration and temperature vary with time to simulate disturbances.

Python
Loading...

ODE Function

We define the right-hand side of the ODEs, implementing the reactor dynamics with Arrhenius kinetics.

Python
Loading...

System Setup

We create the ODE block and scope for recording the state variables.

Python
Loading...

Simulation with Stiff Solver

We use GEAR52A, an adaptive order adaptive timestep implicit GEAR method optimized for stiff ODEs.

Python
Loading...
12:43:27 - INFO - LOGGING (log: True)
12:43:27 - INFO - BLOCKS (total: 4, dynamic: 1, static: 3, eventful: 0)
12:43:27 - INFO - GRAPH (nodes: 4, edges: 5, alg. depth: 1, loop depth: 0, runtime: 0.045ms)
12:43:27 - INFO - STARTING -> TRANSIENT (Duration: 20.00s)
12:43:27 - INFO - --------------------   1% | 0.0s<2.3s | 137.4 it/s
12:43:27 - INFO - ####----------------  20% | 0.1s<0.1s | 685.3 it/s
12:43:27 - INFO - ########------------  41% | 0.1s<0.0s | 599.2 it/s
12:43:27 - INFO - ############--------  61% | 0.1s<0.0s | 618.2 it/s
12:43:27 - INFO - ################----  81% | 0.1s<0.0s | 585.4 it/s
12:43:27 - INFO - #################### 100% | 0.1s<--:-- | 641.3 it/s
12:43:27 - INFO - FINISHED -> TRANSIENT (total steps: 59, successful: 52, runtime: 134.39 ms)

Results: Reactor Dynamics

The plots show:

  • Ca (blue): Concentration of reactant A
  • Cb (orange): Concentration of intermediate B
  • T (green): Reactor temperature

Notice the complex dynamics with multiple timescales and the strong coupling between concentrations and temperature.

Python
Loading...
Output

Analysis: Phase Portrait

Let's examine the relationship between concentration and temperature in phase space.

Python
Loading...
Output