Skip to content
Merged
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ traces `u(t, Γ)` and normal derivatives `NormalDerivative(Γ)`, and the solutio
called in the same symbols. The [documentation](https://docs.sciml.ai/FiniteElementMethod/stable/)
covers how this differs from [MethodOfLines.jl](https://docs.sciml.ai/MethodOfLines/stable/).

A boundary with no condition gets the natural zero-flux condition, with a warning that lists it;
declare it with `FEMDiscretization(Ω, t; natural = [Γ_out])`, or make the warning an error with
`boundary_check = :error`. A boundary given as a lower-dimensional mesh becomes a piece with
`add_boundary_pieces!(grid, :inlet => inlet_mesh)`. See
[boundary conditions](https://docs.sciml.ai/FiniteElementMethod/stable/boundary_conditions/).

## Benchmarks

Accuracy per degree of freedom against MethodOfLines, assembly cost against hand-written Ferrite,
Expand Down
56 changes: 51 additions & 5 deletions docs/src/boundary_conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Restrictions of this release:
- Coordinates are Cartesian and named by the `(x, y) ∈ Ω` pairing. Time is the first or last argument of every dependent variable, never between spatial arguments.
- Dirichlet data may depend on space, time and parameters but not on another field. Neumann and Robin conditions are written with `NormalDerivative(Γ)`, with any conormal coefficient written explicitly. Neumann and Robin data may depend on time; the Robin coefficient multiplying `u(t, Γ)` may depend only on space and parameters until M3.
- Periodic conditions `u(t, Γa) ~ u(t, Γb)` are rejected until M4. Interface conditions between regions have no syntax; every field lives on the whole mesh.
- Boundary pieces must be disjoint. A piece with no condition gets the natural zero-flux condition, with a warning when the operator has a flux.
- Boundary pieces must be disjoint and lie on the boundary. Boundary facets with no condition for a field get the natural zero-flux condition, with a warning when that field's equation has a flux; `natural` declares them and `boundary_check` makes the warning an error or turns it off.
- Where two Dirichlet pieces of one field meet, the condition listed last in the `PDESystem` wins.
- Every field with a time derivative needs an initial condition, linear in that one field and finite at every node.
- Time-dependent mass or stiffness coefficients and `codegen = :mtk` are rejected until M3.
Expand All @@ -41,11 +41,30 @@ Restrictions of this release:
The spatial domain is a `MeshDomain(grid)` of a `Ferrite.Grid`, for example one read from Gmsh
with `FerriteGmsh.togrid` or made with `Ferrite.generate_grid`. Boundary pieces are the grid's
facet sets: `boundarypiece(Ω, :inlet)` is the facet set `"inlet"`. Pieces must be disjoint; a
facet in two pieces that both carry conditions is an error naming the first shared facet.
facet in two pieces that both carry conditions is an error naming the first shared facet, and a
piece containing an interior facet (one shared by two cells) is an error, since a piece lies on
the boundary.
Line, triangle and quadrilateral meshes are tested; tetrahedral and hexahedral meshes construct
but are not evidenced (see [Supported equation classes](@ref)). Every cell of the mesh must
have the same shape.

**Pieces from submeshes.** A boundary may also be given as a lower-dimensional mesh: the boundary
mesh that Gmsh gives for a physical group, or any `Ferrite.Grid` whose cells are facets of the
parent (line cells on a 2D mesh, surface cells on a 3D mesh). [`add_boundary_pieces!`](@ref)
matches each submesh cell to a facet of the parent by the coordinates of its nodes, within a
tolerance `atol` (the two meshes number their nodes independently), and adds the facets as the
facet set `name`:

```julia
grid = rectangle_grid(Triangle, (4, 4))
add_boundary_pieces!(grid, :inlet => inlet_mesh, :wall => wall_mesh)
Ω = MeshDomain(grid)
Γ_in = boundarypiece(Ω, :inlet)
```

A submesh cell that matches no facet of the parent, or only an interior facet, is an error that
names the cell and where it is. The grid's own facet sets work as before.

Coordinates are Cartesian. Their names and order come from the `(x, y) ∈ Ω` pairing (see
[Coordinate binding](@ref)). A curvilinear operator, such as a radial Laplacian, is written
out in Cartesian-coordinate form in the equation.
Expand Down Expand Up @@ -95,8 +114,8 @@ rescaled when it is proportional to the conormal derivative; a condition that is
`Dx(u(t, Γ)) ~ h` on a piece that is not perpendicular to the `x` axis, is an error asking for
the normal derivative.

A boundary piece with no condition gets the natural zero-flux condition. When the equations
have a flux, the discretization warns about each such piece.
Boundary facets with no condition get the natural zero-flux condition; see
[Boundary completeness](@ref).

## Robin conditions

Expand All @@ -119,6 +138,31 @@ Not supported. There is no syntax for a condition on an interior interface betwe
regions, and every field is defined on the whole mesh. Discontinuous coefficients across an
interface are not evidenced (see [Supported equation classes](@ref)).

## Boundary completeness

A boundary facet is covered for a field when it lies in a piece with a Dirichlet condition on
that field, or a Neumann or Robin condition in that field's equation. Where part of the boundary
is not covered for a field whose equation has a flux, the discretization treats it as the natural
zero-flux condition. That is often intended (an insulated wall, a symmetry plane, the outflow
`ν ∂ₙu = 0` of a channel flow), so it is not an error by default, but it is also what a forgotten
piece looks like. The check is therefore configurable on [`FEMDiscretization`](@ref):

- By default (`boundary_check = :warn`) `discretize` logs one warning per call, listing for
each such field the uncovered pieces and their number of facets (and the facets that belong
to no named piece).
- `natural = [Γ_out, :symmetry]` declares pieces as deliberately natural for every field; the
warning is then silent for them. An explicit zero Neumann condition `Dn(u(t, Γ)) ~ 0` also
covers its piece.
- `boundary_check = :error` throws the same message as an `ArgumentError`, for strict
completeness; `boundary_check = :off` skips the check.
- A field whose equation has no flux, such as a pressure, is not checked.

These are errors whatever `boundary_check` is: a facet in two pieces that both carry conditions
(which includes two Dirichlet values on one facet through overlapping pieces, and the same piece
and field listed twice), and a piece that is not on the boundary.

Detecting a singular pure-Neumann problem is not part of this check.

## Validation

Every condition is checked when the system is discretized, by `discretize(pdesys, disc)`, before a solver runs. An unsupported condition, operator or option
Expand All @@ -135,4 +179,6 @@ is an error that names the problem; nothing falls back to `mtkcompile`.
| A periodic condition `u(t, Γa) ~ u(t, Γb)` | error; lands in M4 |
| A time argument between spatial arguments, such as `u(x, t, y)` | error |
| Two Dirichlet pieces of one field sharing nodes | the condition listed last wins |
| A boundary piece with no condition | natural zero-flux condition, with a warning when the equations have a flux |
| Boundary facets with no condition for a field | natural zero-flux condition, with one warning per `discretize` call when that field's equation has a flux; `natural` declares them, `boundary_check = :error` or `:off` |
| A piece that is not on the boundary | error |
| A submesh cell that matches no facet, or an interior facet, of the parent | error from `add_boundary_pieces!` |
8 changes: 7 additions & 1 deletion docs/src/discretization.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Discretization

`FEMDiscretization`, `Galerkin` and `SUPG` are exported, as are the PDEBase names a model needs
`FEMDiscretization`, `Galerkin`, `SUPG` and `add_boundary_pieces!` are exported, as are the PDEBase names a model needs
(`MeshDomain`, `Region`, `boundarypiece`, `boundarypieces`, `NormalDerivative`) and Ferrite's
`Lagrange` and reference shapes. [Getting started](@ref) shows them in use.

Expand All @@ -15,6 +15,12 @@ FiniteElementMethod.assemble_sources!
FiniteElementMethod.operators
```

## Boundary pieces from submeshes

```@docs
add_boundary_pieces!
```

`discretize(pdesys, disc)` returns the `DAEProblem` of the discretization (the `NonlinearProblem` of a steady one, see [Steady problems](@ref)). `symbolic_discretize(pdesys, disc)`
returns `(sys, tspan)`, where `sys` is the array-residual `System` attached to that problem
(see [`FiniteElementMethod.fem_system`](@ref)): one block equation per field over the
Expand Down
2 changes: 1 addition & 1 deletion docs/src/navier_stokes.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bcs = [
[ν => 1e-3, Umax => 0.3])
Q2, Q1 = Lagrange{RefQuadrilateral, 2}(), Lagrange{RefQuadrilateral, 1}()
disc = FEMDiscretization(Ω, t; spaces = Dict(u => Q2, v => Q2, p => Q1),
pairing = Dict(eqs[3] => p), initializealg = CheckInit())
pairing = Dict(eqs[3] => p), natural = [Γout], initializealg = CheckInit())
prob = DAEProblem(dfg, disc)
abstol = [d ? 1e-7 : 1e8 for d in prob.differential_vars] # pressure and Dirichlet rows out of error control
sol = solve(prob, DFBDF(nlsolve = NLNewton(new_W_dt_cutoff = 0), linsolve = UMFPACKFactorization());
Expand Down
2 changes: 1 addition & 1 deletion examples/dfg_cylinder/dfg_cylinder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function dfg_problem(grid; tend = 30.0, ν_ = 1.0e-3, Umax_ = 0.3, ramp = 1 - ex
@named dfg = PDESystem(eqs, bcs, domains, [t, x, y], [U, V, P], [ν => ν_, Umax => Umax_])
Q2, Q1 = Lagrange{RefQuadrilateral, 2}(), Lagrange{RefQuadrilateral, 1}()
disc = FEMDiscretization(
Ω, t; spaces = Dict(u => Q2, v => Q2, p => Q1), pairing = Dict(eqs[3] => p),
Ω, t; spaces = Dict(u => Q2, v => Q2, p => Q1), pairing = Dict(eqs[3] => p), natural = [Γout],
initializealg = CheckInit(), kwargs...
)
return DAEProblem(dfg, disc)
Expand Down
4 changes: 2 additions & 2 deletions src/FiniteElementMethod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using DiffEqBase: BrownFullBasicInit
using DomainSets: DomainSets
using Ferrite: Ferrite, Lagrange, RefLine, RefTriangle, RefQuadrilateral, RefTetrahedron, RefHexahedron
using ForwardDiff: ForwardDiff
using LinearAlgebra: dot, mul!
using LinearAlgebra: dot, mul!, norm
using ModelingToolkit: ModelingToolkit, @parameters, @variables, Differential, MTKParameters,
ProblemTypeCtx, System, complete, getmetadata
using NonlinearSolveFirstOrder: NewtonRaphson
Expand Down Expand Up @@ -46,7 +46,7 @@ using PDEBase: StrongForm, MeshDomain, Region, boundarypiece, boundarypieces, No
include("discretization.jl")
include("frontend.jl")
_is_normal_derivative(op) = op isa NormalDerivative
export FEMDiscretization, Galerkin, SUPG
export FEMDiscretization, Galerkin, SUPG, add_boundary_pieces!
export MeshDomain, Region, boundarypiece, boundarypieces, NormalDerivative

include("precompile.jl")
Expand Down
24 changes: 20 additions & 4 deletions src/discretization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PDEBase.contains_point(grid::Ferrite.Grid, x) = _contains_point(grid, x)
"""
FEMDiscretization(Ω::MeshDomain, t = nothing; spaces = Dict(), regions = Dict(),
quadrature = nothing, test_space = Galerkin(), ibp = Dict(), pairing = Dict(),
codegen = :numeric, dirichlet = :algebraic, algebraic_abstol = :auto, kwargs...)
codegen = :numeric, dirichlet = :algebraic, algebraic_abstol = :auto, natural = (), boundary_check = :warn, kwargs...)

Finite-element discretization of a `PDESystem` whose spatial domain is the mesh domain `Ω`
(of a `Ferrite.Grid`), with time variable `t`. `discretize(pdesys, disc)` assembles the
Expand Down Expand Up @@ -35,6 +35,16 @@ gives a `PDENoTimeSolution`.
- `pairing`: equation `=>` dependent variable it is tested with, needed for an equation
with no time derivative of its own, such as `0 ~ Dxx(v) + f`. In a steady system the
equations not named here take the remaining dependent variables in declared order.
- `natural`: boundary pieces (as `boundarypiece(Ω, name)` or a facet set name) that deliberately carry
the natural zero-flux condition of every field, such as an outflow or a symmetry plane. A piece
with an explicit zero Neumann condition `Dn(u(t, Γ)) ~ 0` needs no entry here. See `boundary_check`.
- `boundary_check`: what to do when part of the boundary has no condition for a field whose
equation has a flux, neither a Dirichlet, Neumann or Robin condition nor a `natural` piece.
`:warn` (default) logs one warning per `discretize` call, listing the pieces and the number of
facets for each such field, and treats them as zero-flux; `:error` throws the same message as an
`ArgumentError`, for strict completeness; `:off` skips the check. A facet claimed by two pieces that both carry
conditions, and a piece that is not on the boundary, are errors whatever `boundary_check` is.
A boundary given as a lower-dimensional mesh is made a piece with [`add_boundary_pieces!`](@ref).
- `algebraic_abstol`: the absolute tolerance the `DAEProblem` carries for its algebraic rows
(those with `differential_vars` false: algebraic fields and Dirichlet rows), so that they
are left out of the solver's error control. `:auto` sets `1e8` when the system has index
Expand Down Expand Up @@ -97,6 +107,8 @@ struct FEMDiscretization{T} <: PDEBase.AbstractMeshDiscretization
pairing::Dict{Equation, Any}
codegen::Symbol
dirichlet::Symbol
natural::Vector{Symbol}
boundary_check::Symbol
algebraic_abstol::Union{Symbol, Nothing, Float64}
kwargs::NamedTuple
end
Expand Down Expand Up @@ -130,7 +142,7 @@ end
function FEMDiscretization(
domain::PDEBase.MeshDomain, time = nothing; spaces = Dict(), regions = Dict(), quadrature = nothing,
test_space = Galerkin(), ibp = Dict(), pairing = Dict(), codegen::Symbol = :numeric,
dirichlet::Symbol = :algebraic, algebraic_abstol = :auto, kwargs...
dirichlet::Symbol = :algebraic, algebraic_abstol = :auto, natural = (), boundary_check::Symbol = :warn, kwargs...
)
domain.mesh isa Ferrite.Grid ||
throw(ArgumentError("FEMDiscretization needs a MeshDomain of a Ferrite.Grid, got a mesh of type $(typeof(domain.mesh))"))
Expand All @@ -144,6 +156,10 @@ function FEMDiscretization(
region isa PDEBase.Region && region.parent === domain ||
throw(ArgumentError("the region of $(_field_op(dv)) must be a Region of the FEMDiscretization's MeshDomain, as Region(Ω, name); got $(repr(region))"))
end
boundary_check in (:warn, :error, :off) ||
throw(ArgumentError("boundary_check must be :warn, :error or :off, got $(repr(boundary_check))"))
natural = Symbol[p isa PDEBase.BoundaryPiece ? p.name : Symbol(p) for p in natural]
check_facetsets(domain.mesh, natural; disjoint = false)
algebraic_abstol === :auto || algebraic_abstol === nothing || algebraic_abstol isa Real && algebraic_abstol > 0 ||
throw(ArgumentError("algebraic_abstol must be :auto, nothing or a positive number, got $(repr(algebraic_abstol))"))
time === nothing && algebraic_abstol isa Real && throw(
Expand All @@ -156,7 +172,7 @@ function FEMDiscretization(
domain, time, Dict{Any, Ferrite.Interpolation}(_field_op(k) => ip for (k, ip) in spaces),
Dict{Any, Any}(_field_op(k) => r for (k, r) in regions),
quadrature, test_space, Dict{Any, StrongForm.IBPPolicy}(ibp), Dict{Equation, Any}(pairing),
codegen, dirichlet, algebraic_abstol isa Real ? Float64(algebraic_abstol) : algebraic_abstol, NamedTuple(kwargs)
codegen, dirichlet, unique(natural), boundary_check, algebraic_abstol isa Real ? Float64(algebraic_abstol) : algebraic_abstol, NamedTuple(kwargs)
)
end

Expand Down Expand Up @@ -188,7 +204,7 @@ PDEBase.supported_conditions(::FEMDiscretization) = PDEBase.SupportedConditions(
- Coordinates are Cartesian and named by the `(x, y) ∈ Ω` pairing. Time is the first or last argument of every dependent variable, never between spatial arguments.
- Dirichlet data may depend on space, time and parameters but not on another field. Neumann and Robin conditions are written with `NormalDerivative(Γ)`, with any conormal coefficient written explicitly. Neumann and Robin data may depend on time; the Robin coefficient multiplying `u(t, Γ)` may depend only on space and parameters until M3.
- Periodic conditions `u(t, Γa) ~ u(t, Γb)` are rejected until M4. Interface conditions between regions have no syntax; every field lives on the whole mesh.
- Boundary pieces must be disjoint. A piece with no condition gets the natural zero-flux condition, with a warning when the operator has a flux.
- Boundary pieces must be disjoint and lie on the boundary. Boundary facets with no condition for a field get the natural zero-flux condition, with a warning when that field's equation has a flux; `natural` declares them and `boundary_check` makes the warning an error or turns it off.
- Where two Dirichlet pieces of one field meet, the condition listed last in the `PDESystem` wins.
- Every field with a time derivative needs an initial condition, linear in that one field and finite at every node.
- Time-dependent mass or stiffness coefficients and `codegen = :mtk` are rejected until M3.
Expand Down
Loading
Loading