Skip to article frontmatterSkip to article content

Two-dimensional diffusion and advection

We next describe how to apply the method of lines to PDEs of the form

ut=ϕ(u,ux,uy,uxx,uxy,uyy),(x,y)[a,b]×[c,d]. u_t = \phi(u,u_x,u_y,u_{xx},u_{xy},u_{yy}), \quad (x,y)\in [a,b]\times [c,d].

The PDE may be of either parabolic or hyperbolic type, with the primary difference being potential restrictions on the time step size. To keep descriptions and implementations relatively simple, we will consider only periodic conditions or Dirichlet boundary conditions.

As described in Tensor-product discretizations, the rectangular domain is discretized by a grid (xi,yj)(x_i,y_j) for i=0,,mi=0,\ldots,m and j=0,,nj=0,\ldots,n. The solution is semidiscretized as a matrix U(t)\mathbf{U}(t) such that UijU_{ij} is the approximate solution at (xi,yj,t)(x_i,y_j,t). Terms involving the spatial derivatives of uu are readily replaced by discrete counterparts, as shown in Table 13.2.1.

Table 13.2.1:Discrete replacements for 2D derivatives

termdiscrete replacement
uuU\mathbf{U}
uxu_xDxU\mathbf{D}_x\mathbf{U}
uyu_yUDyT\mathbf{U}\mathbf{D}_y^T
uxxu_{xx}DxxU\mathbf{D}_{xx}\mathbf{U}
uyyu_{yy}UDyyT\mathbf{U}\mathbf{D}_{yy}^T

13.2.1Matrix and vector shapes

Our destination is an IVP that can be solved by a Runge–Kutta or multistep solver. These solvers are intended for vector problems, but our unknowns naturally have a matrix shape, which is the most convenient for the differentiation formulas (13.1.7) and (13.1.8). Fortunately, it’s easy to translate back and forth between a matrix and an equivalent vector.

Suppose U=mtx(u)\mathbf{U} = \operatorname{mtx}(u) is the matrix of unknowns. Table 13.2.2 shows how to convert between U\mathbf{U} and u=vec(U)\mathbf{u} = \operatorname{vec}(\mathbf{U}).

Table 13.2.2:vec and unvec operations

Julia
MATLAB
Python
vecunvec
u = vec(U)U = reshape(u, m+1, n+1)

In order to modularize our codes, we use Algorithm 13.2.1 to define functions and values related to working with tensor-product grids. Its final output is to be discussed and used in Laplace and Poisson equations.

13.2.2Periodic end conditions

If the boundary conditions are periodic, then the unknowns in the method of lines are the elements of the matrix U(t)\mathbf{U}(t) representing grid values of the numerical solution. For the purposes of an IVP solution, this matrix is equivalent to the vector u(t)\mathbf{u}(t) defined as u=vec(U)\mathbf{u}=\operatorname{vec}(\mathbf{U}).

13.2.3Dirichlet conditions

In Boundaries we coped with boundary conditions by removing the boundary values from the vector of unknowns being solved in the semidiscretized ODE. Each evaluation of the time derivative required us to extend the values to include the boundaries before applying differentiation matrices in space, then remove them from the time derivative vector.

We proceed similarly here, defining chop and extend functions that convert between the full grid and the inner grid of interior values. Mathematically speaking, the chopping operation is defined by

chop(U)=ExUEyT,\operatorname{chop}(\mathbf{U}) = \mathbf{E}_x \mathbf{U} \mathbf{E}_y^T,

where

Ex=[010000010000010]\mathbf{E}_x = \begin{bmatrix} 0 & 1 & 0 & \cdots & 0 & 0 \\ 0 & 0 & 1 & \cdots & 0 & 0 \\ & & & \ddots & & \\ 0 & 0 & 0 & \cdots & 1 & 0 \end{bmatrix}

is (m1)×(m+1)(m-1)\times (m+1), and Ey\mathbf{E}_y is analogous but of size (n1)×(n+1)(n-1)\times (n+1). The left multiplication in (13.2.5) deletes the first and last row of U\mathbf{U}, and the right multiplication deletes its first and last column.

The extension operator is a bit more awkward to write out. It stars with appending rows and columns of zeros around the border of a matrix W\mathbf{W} of interior values:

U~=ExTWEy\tilde{\mathbf{U}} = \mathbf{E}_x^T \mathbf{W} \mathbf{E}_y

We can then modify the new zero values to reflect the boundary conditions, via

U=extend(W)=U~+G,\mathbf{U} = \operatorname{extend}(\mathbf{W}) = \tilde{\mathbf{U}} + \mathbf{G},

Finally, we combine extending and chopping with the need to reshape between vectors and matrices. The function

unpack(w)=extend(unvec(w))\operatorname{unpack}(\mathbf{w}) = \operatorname{extend}(\operatorname{unvec}(\mathbf{w}))

converts a vector of unknowns (i.e., interior values) into a matrix of grid values, including the boundary values. The function

pack(U)=vec(chop(U))\operatorname{pack}(\mathbf{U}) = \operatorname{vec}(\operatorname{chop}(\mathbf{U}))

reverses the transformation, which is needed after the time derivative is computed on the full grid.

13.2.4Wave equation

The wave equation introduces a little additional complexity. First, we write the 2D wave equation utt=c2(uxx+uyy)u_{tt}=c^2(u_{xx}+u_{yy}) in first-order form as

ut=v,vt=c2(uxx+uyy).\begin{split} u_t &= v, \\ v_t &= c^2(u_{xx}+u_{yy}). \end{split}

Typical boundary conditions are to prescribe uu on the boundary and let vv be unspecified.

Now the grid functions are a pair of matrices U(t)\mathbf{U}(t) and V(t)\mathbf{V}(t). We need to chop U\mathbf{U} to an interior W\mathbf{W} and extend back using boundary data. Note that the IVP unknowns W\mathbf{W} and V\mathbf{V} have different sizes, so there are two separate reshaping operations involved. All of these details are handled within the pack and unpack functions we create.

13.2.5Exercises