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 imposed on all the edges of the rectangular domain.

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(xi,yj,t)U_{ij}\approx u(x_i,y_j,t). Terms involving the spatial derivatives of uu are readily replaced by discrete counterparts: DxU\mathbf{D}_x\mathbf{U} for uxu_x, UDyT\mathbf{U}\mathbf{D}_{y}^T for uyu_y, and so on.

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.8) and (13.1.9). Fortunately, it’s easy to translate back and forth between a matrix and an equivalent vector.

The function vec is built-in to Julia, whereas unvec is a particular use case of the reshape function.

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. We proceed similarly here, except that we have changes in the shape as well as boundary conditions to consider.

Suppose we are given a matrix U\mathbf{U} that represents the solution on an (m+1)×(n+1)(m+1)\times (n+1) grid, including boundary values. Then we define

pack(U)=vec(ExUEyT),\operatorname{pack}(\mathbf{U}) = \operatorname{vec}(\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.4) deletes the first and last row of U\mathbf{U} and the right multiplication deletes its first and last column. All that remains, then, are the interior values, which are converted into a vector by the vec operator.

For the inverse transformation, let us be given a vector w\mathbf{w} of interior solution values. Then we define

unpack(w)=ExTunvec(w)Ey.\operatorname{unpack}(\mathbf{w}) = \mathbf{E}_x^T \cdot \operatorname{unvec}(\mathbf{w}) \cdot \mathbf{E}_y.

This operator reshapes the vector to a grid of interior values, then appends one extra zero row and column on each side of the grid.[1]

Now suppose the ODE unknowns for the interior solution values are in the vector w(t)\mathbf{w}(t). When we form unpack(w)\operatorname{unpack}(\mathbf{w}), we reinterpret the values on the tensor-product grid and then extend these values to zero around the boundary. If the boundary values are given as g(x,y)g(x,y), then gg has to be evaluated at the boundary nodes of the grid and inserted into the grid function matrix. Then the grid values are used to compute partial derivatives in xx and yy, the discrete form of the PDE is evaluated, and we pack the result as the computation of w\mathbf{w}'.

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}

Now the grid unknowns are a pair of matrices U(t)\mathbf{U}(t) and V(t)\mathbf{V}(t). Typical boundary conditions would prescribe uu on all of the boundary and let vv be unspecified. Since the boundary values of U\mathbf{U} are prescribed, those values are omitted from the semidiscretization IVP, while all of V\mathbf{V} is included. All of these unknowns need to be packed into and unpacked from a single vector w(t)\mathbf{w}(t) for the IVP solver.

13.2.4Exercises

  1. ⌨ For the given u(x,y)u(x,y), make a plot of the given quantity on the square [2,2]2[-2,2]^2 using appropriate differentiation matrices.

    (a) u(x,y)=exp(xy2)u(x,y) = \exp(x-y^2); plot uxx+uyyu_{xx}+u_{yy}

    (b) u(x,y)=cos(πx)+sin(πy)u(x,y) =\cos (\pi x)+\sin (\pi y); plot ux+uyu_x+u_y

    (c) u(x,y)=exp(x24y2)u(x,y) =\exp(-x^2-4y^2); plot xuyx u_y

  2. ⌨ Following Demo 13.2.2 as a model, solve the Allen–Cahn equation ut=u(1u2)+0.001(uxx+uyy)u_t=u(1-u^2)+0.001(u_{xx}+u_{yy}) on the square [1,1]2[-1,1]^2 with periodic conditions, taking u(x,y,0)=sin(πx)cos(2πy)u(x,y,0)=\sin(\pi x)\cos(2\pi y). Use m=n=60m=n=60 to solve up to t=4t=4, and make an animation of the result.

  3. ⌨ Following Demo 13.2.3 as a model, solve ut=yuxuy+0.03(uxx+uyy)u_t=y u_x-u_y+0.03(u_{xx}+u_{yy}) on the square [1,1]2[-1,1]^2, with u(x,y,0)=(1x2)(1y2)u(x,y,0)=(1-x^2)(1-y^2) and homogeneous Dirichlet boundary conditions. Use m=n=40m=n=40 to solve up to t=2t=2, and make an animation of the result.

  4. ⌨ Following Demo 13.2.4 as a model, solve utt=uxx+uyy+cos(7t)u_{tt}=u_{xx}+u_{yy}+\cos(7t) on the square [1,1]2[-1,1]^2, with u(x,y,0)=x(1x6)(1y2)u(x,y,0)=x(1-x^6)(1-y^2), ut(x,y,0)=0u_t(x,y,0)=0, subject to homogeneous Dirichlet boundary conditions. Take m=n=60m=n=60 to solve between t=0t=0 and t=12t=12, and make an animation of the result.

  5. From Maxwell’s equations we can find a way to convert the wave equation to a first-order form that, unlike (13.2.7), uses only first-order derivatives in space:

    ut=c2(vywx),vt=uy,wt=ux,\begin{split} u_t &= c^2(v_y - w_x),\\ v_t &= u_y, \\ w_t &= -u_x, \end{split}

    subject to u=0u=0 on the boundary.

    (a) ✍ Show that a solution of (13.2.8) satisfies ut=c2(uxx+uyy)u_t=c^2(u_{xx}+u_{yy}).

    (b) ⌨ Solve (13.2.8) with c=2c=2 in the rectangle x[3,3]x\in[-3,3], y[1,1]y\in[-1,1], u(x,y,0)=exp(xx2)(9x2)(1y2)u(x,y,0) = \exp(x-x^2)(9-x^2)(1-y^2), and v=w=0v=w=0 at t=0t=0. Use m=50m=50 for xx and n=25n=25 for yy, solve for 0t60\le t \le 6, and make an animation of the solution.

Footnotes
  1. You might wonder why we use linear algebra to define the extension and deletion of boundary values rather than directly accessing row and column indices in the grid function. The linear algebra approach allows DifferentialEquations to compute the Jacobian matrix of the implicit IVP solver quickly using automatic differentiation tools, greatly speeding up the solution process. Since the matrices in our expressions are sparse, multiplications by them do not affect running time perceptibly.