15.0 s

Matrices from partial differential equations

As we focus in this course on partial differential equations, we need discuss matrices which evolve from the discretization of PDEs.

  • Are there any structural or numerical patterns in these matrices we can take advantage of with regard to memory and time complexity when solving linear systems ?

In this lecture we introduce a relatively simple "drosophila" problem which we will use do discuss these issues.

For the start we use simple structured disceretization grids and a finite difference approach to the discretization. Later, this will be generalized to more general grids and to finite element and finite volume discretization methods.

2.0 ms

Heat conduction in a one-dimensional rod

  • Heat source f(x)

  • vL,vR: ambient temperatures

  • α: boundary heat transfer coefficient

  • Second order boundary value problem in Ω=[0,1]:

{u(x)=f(x)inΩu(0)+α(u(0)vL)=0u(1)+α(u(1)vR)=0

  • The solution u describes the equilibrium temperature distribution. Behind the second derivative is Fouriers law and the continuity equation

  • In math, the boundary conditions are called "Robin" or "third kind". They describe a heat in/outflux proportional to the difference between rod end temperature and ambient temperature

  • Fix a number of discretization points N

  • Let h=1N1

  • Let xi=(i1)h i=1N be discretization points

12.3 μs
N
11
1.1 μs
plotgrid (generic function with 1 method)
107 μs
633 ms

Finite difference approximation

We can approximate continuous functions f by piecewise linear functions defined by the values fi=f(xi). Using more points yields a better approximation:

7.8 μs
196 ms
  • Let ui approximations for u(xi) and fi=f(xi)

  • We can use a finite difference approximation to approximate u(xi+12)ui+1uih

  • Same approach for second derivative: u(xi)=u(xi+12)u(xi12)h

  • Finite difference approximation of the PDE:

u(0)+α(u(0)vL)12h(u0u2)+α(u1vL)=0u(xi)f(xi)ui+1+2uiui1h2fi=0(i=1N)()u(1)+α(u(1)vR)12h(uN+1uN1)+α(uNvR)=0

9.3 μs
114 ms
  • Here, we introduced "mirror values" u0 and uN+1 in order to approximate the boundary conditions accurately, such that the finite difference formulas used to approximate u(0) or u(1) are centered around these values.

  • After rearranging, these values can be expressed via the boundary conditions:

u0=u2+2hα(u1vL)uN+1=uN1+2hα(uNvL)

  • Finally, they can be replaced in ()

5.9 μs

Then, the system after multiplying by h is reduced to:

1h((1+hα)u1u2)=h2f1+αvL1h(ui+1+2uiui1)=hfi(i=2N1)1h((1+hα)uNuN1)=h2fN+αvR

2.4 μs

The resulting discretization matrix is

A=(α+1h1h1h2h1h1h2h1h1h2h1h1h2h1h1h1h+α)

Outside of the three diagonals, the entries are zero.

3.2 μs

The right hand side is:

(h2f1+αvLhf2hf3hfN2hfN1h2fN+αvR)

2.5 μs

Let us define functions assembling these:

2.3 μs
heatmatrix1d (generic function with 1 method)
54.7 μs
heatrhs1d (generic function with 1 method)
123 μs
α
100
959 ns
N1
10000
1.2 μs
A
10000×10000 Array{Float64,2}:
 10099.0  -9999.0      0.0      0.0      0.0  …      0.0      0.0      0.0      0.0
 -9999.0  19998.0  -9999.0      0.0      0.0         0.0      0.0      0.0      0.0
     0.0  -9999.0  19998.0  -9999.0      0.0         0.0      0.0      0.0      0.0
     0.0      0.0  -9999.0  19998.0  -9999.0         0.0      0.0      0.0      0.0
     0.0      0.0      0.0  -9999.0  19998.0         0.0      0.0      0.0      0.0
     0.0      0.0      0.0      0.0  -9999.0  …      0.0      0.0      0.0      0.0
     0.0      0.0      0.0      0.0      0.0         0.0      0.0      0.0      0.0
     ⋮                                        ⋱                             
     0.0      0.0      0.0      0.0      0.0         0.0      0.0      0.0      0.0
     0.0      0.0      0.0      0.0      0.0  …  -9999.0      0.0      0.0      0.0
     0.0      0.0      0.0      0.0      0.0     19998.0  -9999.0      0.0      0.0
     0.0      0.0      0.0      0.0      0.0     -9999.0  19998.0  -9999.0      0.0
     0.0      0.0      0.0      0.0      0.0         0.0  -9999.0  19998.0  -9999.0
     0.0      0.0      0.0      0.0      0.0         0.0      0.0  -9999.0  10099.0
92.2 ms
b
25.4 ms
u
12.5 s
716 ms

For this example, we created an N×N matrix where all entries outside of the main diagonal and the two adjacent ones are zero:

  • Fraction of nonzero entries: 0.00029998

  • Ratio of nonzero entries to number of unknowns: 2.9998

  • In fact, this matrix has O(N) nonzero entries.

331 ms

2D heat conduction

2.4 μs

Just pose the heat problem in a 2D domain Ω=(0,1)×(0,1):

{2ux22uy2=f(x,y)inΩun+α(uv)=0onΩ

We use 2D regular discretization n×n grid with grid points xij=((i1)h,(j1)h). The finite difference approximation yields:

ui1,jui,j1+4uijui+1,jui,j+1h2=fij

This just comes from summing up the 1D finite difference formula for the x and y directions.

We do not discuss the boundary conditions here.

The n×n grid leads to an n2×n2 matrix!

4.6 μs
plotgrid2d (generic function with 1 method)
108 μs
160 ms

Matrix and right hand side assembly inspired by the finite volume method which will be covered later in the course. The result is the same as for the finite difference method with the mirror trick for the boundary condition.

2.3 μs
heatmatrix2d (generic function with 1 method)
65.3 μs
heatrhs2d (generic function with 1 method)
97.3 μs
n
5
1.4 μs
b2
107 ms
A2
25×25 Array{Float64,2}:
 202.0   -1.0    0.0    0.0    0.0   -1.0   0.0  …    0.0    0.0    0.0    0.0    0.0
  -1.0  103.0   -1.0    0.0    0.0    0.0  -1.0       0.0    0.0    0.0    0.0    0.0
   0.0   -1.0  103.0   -1.0    0.0    0.0   0.0       0.0    0.0    0.0    0.0    0.0
   0.0    0.0   -1.0  103.0   -1.0    0.0   0.0       0.0    0.0    0.0    0.0    0.0
   0.0    0.0    0.0   -1.0  202.0    0.0   0.0       0.0    0.0    0.0    0.0    0.0
  -1.0    0.0    0.0    0.0    0.0  103.0  -1.0  …    0.0    0.0    0.0    0.0    0.0
   0.0   -1.0    0.0    0.0    0.0   -1.0   4.0       0.0    0.0    0.0    0.0    0.0
   ⋮                                  ⋮          ⋱    ⋮                         
   0.0    0.0    0.0    0.0    0.0    0.0   0.0       0.0    0.0    0.0    0.0   -1.0
   0.0    0.0    0.0    0.0    0.0    0.0   0.0  …  202.0   -1.0    0.0    0.0    0.0
   0.0    0.0    0.0    0.0    0.0    0.0   0.0      -1.0  103.0   -1.0    0.0    0.0
   0.0    0.0    0.0    0.0    0.0    0.0   0.0       0.0   -1.0  103.0   -1.0    0.0
   0.0    0.0    0.0    0.0    0.0    0.0   0.0       0.0    0.0   -1.0  103.0   -1.0
   0.0    0.0    0.0    0.0    0.0    0.0   0.0       0.0    0.0    0.0   -1.0  202.0
38.0 ms

In order to inspect the matrix, we can turn it into a DataFrame, which can be browsed.

2.4 μs
x1x2x3x4x5x6x7x8more
Float64Float64Float64Float64Float64Float64Float64Float64
1
202.0
-1.0
0.0
0.0
0.0
-1.0
0.0
0.0
2
-1.0
103.0
-1.0
0.0
0.0
0.0
-1.0
0.0
3
0.0
-1.0
103.0
-1.0
0.0
0.0
0.0
-1.0
4
0.0
0.0
-1.0
103.0
-1.0
0.0
0.0
0.0
5
0.0
0.0
0.0
-1.0
202.0
0.0
0.0
0.0
6
-1.0
0.0
0.0
0.0
0.0
103.0
-1.0
0.0
7
0.0
-1.0
0.0
0.0
0.0
-1.0
4.0
-1.0
8
0.0
0.0
-1.0
0.0
0.0
0.0
-1.0
4.0
9
0.0
0.0
0.0
-1.0
0.0
0.0
0.0
-1.0
10
0.0
0.0
0.0
0.0
-1.0
0.0
0.0
0.0
more
25
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
317 ms
u2
18.6 μs
152 ms

In order to achieve this, we stored a matrix which has only five nonzero diagonals as a full N×N matrix, where N=n2:

  • Fraction of nonzero entries: 0.168

  • Ratio of nonzero entries to number of unknowns: 4.2

  • In fact, this matrix has O(N) nonzero entries.

... there must be a better way!

32.1 μs