-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic setup to solve PDEs by collocation (#36)
* add basic setup to solve PDEs by collocation * format * remove commented code (not needed anymore) * fix inundation * fix docstring of system_matrix * use WendlandKernel for better stability * fix typo in docstring * differential operators and pdes only work with a RadialSymmetricKernel * remove unbounded type parameter * remove KernelInterpolation from docs/Project.toml
- Loading branch information
1 parent
d90d905
commit 31180e0
Showing
29 changed files
with
401 additions
and
56 deletions.
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 |
---|---|---|
|
@@ -4,13 +4,15 @@ authors = ["Joshua Lampert <[email protected]> and contributors"] | |
version = "1.0.0-DEV" | ||
|
||
[deps] | ||
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" | ||
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" | ||
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | ||
TypedPolynomials = "afbbf031-7a57-5f58-a1b9-b774a0fad08d" | ||
|
||
[compat] | ||
ForwardDiff = "0.10.36" | ||
LinearAlgebra = "1" | ||
RecipesBase = "1.1" | ||
SpecialFunctions = "2" | ||
|
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,5 @@ | ||
# Solving PDEs by collocation | ||
TODO: | ||
* Explain basic setup | ||
* Define custom differential operators and PDEs and solve them | ||
* AD vs analytic derivatives |
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,26 @@ | ||
using KernelInterpolation | ||
using Plots | ||
|
||
# right-hand-side of Poisson equation | ||
f(x) = 5 / 4 * pi^2 * sin(pi * x[1]) * cos(pi * x[2] / 2) | ||
pde = PoissonEquation(f) | ||
|
||
# analytical solution of equation | ||
u(x) = sin(pi * x[1]) * cos(pi * x[2] / 2) | ||
|
||
n = 10 | ||
nodeset_inner = homogeneous_hypercube(n, (0.1, 0.1), (0.9, 0.9); dim = 2) | ||
n_boundary = 3 | ||
nodeset_boundary = homogeneous_hypercube_boundary(n_boundary; dim = 2) | ||
values = f.(nodeset_inner) | ||
# Dirichlet boundary condition (here taken from analytical solution) | ||
g(x) = u(x) | ||
values_boundary = g.(nodeset_boundary) | ||
|
||
kernel = WendlandKernel{2}(3, shape_parameter = 0.3) | ||
itp = solve(pde, nodeset_inner, nodeset_boundary, values_boundary, kernel) | ||
|
||
many_nodes = homogeneous_hypercube(20; dim = 2) | ||
|
||
plot(many_nodes, itp) | ||
plot!(many_nodes, u) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,35 @@ | ||
abstract type AbstractDifferentialOperator end | ||
|
||
function (D::AbstractDifferentialOperator)(kernel::RadialSymmetricKernel, x, y) | ||
@assert length(x) == length(y) | ||
return save_call(D, kernel, x .- y) | ||
end | ||
|
||
# Workaround to avoid evaluating the derivative at zeros to allow automatic differentiation, | ||
# see https://github.com/JuliaDiff/ForwardDiff.jl/issues/303 | ||
# the same issue appears with Zygote.jl | ||
function save_call(D::AbstractDifferentialOperator, kernel::RadialSymmetricKernel, x) | ||
if all(iszero, x) | ||
x[1] = eps(typeof(x[1])) | ||
end | ||
# x .+= eps(typeof(x[1])) | ||
return D(kernel, x) | ||
end | ||
|
||
""" | ||
Laplacian() | ||
The Laplacian operator. It can be called with an `RadialSymmetricKernel` and points | ||
`x` and `y` to evaluate the Laplacian of the `kernel` at `x - y`. | ||
""" | ||
struct Laplacian <: AbstractDifferentialOperator | ||
end | ||
|
||
function Base.show(io::IO, ::Laplacian) | ||
print(io, "Δ") | ||
end | ||
|
||
function (::Laplacian)(kernel::RadialSymmetricKernel, x) | ||
H = ForwardDiff.hessian(x -> Phi(kernel, x), x) | ||
return tr(H) | ||
end |
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
Oops, something went wrong.