Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/src/discretization.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,46 @@ with them. Limits: linear terms only, `τ` and `b` must be numbers or expression
coordinates, time and `hK` (not parameters of the `PDESystem`), and boundary terms stay
Galerkin.

## Weighted and axisymmetric problems

`ibp = Dict(eq => Weighted(w))` (`Weighted` is exported from `PDEBase.StrongForm`) multiplies the equation `eq` by the
weight `w`, an expression in the coordinates, before it is integrated by parts; that is, `eq` is tested with `w v`
instead of `v`. In axisymmetric coordinates `(r, z)` the volume element is `r dr dz`, so `w = r` turns the radial
operator `(1/r) ∂ᵣ(r ∂ᵣu)` into the divergence `∇⋅(r ∇u)`. The weak form is then the symmetric `∫ r ∇u⋅∇v` with mass
`∫ r u v`, with no product-rule correction term; the default policy, `ProductRule()`, would split the `1/r` factor
and give a non-symmetric stiffness matrix. The weight kills the boundary term on the axis `r = 0`, so a field that
is regular there needs no condition on it: list the axis piece in `natural`. Other weights work the same way, such
as `Weighted(r^2)` for the spherical Laplacian. The continuity equation of axisymmetric flow is weighted by `r` in the same way, with `ibp = Dict(continuity => Weighted(r))`.

The axisymmetric heat equation `uₜ = (1/r) ∂ᵣ(r ∂ᵣu) + ∂ᶻᶻu + f` on the half-strip `(r, z) ∈ [0, 1] × [-1, 1]`, with
the source `f` chosen so that `exp(-t - r² - z²)` is the solution:

```julia
using FiniteElementMethod, ModelingToolkit, DomainSets, OrdinaryDiffEqBDF
using ModelingToolkit: t_nounits as t
using Symbolics: expand_derivatives, substitute
using Ferrite: Triangle

@variables r z u(..)
Dt, Dr, Dz = Differential(t), Differential(r), Differential(z)
exact = exp(-t - r^2 - z^2)
f = expand_derivatives(Dt(exact) - (Dr(r * Dr(exact)) / r + Dz(Dz(exact))))

Ω = MeshDomain(FiniteElementMethod.rectangle_grid(Triangle, (16, 16); lo = (0.0, -1.0), hi = (1.0, 1.0)))
axis, outer, bottom, top = boundarypiece.(Ω, (:left, :right, :bottom, :top))
U = u(t, r, z)
eq = Dt(U) ~ Dr(r * Dr(U)) / r + Dz(Dz(U)) + f
bcs = [[u(t, Γ) ~ exact for Γ in (outer, bottom, top)]; u(0, r, z) ~ substitute(exact, t => 0)]
@named pdesys = PDESystem([eq], bcs, [(r, z) ∈ Ω, t ∈ Interval(0.0, 0.5)], [t, r, z], [U], [])

disc = FEMDiscretization(
Ω, t; spaces = Dict(u => Lagrange{RefTriangle, 2}()), ibp = Dict(eq => Weighted(r)), natural = (axis,),
)
sol = solve(discretize(pdesys, disc), DFBDF())
```

`test/shared/frontend_tests/weighted.jl` runs this problem on three meshes and checks the P2 convergence rate.

## Curved meshes

The geometry of every cell comes from the mesh: the space, the assembly (facet normals
Expand Down
3 changes: 3 additions & 0 deletions src/FiniteElementMethod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ export FieldInfo, FEMMetadata, piece_nodes, normal_flux, write_vtk, facet_dofs,
export Lagrange, RefLine, RefTriangle, RefQuadrilateral, RefTetrahedron, RefHexahedron

using PDEBase: StrongForm, MeshDomain, Region, boundarypiece, boundarypieces, NormalDerivative
using PDEBase.StrongForm: Weighted
include("discretization.jl")
include("frontend.jl")
_is_normal_derivative(op) = op isa NormalDerivative
export FEMDiscretization, Galerkin, SUPG, add_boundary_pieces!
export MeshDomain, Region, boundarypiece, boundarypieces, NormalDerivative
# The integration-by-parts policy `ibp = Dict(eq => Weighted(w))` of `FEMDiscretization`.
export Weighted

include("precompile.jl")

Expand Down
2 changes: 2 additions & 0 deletions test/Frontend/weighted.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
using FiniteElementMethod, Test
include(joinpath(@__DIR__, "..", "shared", "frontend_tests", "weighted.jl"))
4 changes: 2 additions & 2 deletions test/qa/qa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ run_qa(
all_explicit_imports_via_owners = (; ignore = LTS_MTK_OWNER),
all_qualified_accesses_via_owners = (; ignore = LTS_MTK_OWNER),
),
# The user API of a FEM model: PDEBase's mesh vocabulary and Ferrite's interpolations.
# The user API of a FEM model: PDEBase's mesh vocabulary and weighted integration by parts, and Ferrite's interpolations.
reexports_allow = (
:MeshDomain, :Region, :boundarypiece, :boundarypieces, :NormalDerivative, :Lagrange,
:RefLine, :RefTriangle, :RefQuadrilateral, :RefTetrahedron, :RefHexahedron,
:RefLine, :RefTriangle, :RefQuadrilateral, :RefTetrahedron, :RefHexahedron, :Weighted,
),
)
49 changes: 49 additions & 0 deletions test/shared/frontend_tests/weighted.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# `Weighted` is exported: the r-weighted weak form of the axisymmetric heat equation u_t = (1/r)(r u_r)_r + u_zz
# in (r, z), with the weight r of the volume element r dr dz, converges at the P2 rate.
using Test
using FiniteElementMethod: FiniteElementMethod, rectangle_grid
using Ferrite: Triangle

include(joinpath(@__DIR__, "..", "frontend.jl"))

using ModelingToolkit: t_nounits as t
using Symbolics: Symbolics, expand_derivatives, substitute

@testset verbose = true "Weighted" begin
@testset "exported" begin
@test isdefined(FiniteElementMethod, :Weighted)
@test Base.isexported(FiniteElementMethod, :Weighted)
@test Weighted(2) isa FiniteElementMethod.PDEBase.StrongForm.IBPPolicy
end

@testset "axisymmetric heat equation, ibp = Dict(eq => Weighted(r))" begin
@variables r z u(..)
Dt, Dr, Dz = Differential(t), Differential(r), Differential(z)
# The solution is flat at the axis r = 0, where the weak form carries the natural condition.
exact = exp(-t) * exp(-r^2 - z^2)
source = expand_derivatives(Dt(exact) - (Dr(r * Dr(exact)) / r + Dz(Dz(exact))))
errors = map((4, 8, 16)) do n
grid = rectangle_grid(Triangle, (n, n); lo = (0.0, -1.0), hi = (1.0, 1.0))
Ω = MeshDomain(grid)
axis, outer, bottom, top = boundarypiece.(Ω, (:left, :right, :bottom, :top))
U = u(t, r, z)
eq = Dt(U) ~ Dr(r * Dr(U)) / r + Dz(Dz(U)) + source
bcs = [
[u(t, Γ) ~ exact for Γ in (outer, bottom, top)]
u(0, r, z) ~ substitute(exact, t => 0)
]
sol, _ = fem_solve(
[eq], bcs, Ω, (r, z), t, [U]; tspan = (0.0, 0.5),
spaces = Dict(u => Lagrange{RefTriangle, 2}()), ibp = Dict(eq => Weighted(r)), natural = (axis,),
solve_kwargs = (; abstol = 1.0e-11, reltol = 1.0e-11),
)
@test SciMLBase.successful_retcode(sol)
reference = [exp(-sol.t[end]) * exp(-ri^2 - zi^2) for (ri, zi) in zip(sol[r], sol[z])]
maximum(abs, sol[U][end, :] .- reference) / maximum(abs, reference)
end
rates = log2.(errors[1:(end - 1)] ./ errors[2:end])
@info "Weighted(r) axisymmetric heat" errors rates
@test errors[end] < 2.0e-4
@test all(>(2.7), rates)
end
end
Loading