wip: theory for Task 1 (subtask 1 - 4)

This commit is contained in:
Daniel Kapla 2022-05-16 17:42:36 +02:00
parent 58b67a304b
commit c05ea06703
1 changed files with 138 additions and 77 deletions

View File

@ -96,7 +96,12 @@ In the following we will also use the short hand notation
\partial_x^k C_i^n = \left.\frac{\partial^k C(x, t)}{\partial x^k}\right|_{x = x_i, t = t_n}, \qquad
\partial_t^k C_i^n = \left.\frac{\partial^k C(x, t)}{\partial t^k}\right|_{x = x_i, t = t_n}
\end{displaymath}
for $k\in\mathbb{N}$ as well as $i$ and $n$ are the space and time discretization indices, respectively.
for $k\in\mathbb{N}$ as well as $i$ and $n$ are the space and time discretization indices, respectively. To distringuish between the exact solution and the approximation we add a ``hat'' $\widehat{\phantom{C}}$ to the approximation. For example $C_i^n$ is the true value and $\widehat{C}_i^n$ is the discretized solution with discretization errors at the evaluation point $(x_i, t_n)$.
Furthermore, define
\begin{displaymath}
d = \frac{D\Delta t}{\Delta x^2}.
\end{displaymath}
\subsection{Explicit scheme with Dirichlet/Neumann BC}\label{sec:task01_1}
Given the Dirichlet boundary conditions $C(0, t) = 0$ and the Neumann BC $\partial_x C(h, t) = 0$. First we derive a $2^{nd}$ order \emph{explicit} finite difference approach with a $1^{st}$ order discretization in time (see Figure~\ref{fig:ex1}).
@ -176,123 +181,179 @@ Substitution into \eqref{eq:ex1} yields after rearranging the update rule for in
\begin{equation}\label{eq:task01_1_update}
\widehat{C}_i^{n+1} = \widehat{C}_i^n + \frac{D\Delta t}{\Delta x^2}(\widehat{C}_{i-1}^{n} - 2\widehat{C}_{i}^{n} + \widehat{C}_{i+1}^{n}).
\end{equation}
This holds at $x_i$ where $i = 2, ..., N_x - 1$, or in other words everywhere inside the space domain excluding the boundary. The boundary needs to be handled seperately. The left boundary condition is a Dirichlet constraint which is simply constanct giving $C_1^n = 0$ for all time while the Neumann condition on the right requires an additional discretization step for computing the next value. Therefore, we take the second order Taylor expantion of $C$ with respect to $x$ in the negative direction given by
This holds at $x_i$ where $i = 2, ..., N_x - 1$, or in other words everywhere inside the space domain excluding the boundary. The boundary needs to be handled seperately. The left boundary condition is a Dirichlet constraint which is simply constanct giving $C_1^n = 1$ for all time while the Neumann condition on the right requires an additional discretization step for computing the next value. To achieve a $2^{nd}$ order discretization scheme it's usualy required to take the sum/difference of an Taylor expantion into the left and right direction at a given point. As we are on the boundary there is no point at the right, which can be simply solved by adding a ghost point $x_{N_x+1}$ to the system (which will not show up in the final scheme). This leads to the two $3^{rd}$ order expantions evaluated at the right boundary as
\begin{align*}
C_{N_x-1}^n &= C_{N_x}^n - \partial_x C_{N_x}^n\Delta x + \partial_x^2 C_{N_x}^n + \mathcal{O}(\Delta x^3), \\
C_{N_x+1}^n &= C_{N_x}^n + \partial_x C_{N_x}^n\Delta x + \partial_x^2 C_{N_x}^n + \mathcal{O}(\Delta x^3).
\end{align*}
Taking the difference of both equations gives
\begin{displaymath}
C(x - \Delta x, t) = C(x, t) - \partial_x C(x, t)\Delta x + \mathcal{O}(\Delta x^2).
0 = \partial_x C_{N_x}^n\Delta x = \frac{C_{N_x-1}^n - C_{N_x+1}^n}{2\Delta x} + \mathcal{O}(\Delta x^2).
\end{displaymath}
As we are interesetd in the boundary value for the Neumann BC, the derivative at $x = h$ is given which leads to
which yields a $2^{nd}$ order equation for the Neumann BC on the right as
\begin{displaymath}
C(h, t) = C(h - \Delta x, t) + \partial_x C(h, t)\Delta x + \mathcal{O}(\Delta x^2)
C_{N_x-1}^n = C_{N_x+1}^n.
\end{displaymath}
which is second order accurate. Therefore, the right boundary value for our boundary condition $\partial_x C(h, t_n) = 0$ at time $t_n$ is
Now where ready to give an equation system of the entire system (including the boundaryies) which reads
\begin{align*}
\widehat{C}_1^{n+1} &= 1, \\
\widehat{C}_i^{n+1} &= d\widehat{C}_{i-1}^{n} + (1 - 2d)\widehat{C}_{i}^{n} + d\widehat{C}_{i+1}^{n},
&& i = 2, ..., N_x - 1 \\
\widehat{C}_{N_x}^{n+1} &= 2d\widehat{C}_{N_x-1}^{n} + (1 - 2d)\widehat{C}_{N_x}^{n}.
\end{align*}
or in matrix form
\begin{displaymath}
\widehat{C}_{N_x}^n = \widehat{C}_{N_x - 1}^n.
C^{n+1} = A_1 C^{n}
\end{displaymath}
for a tridiagonal matrix $A_1$ of dimensions $N_x\times N_x$
\begin{displaymath}
A_1 = \begin{pmatrix}
1 & 0 \\
d & 1-2d & d \\
& d & 1-2d & d \\
& & d & 1-2d & \ddots \\
& & & \ddots & \ddots & d \\
& & & & d & 1-2d & d \\
& & & & & 2d & 1-2d
\end{pmatrix}.
\end{displaymath}
See \texttt{task01\_1-2.py} or \texttt{Mixed\_EA.m} for an implementation of this scheme.
\subsubsection{Stability} \todo{Prove that the update scheme is unstable for $d = \frac{D\Delta x^2}{\Delta t} > 0.5$}
\subsection{Explicit scheme with Dirichlet BC at both boundaries}\label{sec:task01_2}
Not we have the same setting as in Section~\ref{sec:task01_1} except for Dirichlet boundary conditions on both sides given by $C(0, t) = 1$ and $C(h, t) = 0$. This means tha the left and right values are constants and internal nodes follow the same update scheme as in \eqref{eq:task01_1_update}.
This is identical to the previous task except the right boundary condition. The new boundary condition is a Dirichlet BC with $C_{N_x}^n = 0$ which alters the equation system from Section~\ref{sec:task01_1} in the last two equations to
\begin{align*}
\widehat{C}_1^{n+1} &= 1, \\
\widehat{C}_i^{n+1} &= d\widehat{C}_{i-1}^{n} + (1 - 2d)\widehat{C}_{i}^{n} + d\widehat{C}_{i+1}^{n},
&& i = 2, ..., N_x - 2 \\
\widehat{C}_{N_x-1}^{n+1} &= d\widehat{C}_{N_x-2}^{n} + (1 - 2d)\widehat{C}_{N_x-1}^{n}, \\
\widehat{C}_{N_x}^{n+1} &= 0.
\end{align*}
which yields a new tridiagonal matrix $A_2$ of size $N_x\times N_x$ for the update rule $C^{n+1} = A_2 C^{n}$ as
\begin{displaymath}
A_2 = \begin{pmatrix}
1 & 0 \\
d & 1-2d & d \\
& d & 1-2d & d \\
& & d & 1-2d & \ddots \\
& & & \ddots & \ddots & d \\
& & & & d & 1-2d & 0 \\
& & & & & 0 & 0
\end{pmatrix}.
\end{displaymath}
\todo{include pictures and compare with Section~\ref{sec:task01_1}}
\subsection{Implicit scheme with Dirichlet/Neumann BC}\label{sec:task01_3}
The following computations are similar in nature to the explicit scheme, therefore well keep it short. The implicit discretization for the derivatives is (see: Figure~\ref{fig:ex1}) then
\begin{displaymath}
\partial_x^2 C_{i}^{n+1} = \frac{\widehat{C}_{i-1}^{n+1} - 2\widehat{C}_{i}^{n+1} + \widehat{C}_{i+1}^{n+1}}{\Delta x^2},
\partial_x^2 \widehat{C}_{i}^{n+1} = \frac{\widehat{C}_{i-1}^{n+1} - 2\widehat{C}_{i}^{n+1} + \widehat{C}_{i+1}^{n+1}}{\Delta x^2},
\qquad
\partial_t \widehat{C}_i^n = \frac{\widehat{C}_{i}^{n+1} - \widehat{C}_{i}^{n}}{\Delta t}.
\end{displaymath}
Substitution into \eqref{eq:ex1} gives the implicit (inverse) update rule
Substitution into \eqref{eq:ex1} gives the implicit (inverse) update rules
\begin{align*}
\widehat{C}_1^{n} &= \widehat{C}_1^{n+1} = 1, \\
\widehat{C}_i^{n} &= -d \widehat{C}_{i-1}^{n+1} + (1 + 2 d) \widehat{C}_i^{n+1} - d \widehat{C}_{i+1}^{n+1} && i = 2, ..., N_x - 1 \\
\widehat{C}_{N_x}^{n} &= -2d \widehat{C}_{N_x-1}^{n+1} + (1 + 2 d) \widehat{C}_{N_x}^{n+1}
\end{align*}
which leads again to an matrix equation
\begin{displaymath}
\widehat{C}_i^{n} = -d \widehat{C}_{i-1}^{n+1} + (1 + 2 d) \widehat{C}_i^{n+1} - d \widehat{C}_{i+1}^{n+1}
C^n = A_3 C^{n+1}
\end{displaymath}
for $i = 2, ..., N_x - 1$ and $d = \frac{D \Delta x^2}{\Delta t}$. The boundary conditions are ether $\widehat{C}_1^{n+1} = 0$ for the left Dirichlet and $\widehat{C}_{N_x}^{n+1} = \widehat{C}_{N_x-1}^{n+1}$ as the right Neumann BC. By collecting all coefficients of the $i = 2, ..., N_x - 1$ into a $(N_x - 2)\times (N_x - 2)$ trigiagonal matrix
where the $N_x\times N_x$ tridiagonal matrix $A_3$ has the form
\begin{displaymath}
A = \begin{pmatrix}
1+2d & -d & \\
-d & 1+2d & -d & \\
& -d & 1+2d & \ddots \\
& & \ddots & \ddots & -d \\
& & & -d & 1+2d & -d \\
& & & & -d & 1+d
A_3 = \begin{pmatrix}
1 & 0 \\
-d & 1+2d & -d \\
& -d & 1+2d & -d \\
& & -d & 1+2d & \ddots \\
& & & \ddots & \ddots & -d \\
& & & & -d & 1+2d & -d \\
& & & & & -2d & 1+2d
\end{pmatrix}
\end{displaymath}
\todo{fix $1+d$ which is first order accurate!!!}
where the last entry $A_{N_x, N_x} = 1 - d$ is due to the Neumann BC $\widehat{C}_{N_x}^{n+1} = \widehat{C}_{N_x-1}^{n+1}$ which means
\begin{displaymath}
\widehat{C}_i^{N_x-1} = -d \widehat{C}_i^{N_x-2} + (1 + 2 d) \widehat{C}_i^{N_x - 1} - d \widehat{C}_i^{N_x}
= -d \widehat{C}_i^{N_x-2} + (1 + d) \widehat{C}_i^{N_x - 1}.
\end{displaymath}
Finaly, we end up with the implicit update rule
\begin{displaymath}
\widehat{C}^{n+1} = A \widehat{C}^{n}
\end{displaymath}
which is perfomed by solving the linear system for $\widehat{C}^{n+1}$.
and the update is performed by solving for $C^{n+1}$.
\todo{include pictures and compare with Section~\ref{sec:task01_1}}
\subsection{Second order in time for implicit scheme with Dirichlet/Neumann BC}
\begin{figure}[h!]
\centering
\begin{tikzpicture}[>=latex]
\draw[->] (0.8, 0) -- (5.5, 0) node[anchor = west] {$x$};
\draw[->] (1, -0.2) -- (1, 3.5) node[anchor = south] {$t$};
\draw[dashed] (0.8, 1.5) node[anchor = east] {$t_{n+1/2}$} -- (5.5, 1.5);
\draw[dashed] (3, -0.2) node[anchor = north] {$x_i$}
-- (3, 3.5) node[anchor = south] {Crank-Nicolson};
\foreach \x in {1, ..., 5} {
\foreach \y in {0, ..., 3} {
\node[circle, draw, fill = white,
inner sep = 0pt, outer sep = 0pt,
minimum size = 4pt] at (\x, \y) {};
}
}
\node[circle, fill = black,
inner sep = 0pt, outer sep = 0pt,
minimum size = 4pt] at (3, 1.5) {};
\foreach \x/\y in {2/1, 3/1, 4/1, 2/2, 3/2, 4/2} {
\node[circle, fill = gray,
inner sep = 0pt, outer sep = 0pt,
minimum size = 4pt] at (\x, \y) {};
}
\end{tikzpicture}
\caption{\label{fig:ex4}.Crank-Nicolson dependency relations.}
\end{figure}
To derive the second order accurate scheme in time we employ the Crank-Nicolson approach to derive the descritization at the time midpoints $t_{n+1/2} = (n + 1/2)\Delta t$ which we index with $n+1/2$.
The second order im space is identical except the evaluation points
\begin{displaymath}
\partial_x^2 C_i^{n+1/2} = \frac{C_{i-1}^{n+1/2} - 2C_{i}^{n+1/2} + C_{i+1}^{n+1/2}}{\Delta x^2} + \mathcal{O}(\Delta x^2)
\end{displaymath}
and for the time descritization we take the Taylor expantion
\begin{displaymath}
\partial_{t}C_i^{n+1/2} = \frac{C_i^{n+1} - C_i^{n}}{2\Delta t} + \mathcal{O}(\Delta t^2)
\end{displaymath}
\todo{check if this follows from $\exists \partial_x^4 C$ beeing continuous}
\begin{displaymath}
\partial_x^2 C_i^{n+1/2} = \frac{\partial_x^2 C_i^{n} + \partial_x^2 C_i^{n+1}}{2} + \mathcal{O}(\Delta x^2)
\end{displaymath}
Meaning
By expantion into $\pm\Delta t / 2$ at the midpoints we get
\begin{align*}
0 = \partial_{t}C_i^{n+1/2}-D\partial_x^2 C_i^{n+1/2} &= \frac{C_i^{n+1} - C_i^{n}}{2\Delta t}
-D\frac{\partial_x^2 C_i^{n} + \partial_x^2 C_i^{n+1}}{2} + \mathcal{O}(\Delta t^2 + ?) \\
&= \frac{C_i^{n+1} - C_i^{n}}{2\Delta t} -
D\frac{C_{i-1}^{n} + C_{i-1}^{n+1} - 2(C_{i}^{n} + C_{i}^{n+1}) + C_{i+1}^{n} + C_{i+1}^{n+1}}{2\Delta x^2}
C_i^n &= C_i^{n+1/2} - \partial_tC_i^{n+1/2}\frac{\Delta t}{2} + \partial_t^2 C_i^{n+1/2}\frac{\Delta t^2}{8} + \mathcal{O}(\Delta t^3), \\
C_i^{n+1} &= C_i^{n+1/2} + \partial_tC_i^{n+1/2}\frac{\Delta t}{2} + \partial_t^2 C_i^{n+1/2}\frac{\Delta t^2}{8} + \mathcal{O}(\Delta t^3).
\end{align*}
rearranging yields
Taking the difference yields a $2^{nd}$ order approx at the time midpoints
\begin{displaymath}
-d C_{i-1}^{n+1} + (1 + 2d) C_i^{n+1}-dC_{i+1}^{n+1}
= d C_{i-1}^n + (1 - 2d)C_i^n + dC_{i+1}^n
= -(-d C_{i-1}^n + (1 + 2d)C_i^n - dC_{i+1}^n) + 2C_i^n
\partial_t C_{i}^{n+1/2} = \frac{C_i^{n+1} - C_i^n}{\Delta t} + \mathcal{O}(\Delta t^2).
\end{displaymath}
Now with second order approximation of the Neumann BC
while the $2^{nd}$ order space discretization is analog to above but evaluated also at $(x_i, t_{n+1/2})$ to get
\begin{displaymath}
0\overset{!}{=} \partial_x C_{N_x}^{n+1} = \frac{C_{N_x-2}^{n+1} - C_{N_x}^{n+1}}{2\Delta x} + \mathcal{O}(\Delta x^2)
\partial_x^2 C_{i}^{n+1/2} = \frac{C_{i-1}^{n+1} - 2C_{i}^{n+1} + C_{i+1}^{n+1}}{\Delta x^2} + \mathcal{O}(\Delta x^2)
\end{displaymath}
leading to an second order in time and space discretization scheme
\begin{displaymath}
\partial_t \widehat{C}_{i}^{n+1/2} - D \partial_x^2 \widehat{C}_{i}^{n+1/2} = \mathcal{O}(\Delta x^2 + \Delta t^2).
\end{displaymath}
which means that (\todo{check why?! should be at $N_x - 1$}) for index $i = N_x - 1$
\begin{align*}
-d C_{N_x-2}^{n+1} + (1 + 2d) C_{N_x-1}^{n+1}-dC_{N_x}^{n+1}
&= -(-d C_{N_x-2}^n + (1 + 2d)C_{N_x-1}^n - dC_{N_x}^n) + 2C_{N_x-1}^n \\
-d C_{N_x-2}^{n+1} + (1 + 2d) C_{N_x-1}^{n+1}-d C_{N_x-2}^{n+1}
&= -(-d C_{N_x-2}^n + (1 + 2d)C_{N_x-1}^n - d C_{N_x-2}^{n}) + 2C_{N_x-1}^n \\
-2d C_{N_x-2}^{n+1} + (1 + 2d) C_{N_x-1}^{n+1}
&= -(-2d C_{N_x-2}^n + (1 + 2d)C_{N_x-1}^n) + 2C_{N_x-1}^n
\end{align*}
Next we need to ensure the evaluation to be done at the known grid points instead of the unknown time midpoints. Therefore, we replace the evaluation at the time midpoints by there in time mean of the two direct neighbouring grid points
\begin{displaymath}
A C^{n+1} = (2 I - A) C^n
\widehat{C}_i^{n+1/2} = \frac{\widehat{C}_i^{n} + \widehat{C}_i^{n+1}}{2}.
\end{displaymath}
with
After a bit of algebra and condideration of the Dirichlet and Neumann BC we end up with
\begin{align*}
\widehat{C}_1^{n+1} &= \widehat{C}_1^{n} = 1, \\
\widehat{C}_i^{n+1} - \frac{d}{2}\widehat{C}_{i-1}^{n+1} + dC_{i}^{n+1} - \frac{d}{2}\widehat{C}_{i+1}^{n+1}
&= C_i^{n} + \frac{d}{2}\widehat{C}_{i-1}^{n} - dC_{i}^{n} + \frac{d}{2}\widehat{C}_{i+1}^{n},
&& i = 2, ..., N_x - 1, \\
\widehat{C}_i^{n+1} - d\widehat{C}_{N_x-1}^{n+1} + dC_{N_x}^{n+1}
&= C_i^{n} + d\widehat{C}_{N_x-1}^{n} - dC_{N_x}^{n}.
\end{align*}
To write this again in a compact matrix equation system we define the $N_x\times N_x$ matrix $A_4$ as
\begin{displaymath}
A = \begin{pmatrix}
1 & 0 & 0 \\
-d & 1+2d & -d \\
& -d & 1+2d & -d \\
& & -d & 1+2d & \ddots \\
& & & \ddots & \ddots & -d \\
& & & & -d & 1+2d & -d \\
& & & & & -2d & 1+2d
A_4 = \begin{pmatrix}
0 & 0 \\
-d/2 & d & -d/2 \\
& -d/2 & d & \ddots \\
& & \ddots & \ddots & -d/2 \\
& & & -d/2 & d & -d/2 \\
& & & & -d & d\\
\end{pmatrix}
\end{displaymath}
then the entire system has the form
\begin{displaymath}
(I + A_4)C^{n+1} = (I - A_4)C^n
\end{displaymath}
where $I$ is the identity. Again, the update is performed by solving this tridiagonal system for $C^{n+1}$.
\end{document}