-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sk/update basic examples and docstrings #96
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7bf8f80
Updated basic examples
SKopecz 4009c33
Edited doc strings for MPRK and SSPMPRK schemes
SKopecz 73bfa3a
Started tutorial for linear advection
SKopecz 6769b68
spell check
SKopecz 68615ad
documenter latex
SKopecz afc405f
extended tutorial
SKopecz a5f2b54
Update docs/make.jl
SKopecz c887c57
Update docs/src/linear_advection.md
SKopecz bd0bd0f
Update docs/src/linear_advection.md
SKopecz bf8b3be
Update docs/src/linear_advection.md
SKopecz a25f9b4
Updated Project.toml and improved tutorial for linear advection
SKopecz 18a4a0f
N=1000 in linear advection tutorial
SKopecz e4ab817
Update docs/src/linear_advection.md
SKopecz f3a90c9
Update docs/src/linear_advection.md
SKopecz 8cdb600
Update docs/src/linear_advection.md
SKopecz 4d845d7
Update docs/src/linear_advection.md
SKopecz c37dbba
Added plot labels
SKopecz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
[deps] | ||
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | ||
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" | ||
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" | ||
PositiveIntegrators = "d1b20bf0-b083-4985-a874-dc5121669aa5" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
|
||
[compat] | ||
BenchmarkTools = "1" | ||
Documenter = "1" | ||
OrdinaryDiffEq = "6" | ||
OrdinaryDiffEq = "6.59" | ||
Plots = "1" | ||
SparseArrays = "1.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Tutorial: Solution of the linear advection equation | ||
|
||
This tutorial is about the efficient solution of production-destruction systems (PDS) with a large number of differential equations. | ||
We will explore several ways to represent such large systems and assess their efficiency. | ||
|
||
## Definition of the production-destruction system | ||
|
||
One example of the occurrence of a PDS with a large number of equations is the space discretization of a partial differential equation. In this tutorial we want to solve the linear advection equation | ||
|
||
```math | ||
\partial_t u(t,x)=-a\partial_x u(t,x),\quad u(0,x)=u_0(x) | ||
``` | ||
|
||
with ``a>0``, ``t≥ 0``, ``x\in[0,1]`` and periodic boundary conditions. To keep things as simple as possible, we | ||
discretize the space domain as ``0=x_0<x_1\dots <x_{N-1}<x_N=1`` with ``x_i = i Δ x`` for ``i=0,\dots,N`` and ``Δx=1/N``. An upwind discretization of the spatial derivative yields the ODE system | ||
|
||
```math | ||
\begin{aligned} | ||
&\partial_t u_1(t) =-\frac{a}{Δx}\bigl(u_1(t)-u_{N}(t)\bigr),\\ | ||
&\partial_t u_i(t) =-\frac{a}{Δx}\bigl(u_i(t)-u_{i-1}(t)\bigr),\quad i=2,\dots,N, | ||
\end{aligned} | ||
``` | ||
|
||
where ``u_i(t)`` is an approximation of ``u(t,x_i)`` for ``i=1,\dots, N``. | ||
This system can also be written as ``\partial_t \mathbf u(t)=\mathbf A\mathbf u(t)`` with ``\mathbf u(t)=(u_1(t),\dots,u_N(t))`` and | ||
|
||
```math | ||
\mathbf A= \frac{a}{Δ x}\begin{bmatrix}-1&0&\dots&0&1\\1&-1&\ddots&&0\\0&\ddots&\ddots&\ddots&\vdots\\ \vdots&\ddots&\ddots&\ddots&0\\0&\dots&0&1&-1\end{bmatrix}. | ||
``` | ||
|
||
In particular the matrix ``\mathbf A`` shows that there is a single production term and a single destruction term per equation. | ||
Furthermore, the system is conservative as ``\mathbf A`` has column sum zero. | ||
To be precise, the production matrix ``\mathbf P = (p_{i,j})`` of this conservative PDS is given by | ||
|
||
```math | ||
\begin{aligned} | ||
&p_{1,N}(t,\mathbf u(t)) = \frac{a}{Δ x}u_N(t),\\ | ||
&p_{i,i-1}(t,\mathbf u(t)) = \frac{a}{Δ x}u_{i-1}(t),\quad i=2,\dots,N. | ||
\end{aligned} | ||
``` | ||
|
||
Since the PDS is conservative, we have ``d_{i,j}=p_{j,i}`` and the system is fully determined by the production matrix ``\mathbf P``. | ||
|
||
## Solution of the production-destruction system | ||
|
||
Now we are ready to define a `ConservativePDSProblem` and to solve this problem with a method of [PositiveIntegrators.jl](https://github.com/SKopecz/PositiveIntegrators.jl) or [OrdinaryDiffEq.jl](https://docs.sciml.ai/OrdinaryDiffEq/stable/). In the following we use ``a=1``, ``N=1000`` and the time domain ``t\in[0,1]``. Moreover, we choose the step function | ||
|
||
```math | ||
u_0(x)=\begin{cases}1, & 0.4 ≤ x ≤ 0.6,\\ 0,& \text{elsewhere}\end{cases} | ||
``` | ||
|
||
as initial condition. Due to the periodic boundary conditions and the transport velocity ``a=1``, the solution at time ``t=1`` is identical to the initial distribution, i.e. ``u(1,x) = u_0(x)``. | ||
|
||
```@example LinearAdvection | ||
N = 1000 # number of subintervals | ||
dx = 1/N # mesh width | ||
x = LinRange(dx, 1.0, N) # discretization points x_1,...,x_N = x_0 | ||
u0 = @. 0.0 + (0.4 ≤ x ≤ 0.6) * 1.0 # initial solution | ||
tspan = (0.0, 1.0) # time domain | ||
|
||
nothing #hide | ||
``` | ||
|
||
As mentioned above, we will try different approaches to solve this PDS and compare their efficiency. These are | ||
1. an in-place implementation with a dense matrix, | ||
2. an in-place implementation with a sparse matrix. | ||
|
||
### Standard in-place implementation | ||
|
||
```@example LinearAdvection | ||
using PositiveIntegrators # load ConservativePDSProblem | ||
|
||
function lin_adv_P!(P, u, p, t) | ||
P .= 0.0 | ||
N = length(u) | ||
dx = 1 / N | ||
P[1, N] = u[N] / dx | ||
for i in 2:N | ||
P[i, i - 1] = u[i - 1] / dx | ||
end | ||
return nothing | ||
end | ||
|
||
prob = ConservativePDSProblem(lin_adv_P!, u0, tspan) # create the PDS | ||
|
||
sol = solve(prob, MPRK43I(1.0, 0.5); save_everystep = false) | ||
|
||
nothing #hide | ||
``` | ||
|
||
```@example LinearAdvection | ||
using Plots | ||
|
||
plot(x, u0; label = "u0", xguide = "x", yguide = "u") | ||
plot!(x, last(sol.u); label = "u") | ||
``` | ||
|
||
### Using sparse matrices | ||
|
||
TODO: Some text | ||
|
||
```@example LinearAdvection | ||
using SparseArrays | ||
p_prototype = spdiagm(-1 => ones(eltype(u0), N - 1), | ||
N - 1 => ones(eltype(u0), 1)) | ||
prob_sparse = ConservativePDSProblem(lin_adv_P!, u0, tspan; p_prototype=p_prototype) | ||
|
||
sol_sparse = solve(prob_sparse, MPRK43I(1.0, 0.5); save_everystep = false) | ||
|
||
nothing #hide | ||
``` | ||
|
||
```@example LinearAdvection | ||
plot(x,u0; label = "u0", xguide = "x", yguide = "u") | ||
plot!(x, last(sol_sparse.u); label = "u") | ||
``` | ||
|
||
### Performance comparison | ||
|
||
Finally, we use [BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl) | ||
to compare the performance of the different implementations. | ||
|
||
```@example LinearAdvection | ||
using BenchmarkTools | ||
@benchmark solve(prob, MPRK43I(1.0, 0.5); save_everystep = false) | ||
``` | ||
|
||
```@example LinearAdvection | ||
@benchmark solve(prob_sparse, MPRK43I(1.0, 0.5); save_everystep = false) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This TODO note is still open?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we take care of this once the sparse matrices work as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough 👍