Skip to content

Commit

Permalink
Update README.md and add example
Browse files Browse the repository at this point in the history
Updated README.md containing simple performance test and added example
of how to assemble 1d poisson.
  • Loading branch information
ahojukka5 committed Oct 6, 2018
1 parent 995b86d commit 2962534
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README.md
Expand Up @@ -21,6 +21,47 @@ dofs2 = [4, 5]
K[dofs1, dofs2] += Ke
```

## Performance test

To demonstrate the performance of the package, Poisson problem in 1 dimension
is assembled (see `examples/poisson1d.jl`) using three different strategies:
1) assemble to dense matrix, like shown above
2) assemble to sparse matrix of CSC format
3) assemble to sparse matrix of COO format

### Assembling to dense matrix

```bash
[ Info: Dense matrix:
2.298 s (30004 allocations: 6.71 GiB)
```
Dense matrix is not suitable for global (sparse) assembly due to it's massive
requirement of available memory.
### Assembling to the sparse matrix format CSC (naive)
```bash
[ Info: Sparse matrix (CSC format):
15.536 s (568673 allocations: 26.97 GiB)
```
`SparseMatrixCSC` is not suitable for (naive) assembly because the change of
sparsity pattern is very expensive.
### Assembling to the sparse matrix format COO
```bash
[ Info: Sparse matrix (COO format):
5.854 ms (73 allocations: 9.89 MiB)
```
`SparseMatrixCOO` is suitable sparse format for assembling global matrices, yet
it still have some shortcomings. In practice for solving linear system, COO format
needs to be converted to CSC format and it costs. Thus it would be benefical to do
first-time assembly in COO format, and after that store the sparsity pattern and
move to use direct assembly to CSC format.
[gitter-url]: https://gitter.im/JuliaFEM/JuliaFEM.jl
[docs-latest-img]: https://img.shields.io/badge/docs-latest-blue.svg
Expand Down
78 changes: 78 additions & 0 deletions examples/poisson1d.jl
@@ -0,0 +1,78 @@
# This file is a part of JuliaFEM.
# License is MIT: see https://github.com/JuliaFEM/FEMSparse.jl/blob/master/LICENSE

# Sparse assembly test, 1d poisson equation:
# $$u'' = 0$$,
# $$u(0) = 0$$,
# $$u(1) = 1$$,
# discretized to $$N$$ elements.

using Test, BenchmarkTools, LinearAlgebra, SparseArrays
using Revise
using FEMSparse

function fill_dense(N)
h = 1.0/N
Ke = h*[1.0 -1.0; -1.0 1.0]
K = zeros(N+1, N+1)
for i in 1:N
K[i:i+1,i:i+1] .+= Ke
end
return K
end

function fill_sparse_csc(N)
h = 1.0/N
Ke = h*[1.0 -1.0; -1.0 1.0]
K = spzeros(N+1, N+1)
for i in 1:N
K[i:i+1,i:i+1] += Ke
end
return K
end

function fill_sparse_coo(N)
h = 1.0/N
Ke = h*[1.0 -1.0; -1.0 1.0]
K = SparseMatrixCOO()
for i in 1:N
add!(K, i:i+1, i:i+1, Ke)
end
return SparseMatrixCSC(K)
end

function test_assembly(K)
N = size(K,1)-1
# set boundary conditions: u(0) = 0, u(1) = 1
u = zeros(N+1)
u[end] = 1.0
f = -K*u
K[1,:] .= 0.0
K[:,1] .= 0.0
K[1,1] = 1.0
f[end] = 0.0
K[end,:] .= 0.0
K[:,end] .= 0.0
K[end,end] = 1.0
f[end] = 1.0
u = K\f
u_acc = collect(range(0.0, stop=1.0, length=N+1))
return isapprox(u, u_acc; atol=1.0e-12)
end

@info("Warm-up")

@test test_assembly(fill_dense(10))
@test test_assembly(fill_sparse_csc(10))
@test test_assembly(fill_sparse_coo(10))

@info("Test")

@info("Dense matrix:")
@btime fill_dense(30_000)

@info("Sparse matrix (CSC format):")
@btime fill_sparse_csc(30_000)

@info("Sparse matrix (COO format):")
@btime fill_sparse_coo(30_000)

0 comments on commit 2962534

Please sign in to comment.