2 1-D Linear Convection
2.1 What is Convection
To briefly describe, convection is like movement affected by the fluid flow itself.
2.2 The Equation
\[\frac{\partial u }{\partial t} + c \frac{\partial u}{\partial x} = 0\]
- \(c\): speed of initial wave
- Initial condition(at the time \(t = 0\), the velocity of the flow, and here it’s understood as a wave) denotes as \(u_0\):
\[u(x, 0) = u_0(x)\]
Then the exact solution of the linear convection equation:
\[u(x, t) = u_0(x - ct)\]
We discretize this equation in both space and time, using the Forward difference scheme for the time derivative and the Backward difference scheme for the space derivative from the definition of a derivative,
Consider discretizing the spatial coordinate \(x\) into points that we index from \(i = 0\) to \(N\), and stepping in discrete time intervals of size \(\Delta t\)
\[\frac{\partial u}{\partial x} \approx \frac{u(x + \Delta x) - u(x)}{\Delta x}\]
discrete equation follows:
\[\frac{u_i^{n+1} - u_i^n}{\Delta t} + c \frac{u_i^n - u_{i-1}^n}{\Delta x} = 0\]
\(n\) & \(n + 1\): two consecutive steps in time
\(i - 1\) & \(i\): two neighboring points of the discretized x coordinate We can solve for our unknown to get an equation that allows us to advance in time, as follows:
\[u_i^{n+1} = n_i^n - c \frac{\Delta t}{\Delta x}(u_i^n - u_{i-1}^n)\]
2.3 Implementation
nx: steps (= 41) dx = 2 / (nx - 1) (x-start = 0, x-end = 2) nt: the number of timesteps we want to calculate (= 25) dt: the amount of time each timestep covers (delta t) (= .25) c: wave speed (= 1)
initial conditions: 1. initial velocity \(u_0\) is given as \(u = 2\) in the interval \(0.5 \le x \le 1\) and \(u = 1\) everywhere else in \((0, 2)\)
array-u outputs:
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0,
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
time to implement discretization of the convention equation using a finite-difference scheme
def params {:x-start 0
(:x-end 2
:nx 41
:nt 20
:c 1.0
:dt 0.025})
def array-x (one-d/create-array-x params)) (
def array-u (one-d/create-array-u {:array-x array-x})) (
let [nx 41
(:nx nx})
array-x (one-d/create-array-x {:array-x array-x})
array-u (one-d/create-array-u {
u (one-d/simulate array-u params)]
(kind/vega-lite:mark "line"
{:width 500 :height 300
:encoding {:x {:field "x" :type "quantitative"}
:y {:field "y" :type "quantitative"}}
:data {:values (into [] (map #(hash-map :x % :y %2) array-x u))}}))
source: notebooks/steps/step_01.clj