From c3e274fea0cd45f7f2bd3ff70f6f8785995993ac Mon Sep 17 00:00:00 2001 From: t Date: Thu, 5 Nov 2026 11:48:56 +0000 Subject: [PATCH] Export Weighted and document axisymmetric weighted integration by parts ibp = Dict(eq => Weighted(r)) needed using PDEBase.StrongForm: Weighted. Re-export it, allow it in the QA re-export check, document it in the Discretization page and test it on an axisymmetric heat equation with P2 elements. Agent-Harness: Claude Code 2.1.318 Agent-Model: claude-sonnet-5-5 Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c Agent-Review: unreviewed Co-authored-by: Claude Code --- docs/src/discretization.md | 40 +++++++++++++++++++++ src/FiniteElementMethod.jl | 3 ++ test/Frontend/weighted.jl | 2 ++ test/qa/qa.jl | 4 +-- test/shared/frontend_tests/weighted.jl | 49 ++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 test/Frontend/weighted.jl create mode 100644 test/shared/frontend_tests/weighted.jl diff --git a/docs/src/discretization.md b/docs/src/discretization.md index bea6f353..dc39f3ee 100644 --- a/docs/src/discretization.md +++ b/docs/src/discretization.md @@ -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 diff --git a/src/FiniteElementMethod.jl b/src/FiniteElementMethod.jl index 56affdf7..d8921880 100644 --- a/src/FiniteElementMethod.jl +++ b/src/FiniteElementMethod.jl @@ -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") diff --git a/test/Frontend/weighted.jl b/test/Frontend/weighted.jl new file mode 100644 index 00000000..9ab5f8c8 --- /dev/null +++ b/test/Frontend/weighted.jl @@ -0,0 +1,2 @@ +using FiniteElementMethod, Test +include(joinpath(@__DIR__, "..", "shared", "frontend_tests", "weighted.jl")) diff --git a/test/qa/qa.jl b/test/qa/qa.jl index 7f37253e..2227ee5c 100644 --- a/test/qa/qa.jl +++ b/test/qa/qa.jl @@ -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, ), ) diff --git a/test/shared/frontend_tests/weighted.jl b/test/shared/frontend_tests/weighted.jl new file mode 100644 index 00000000..35b358b2 --- /dev/null +++ b/test/shared/frontend_tests/weighted.jl @@ -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