Example: 1D diffusion with advection for steady flow, with multiple channel connections
This is a solution usually employed for many purposes when there is a contamination problem in streams or rivers under steady flow conditions, but information is given in one dimension only. Often the problem can be simplified into a 1-dimensional problem and still yield useful information. Here we model the concentration of a solute contaminant in water. This problem is composed of three parts: the known diffusion equation (D_x chosen as constant), an
advective component (which means that the system is evolving in space due to a velocity field), which we choose to be a constant U_x, and a lateral interaction between longitudinal channels (k): {{NumBlk|:|\frac{\partial C}{\partial t} = D_x \frac{\partial^2 C}{\partial x^2} - U_x \frac{\partial C}{\partial x} - k(C - C_N) - k(C - C_M),|}} where C is the concentration of the contaminant, and subscripts N and M correspond to
previous and
next channel. The Crank–Nicolson method (where i represents position, and j time) transforms each component of the PDE into the following: {{NumBlk|:|\frac{\partial C}{\partial t} \Rightarrow \frac{C_i^{j+1} - C_i^j}{\Delta t},|}} {{NumBlk|:|\frac{\partial^2 C}{\partial x^2} \Rightarrow \frac{1}{2 (\Delta x)^2}\left( (C_{i+1}^{j+1} - 2 C_i^{j+1} + C_{i-1}^{j+1}) + (C_{i+1}^j - 2 C_i^j + C_{i-1}^j) \right),|}} {{NumBlk|:|\frac{\partial C}{\partial x} \Rightarrow \frac{1}{2}\left( \frac{(C_{i+1}^{j+1} - C_{i-1}^{j+1})}{2 (\Delta x)} + \frac{(C_{i+1}^j - C_{i-1}^j)}{2 (\Delta x)} \right),|}} {{numBlk|:|C \Rightarrow \frac{1}{2} (C_i^{j+1} + C_i^j),|}} {{NumBlk|:|C_N \Rightarrow \frac{1}{2} (C_{Ni}^{j+1} + C_{Ni}^j),|}} {{numBlk|:|C_M \Rightarrow \frac{1}{2} (C_{Mi}^{j+1} + C_{Mi}^j).|}} Now we create the following constants to simplify the algebra: : \lambda = \frac{D_x \, \Delta t}{2 \, \Delta x^2}, : \alpha = \frac{U_x \, \Delta t}{4 \, \Delta x}, : \beta = \frac{k \, \Delta t}{2}, and substitute (), (), (), (), (), (), \alpha, \beta and \lambda into (). We then put the
new time terms on the left (j+1) and the
present time terms on the right (j) to get : -\beta C_{Ni}^{j+1} - (\lambda + \alpha)C_{i-1}^{j+1} + (1 + 2\lambda + 2\beta)C_i^{j+1} - (\lambda - \alpha)C_{i+1}^{j+1} - \beta C_{Mi}^{j+1} = {} : \qquad \beta C_{Ni}^j + (\lambda + \alpha)C_{i-1}^j + (1 - 2\lambda - 2\beta)C_i^j + (\lambda - \alpha)C_{i+1}^j + \beta C_{Mi}^j. To model the
first channel, we realize that it can only be in contact with the following channel (M), so the expression is simplified to : -(\lambda + \alpha)C_{i-1}^{j+1} + (1 + 2\lambda + \beta)C_i^{j+1} - (\lambda - \alpha)C_{i+1}^{j+1} - \beta C_{Mi}^{j+1} = {} : \qquad {} +(\lambda + \alpha)C_{i-1}^j + (1 - 2\lambda - \beta)C_i^j + (\lambda - \alpha)C_{i+1}^j + \beta C_{Mi}^j. In the same way, to model the
last channel, we realize that it can only be in contact with the previous channel (N), so the expression is simplified to : -\beta C_{Ni}^{j+1} - (\lambda + \alpha)C_{i-1}^{j+1} + (1 + 2\lambda + \beta)C_i^{j+1} - (\lambda - \alpha)C_{i+1}^{j+1} = {} : \qquad \beta C_{Ni}^j + (\lambda + \alpha)C_{i-1}^j + (1 - 2\lambda - \beta)C_i^j + (\lambda - \alpha)C_{i+1}^j. To solve this linear system of equations, we must now see that boundary conditions must be given first to the beginning of the channels: : C_0^j: boundary condition for the channel at present time step, : C_{0}^{j+1}: boundary condition for the channel at next time step, : C_{N0}^j: boundary condition for the previous channel to the one analyzed at present time step, : C_{M0}^j: boundary condition for the next channel to the one analyzed at present time step. For the last cell of the channels (z), the most convenient condition becomes an adiabatic one, so : \left. \frac{\partial C}{\partial x} \right|_{x=z} = \frac{C_{i+1} - C_{i-1}}{2 \, \Delta x} = 0. This condition is satisfied if and only if (regardless of a null value) : C_{i+1}^{j+1} = C_{i-1}^{j+1}. Let us solve this problem (in a matrix form) for the case of 3 channels and 5 nodes (including the initial boundary condition). We express this as a linear system problem: : \mathbf{AA}\,\mathbf{C^{j+1}} = \mathbf{BB}\,\mathbf{C^j} + \mathbf{d}, where : \mathbf{C^{j+1}} = \begin{bmatrix} C_{11}^{j+1}\\ C_{12}^{j+1} \\ C_{13}^{j+1} \\ C_{14}^{j+1} \\ C_{21}^{j+1}\\ C_{22}^{j+1} \\ C_{23}^{j+1} \\ C_{24}^{j+1} \\ C_{31}^{j+1}\\ C_{32}^{j+1} \\ C_{33}^{j+1} \\ C_{34}^{j+1} \end{bmatrix}, \quad \mathbf{C^j} = \begin{bmatrix} C_{11}^j\\ C_{12}^j \\ C_{13}^j \\ C_{14}^j \\ C_{21}^j\\ C_{22}^j \\ C_{23}^j \\ C_{24}^j \\ C_{31}^j\\ C_{32}^j \\ C_{33}^j \\ C_{34}^j \end{bmatrix}. Now we must realize that
AA and
BB should be arrays made of four different subarrays (remember that only three channels are considered for this example, but it covers the main part discussed above): : \mathbf{AA} = \begin{bmatrix} AA1 & AA3 & 0 \\ AA3 & AA2 & AA3 \\ 0 & AA3 & AA1 \end{bmatrix}, \quad \mathbf{BB} = \begin{bmatrix} BB1 & -AA3 & 0 \\ -AA3 & BB2 & -AA3 \\ 0 & -AA3 & BB1 \end{bmatrix}, where the elements mentioned above correspond to the next arrays, and an additional 4×4 full of zeros. Please note that the sizes of AA and BB are 12×12: : \mathbf{AA1} = \begin{bmatrix} (1 + 2\lambda + \beta) & -(\lambda - \alpha) & 0 & 0 \\ -(\lambda + \alpha) & (1 + 2\lambda + \beta) & -(\lambda - \alpha) & 0 \\ 0 & -(\lambda + \alpha) & (1 + 2\lambda + \beta) & -(\lambda - \alpha) \\ 0 & 0 & -2\lambda & (1 + 2\lambda + \beta) \end{bmatrix}, : \mathbf{AA2} = \begin{bmatrix} (1 + 2\lambda + 2\beta) & -(\lambda - \alpha) & 0 & 0 \\ -(\lambda + \alpha) & (1 + 2\lambda + 2\beta) & -(\lambda - \alpha) & 0 \\ 0 & -(\lambda + \alpha) & (1 + 2\lambda + 2\beta) & -(\lambda - \alpha) \\ 0 & 0 & -2\lambda & (1 + 2\lambda + 2\beta) \end{bmatrix}, : \mathbf{AA3} = \begin{bmatrix} -\beta & 0 & 0 & 0 \\ 0 & -\beta & 0 & 0 \\ 0 & 0 & -\beta & 0 \\ 0 & 0 & 0 & -\beta \end{bmatrix}, : \mathbf{BB1} = \begin{bmatrix} (1 - 2\lambda - \beta) & (\lambda - \alpha) & 0 & 0 \\ (\lambda + \alpha) & (1 - 2\lambda - \beta) & (\lambda - \alpha) & 0 \\ 0 & (\lambda + \alpha) & (1 - 2\lambda - \beta) & (\lambda - \alpha) \\ 0 & 0 & 2\lambda & (1 - 2\lambda - \beta) \end{bmatrix}, : \mathbf{BB2} = \begin{bmatrix} (1 - 2\lambda - 2\beta) & (\lambda - \alpha) & 0 & 0 \\ (\lambda + \alpha) & (1 - 2\lambda - 2\beta) & (\lambda - \alpha) & 0 \\ 0 & (\lambda + \alpha) & (1 - 2\lambda - 2\beta) & (\lambda - \alpha) \\ 0 & 0 & 2\lambda & (1 - 2\lambda - 2\beta) \end{bmatrix}. The
d vector here is used to hold the boundary conditions. In this example it is a 12×1 vector: : \mathbf{d} = \begin{bmatrix} (\lambda + \alpha)(C_{10}^{j+1} + C_{10}^j) \\ 0 \\ 0 \\ 0 \\ (\lambda + \alpha)(C_{20}^{j+1} + C_{20}^j) \\ 0 \\ 0 \\ 0 \\ (\lambda + \alpha)(C_{30}^{j+1} + C_{30}^j) \\ 0 \\ 0 \\ 0 \end{bmatrix}. To find the concentration at any time, one must iterate the following equation: : \mathbf{C^{j+1}} = \mathbf{AA}^{-1} (\mathbf{BB}\,\mathbf{C^j} + \mathbf{d}). ==Example: 2D diffusion==