Difference between revisions of "Software tutorial/Integration of ODEs"
Line 231: | Line 231: | ||
We will now expand our example to a system of two coupled ODEs. Still in the same reactor we are now considering the rate constant to be a function of temperature: \(k = 0.24 e^{-\frac{E_a}{T}}\), with \(E_a = 9000\) J/(mol) and R = \(8.314 \) J/mol.K, and \(T(t)\) is the time-varying tank temperature, measured in Kelvin. Furthermore, the reaction is known to release heat, with \(\Delta H_r = 400\) J/mol. The time-dependent mass and energy balance for this system is now: | We will now expand our example to a system of two coupled ODEs. Still in the same reactor we are now considering the rate constant to be a function of temperature: \(k = 0.24 e^{-\frac{E_a}{T}}\), with \(E_a = 9000\) J/(mol) and R = \(8.314 \) J/mol.K, and \(T(t)\) is the time-varying tank temperature, measured in Kelvin. Furthermore, the reaction is known to release heat, with \(\Delta H_r = 400\) J/mol. The time-dependent mass and energy balance for this system is now: | ||
\[ \begin{align*}\frac{dC_{\sf A}(t)}{dt} &= \frac{F(t)}{V} \bigg( C_{{\sf A},\text{in}} - C_{\sf A} \bigg) - 0.24 e^{-\frac{E_a}{T}} C_{\sf A}^2 \\ \frac{dT(t)}{dt} &= \frac{F(t)C_p(T)}{V}\bigg(C_{{\sf A},\text{in}}T_\text{in} - C_{\sf A}(T) \bigg) - 0.24 e^{-\frac{E_a}{T}} C_{\sf A}^2 (\Delta H_r)\end{align*}\] | \[ \begin{align*}\frac{dC_{\sf A}(t)}{dt} &= \frac{F(t)}{V} \bigg( C_{{\sf A},\text{in}} - C_{\sf A} \bigg) - 0.24 e^{-\frac{E_a}{T}} C_{\sf A}^2 \\ \frac{dT(t)}{dt} &= \frac{F(t)C_p(T)}{V}\bigg(C_{{\sf A},\text{in}}T_\text{in} - C_{\sf A}(t)T(t) \bigg) - 0.24 e^{-\frac{E_a}{T}} C_{\sf A}^2 (\Delta H_r)\end{align*}\] | ||
where \(C_{{\sf A},\text{in}} = 5.5\) mol/L, we will initially assume constant volume of \(V = 100\) L and constant inlet flow of \(F(t) = 20.1 \) L/min. Also, \(C_p(T) = 128 + 0.02(T-273)\) J/(mol.K), the molar heat capacity of the liquid phase system, a weak function of the system temperature. We need an additional initial condition for the temperature differential equation: | where \(C_{{\sf A},\text{in}} = 5.5\) mol/L, \(T_\text{in} = 288\) K; we will initially assume constant volume of \(V = 100\) L and constant inlet flow of \(F(t) = 20.1 \) L/min. Also, \(C_p(T) = 128 + 0.02(T-273)\) J/(mol.K), the molar heat capacity of the liquid phase system, a weak function of the system temperature. We need an additional initial condition for the temperature differential equation: | ||
* \( C_{\sf A}(t=0) = 3.2\) mol/L | * \( C_{\sf A}(t=0) = 3.2\) mol/L | ||
* \(T(t=0) = 295 K \) | * \(T(t=0) = 295 K \) | ||
In the code below we will integrate the ODE from \(t_\text{start} = 0.0\) minutes up to \(t_\text{final} = 7.0\) minutes and plot the time-varying trajectory of concentration in the tank. | In the code below we will integrate the ODE from \(t_\text{start} = 0.0\) minutes up to \(t_\text{final} = 7.0\) minutes and plot the time-varying trajectory of concentration in the tank. |
Revision as of 17:24, 18 November 2010
Background
In our course we have learned several ways of integrating a single equation \(\displaystyle \frac{dy(t)}{dt} = f(t, y) \) with a given initial condition \(y(t=0)=y_0\); and we have shown that a system of \(n\) first order ODE's can be integrated: \[ \displaystyle \frac{d{\bf y}(t)}{dt} = {\bf f}(t, {\bf y}) \]given a set of \(n\) initial conditions \({\bf y}(t=0) = {\bf y}_0\), i.e. \( {\bf y}_0\) is an \(n \times 1\) vector. The course notes covered Euler's method, Heun's method and Runge-Kutta methods.
However, we only coded Euler's method (because it was simple!), but not the others. These other methods have been (reliably) coded in software packages and sophisticated error correction tools built into their algorithms. You should always use these toolbox functions to integrate your differential equation models. In this section we will show how to use MATLAB and Python's built-in functions to integrate:
- a single differential equation
- a system of differential and algebraic equations.
We will only look at initial value problems (IVPs) in this tutorial.
MATLAB
MATLAB's has several ODE solvers available. You can read up more about the implementation details in this technical document.
Python
Like MATLAB, several integrators are available in Python. The integrator I will use in this tutorial is one of the most recent additions to SciPy - the VODE integrator developed at Lawrence Livermore National Laboratories in 1988. It is a good general-purpose solver for both stiff and non-stiff systems.
NOTE: we will use the superior vode Python integrator - it requires a little more code than the other built-in Python integrator, based on lsodea.
Example problem
The example we will work with is a common modelling reaction: a liquid-based stirred tank reactor, with (initially) constant physical properties, a second order chemical reaction where species A is converted to B according to \({\sf A} \rightarrow {\sf B} \), with reaction rate \(r = k C_{\sf A}^2\). One can find the time-dependent mass balance for this system to be:
\[ \frac{dC_{\sf A}(t)}{dt} = \frac{F(t)}{V} \bigg( C_{{\sf A},\text{in}} - C_{\sf A} \bigg) - k C_{\sf A}^2 \]
where \(C_{{\sf A},\text{in}} = 5.5\) mol/L, we will initially assume constant volume of \(V = 100\) L and constant inlet flow of \(F(t) = 20.1 \) L/min. The reaction rate constant is 0.24 \(\text{min}^{-1}\). We must specify an initial condition for every differential equation: we will let \( C_{\sf A}(t=0) = 3.2\) mol/L.
In the code below we will integrate the ODE from \(t_\text{start} = 0.0\) minutes up to \(t_\text{final} = 7.0\) minutes and plot the time-varying trajectory of concentration in the tank.
The reason for the longer Python code is because we have to constructe the vectors that store the time variable and the corresponding concentration output (as a function of that time variable). The MATLAB code builds this vector for us. MATLAB's vectors are very well suited to plotting, but there is a feature that may not be 100% useful:
MATLAB | Python |
---|---|
Let's take a look at the MATLAB output: >> t(1:10)
ans =
0
0.0805
0.1610
0.2414
0.3219
0.4780
0.6341
0.7902
0.9463
1.1213
>> y(1:10)
ans =
3.2000
3.0498
2.9184
2.8030
2.7011
2.5346
2.4014
2.2943
2.2067
2.1264
MATLAB places the points at unequal step sizes of time. This is what their documentation has to say:
|
Let's take a look at the Python output: >>> t[0:10]
array([[ 0. ],
[ 0.1],
[ 0.2],
[ 0.3],
[ 0.4],
[ 0.5],
[ 0.6],
[ 0.7],
[ 0.8],
[ 0.9]])
>>> CA[0:10]
array([[ 3.2 ],
[ 3.01656488],
[ 2.86105764],
[ 2.72822726],
[ 2.61403267],
[ 2.51531912],
[ 2.42958245],
[ 2.35480722],
[ 2.28935862],
[ 2.23189292]])
The evenly spaced periods of time were by design - see the Python code above. After reading the MATLAB description alongside, you might think the Python integration is inaccurate. This is not true: the Python code we have used here will take multiple steps, of variable size, within each of the delta_t time steps we specified, but the code will only report the values at the boundaries of each delta_t, not the intermediate values. |
Example with coupled ODEs
We will now expand our example to a system of two coupled ODEs. Still in the same reactor we are now considering the rate constant to be a function of temperature: \(k = 0.24 e^{-\frac{E_a}{T}}\), with \(E_a = 9000\) J/(mol) and R = \(8.314 \) J/mol.K, and \(T(t)\) is the time-varying tank temperature, measured in Kelvin. Furthermore, the reaction is known to release heat, with \(\Delta H_r = 400\) J/mol. The time-dependent mass and energy balance for this system is now:
\[ \begin{align*}\frac{dC_{\sf A}(t)}{dt} &= \frac{F(t)}{V} \bigg( C_{{\sf A},\text{in}} - C_{\sf A} \bigg) - 0.24 e^{-\frac{E_a}{T}} C_{\sf A}^2 \\ \frac{dT(t)}{dt} &= \frac{F(t)C_p(T)}{V}\bigg(C_{{\sf A},\text{in}}T_\text{in} - C_{\sf A}(t)T(t) \bigg) - 0.24 e^{-\frac{E_a}{T}} C_{\sf A}^2 (\Delta H_r)\end{align*}\]
where \(C_{{\sf A},\text{in}} = 5.5\) mol/L, \(T_\text{in} = 288\) K; we will initially assume constant volume of \(V = 100\) L and constant inlet flow of \(F(t) = 20.1 \) L/min. Also, \(C_p(T) = 128 + 0.02(T-273)\) J/(mol.K), the molar heat capacity of the liquid phase system, a weak function of the system temperature. We need an additional initial condition for the temperature differential equation:
- \( C_{\sf A}(t=0) = 3.2\) mol/L
- \(T(t=0) = 295 K \)
In the code below we will integrate the ODE from \(t_\text{start} = 0.0\) minutes up to \(t_\text{final} = 7.0\) minutes and plot the time-varying trajectory of concentration in the tank.