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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ disc = FEMDiscretization(
Ω, t; spaces = Dict(u => Q2, v => Q2, p => Q1),
pairing = Dict(eqs[3] => p), natural = [Γ_out], initializealg = CheckInit()
)
prob = DAEProblem(dfg, disc)
prob = discretize(dfg, disc)
abstol = [differential ? 1.0e-6 : 1.0e8 for differential in prob.differential_vars] # no error control on p
sol = solve(prob, DFBDF(nlsolve = NLNewton(new_W_dt_cutoff = 0), linsolve = UMFPACKFactorization()); abstol, reltol = 1.0e-6)
sol(tend, 0.6, 0.2; dv = v(t, x, y)) # the cross-stream velocity in the wake
Expand Down
2 changes: 1 addition & 1 deletion docs/src/assets/axisym_swirl.fnv1a
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e369978afda92609
7255430df70ab8ea
2 changes: 1 addition & 1 deletion docs/src/assets/axisym_swirl_animation.fnv1a
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3bb1f417d3ec6ef4
c2f9f849078215af
2 changes: 1 addition & 1 deletion docs/src/assets/ns_showcase.fnv1a
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a3eb010d9c375841
0d80e6fde5646fde
4 changes: 2 additions & 2 deletions docs/src/extension_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ accepts a `ModelingToolkit.PDESystem` on a `MeshDomain` and returns
when it has none, `FEMDiscretization(Ω)`, for a nonlinear solver such as `NewtonRaphson`.

Both carry the sparse analytic Jacobian and its prototype, so `solve(prob, alg)` needs nothing
else. `DAEProblem(pdesys, disc)` and `NonlinearProblem(pdesys, disc)` build the same problems
directly.
else. `NonlinearProblem(pdesys, disc)` and `DAEProblem(pdesys, disc)` are deprecated in favour of
`discretize`.

## Keywords

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 @@ -33,7 +33,7 @@ bcs = [
Q2, Q1 = Lagrange{RefQuadrilateral, 2}(), Lagrange{RefQuadrilateral, 1}()
disc = FEMDiscretization(Ω, t; spaces = Dict(u => Q2, v => Q2, p => Q1),
pairing = Dict(eqs[3] => p), natural = [Γout], initializealg = CheckInit())
prob = DAEProblem(dfg, disc)
prob = discretize(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(linsolve = UMFPACKFactorization());
abstol, reltol = 1e-7)
Expand Down
2 changes: 1 addition & 1 deletion examples/axisym_swirl/axisym_swirl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function axisym_problem(grid; tend, abstol = 1.0e-8)
Ω, t; spaces = Dict(ur => P2, ut => P2, uz => P2, p => P1), pairing = Dict(eqs[4] => p),
ibp = Dict(eqs[i] => Weighted(r) for i in 1:3), natural = [Γaxis], initializealg = CheckInit(), abstol
)
return DAEProblem(axisym, disc)
return discretize(axisym, disc)
end

"""
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 @@ -74,7 +74,7 @@ function dfg_problem(grid; tend = 30.0, ν_ = 1.0e-3, Umax_ = 0.3, ramp = 1 - ex
Ω, t; spaces = Dict(u => Q2, v => Q2, p => Q1), pairing = Dict(eqs[3] => p), natural = [Γout],
initializealg = CheckInit(), kwargs...
)
return DAEProblem(dfg, disc)
return discretize(dfg, disc)
end

"Absolute tolerances: `abstol` on the differential variables, none (1e8) on the algebraic ones."
Expand Down
6 changes: 3 additions & 3 deletions src/discretization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Finite-element discretization of a `PDESystem` whose spatial domain is the mesh
(of a `Ferrite.Grid`), with time variable `t`. `discretize(pdesys, disc)` assembles the
Galerkin operators with Ferrite and returns a `DAEProblem` with one array unknown per field.
Without `t` the discretization is steady: the `PDESystem` must have no time variable, and
`discretize` (or `NonlinearProblem(pdesys, disc)`) returns a `NonlinearProblem` whose `solve`
`discretize` returns a `NonlinearProblem` whose `solve`
gives a `PDENoTimeSolution`.

- `spaces`: dependent variable (`u` or `u(t, x, y)`) `=>` `Ferrite.Interpolation`; the
Expand Down Expand Up @@ -52,7 +52,7 @@ gives a `PDENoTimeSolution`.
pressure in `0 ~ Dx(u1) + Dy(u2)`; a nonlinear term in the field, as in `0 ~ v^2 - u`,
counts as present), and nothing otherwise; a positive number sets that
value on the algebraic rows of any system; `nothing` sets nothing. The differential rows
get the `abstol` passed to `FEMDiscretization` or `DAEProblem` if it is a number, else
get the `abstol` passed to `FEMDiscretization` or `discretize` if it is a number, else
OrdinaryDiffEq's default `1e-6`; an array `abstol` passed there is used as it is. An
`abstol` passed to `solve` replaces the problem's whole vector. The keyword belongs to the
time-dependent (DAE) path: a steady discretization ignores `:auto` and `nothing` and
Expand All @@ -75,7 +75,7 @@ gives a `PDENoTimeSolution`.
`sol(t, x, y)`, traces and `write_vtk`; only `sol.original_sol` and the array unknowns
hold the free dofs alone. `:algebraic` is recommended.
- `codegen = :mtk` is reserved and raises; remaining keywords are
passed to the `DAEProblem` (or `NonlinearProblem`), where a keyword given at the
passed to the `DAEProblem` (or the steady `NonlinearProblem`), where a keyword given at the
`discretize` call wins. `iv`, `unknowns`, `du0`, `coordinate`, `name` and `parameters` are set
by the discretization and rejected, as are `tspan`, `u0`, `p` and `differential_vars`, which
come from the `PDESystem` and the weak form.
Expand Down
16 changes: 14 additions & 2 deletions src/frontend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,18 +526,25 @@ function _abstol(algebraic, ops, selfnonlinear, abstol)
return (; abstol = [d ? differential : algebraic for d in ops.differential_vars])
end

SciMLBase.DAEProblem(pdesys::ModelingToolkit.PDESystem, disc::FEMDiscretization; kwargs...) =
function SciMLBase.DAEProblem(pdesys::ModelingToolkit.PDESystem, disc::FEMDiscretization; kwargs...)
Base.depwarn("`DAEProblem(pdesys, disc)` is deprecated, use `discretize(pdesys, disc)` instead", :DAEProblem)
return _daeproblem(pdesys, disc; kwargs...)
end

_daeproblem(pdesys, disc; kwargs...) =
disc.time === nothing ?
throw(
ArgumentError(
"the FEMDiscretization has no time variable, so it lowers to a NonlinearProblem, not a DAEProblem; " *
"call discretize(pdesys, disc) or NonlinearProblem(pdesys, disc)"
"call discretize(pdesys, disc)"
)
) : SciMLBase.discretize(pdesys, disc; kwargs...)

"""
NonlinearProblem(pdesys::PDESystem, disc::FEMDiscretization; kwargs...)

Deprecated: use `discretize(pdesys, disc; kwargs...)`, which returns the same problem.

Discretize the steady `pdesys` with a `disc` that has no time variable and return the
`NonlinearProblem` `K u + N(u) - F = 0` of the Galerkin operators, with Dirichlet rows
`u_d - g_d` as algebraic equations, the sparse analytic Jacobian `K + ∂N/∂u` and the
Expand All @@ -555,6 +562,11 @@ Keywords are passed to the `NonlinearProblem` after `disc.kwargs`; the keywords
`differential_vars` are rejected, as for the `DAEProblem`.
"""
function SciMLBase.NonlinearProblem(pdesys::ModelingToolkit.PDESystem, disc::FEMDiscretization; kwargs...)
Base.depwarn("`NonlinearProblem(pdesys, disc)` is deprecated, use `discretize(pdesys, disc)` instead", :NonlinearProblem)
return _nonlinearproblem(pdesys, disc; kwargs...)
end

function _nonlinearproblem(pdesys, disc; kwargs...)
disc.time === nothing || throw(
ArgumentError(
"the FEMDiscretization has the time variable $(disc.time), so it lowers to a DAEProblem; " *
Expand Down
1 change: 0 additions & 1 deletion test/Interface/rejections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ end
timed = [u(t, Γ) ~ sin(t) for Γ in (left, right, bottom, top)]
@test_throws "dirichlet = :eliminate needs Dirichlet data that are constant in time" lower([heat], [timed; ic]; dirichlet = :eliminate)
@named steady = PDESystem([Dx(Dx(u(x, y))) + Dy(Dy(u(x, y))) ~ 0], [u(Γ) ~ 0 for Γ in (left, right, bottom, top)], [(x, y) ∈ Ω], [x, y], [u(x, y)], [])
@test_throws "the FEMDiscretization has no time variable, so it lowers to a NonlinearProblem" DAEProblem(steady, FEMDiscretization(Ω))
@named transient = PDESystem([heat], [dirichlet; ic], [(x, y) ∈ Ω, t ∈ Interval(0.0, 1.0)], [t, x, y], [u(t, x, y)], [])
@test_throws "for a time-dependent problem write FEMDiscretization(Ω, t)" discretize(transient, FEMDiscretization(Ω))
@test_throws "`spaces` has keys that are not dependent variables of the system: v" lower(
Expand Down
2 changes: 0 additions & 2 deletions test/shared/frontend_tests/arguments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,12 @@ const DERIVED_VALUES = (tspan = (0.0, 2.0), u0 = zeros(9), p = [1.0], differenti
@testset "keyword `$k` is derived" for k in keys(DERIVED_MESSAGES)
kw = NamedTuple{(k,)}((DERIVED_VALUES[k],))
@test_throws ArgumentError(DERIVED_MESSAGES[k]) discretize(pdesys, disc; kw...)
@test_throws ArgumentError(DERIVED_MESSAGES[k]) SciMLBase.DAEProblem(pdesys, disc; kw...)
end

@testset "derived keywords in FEMDiscretization and NonlinearProblem" begin
@test_throws ArgumentError(DERIVED_MESSAGES.tspan) FEMDiscretization(Ω, t; tspan = (0.0, 2.0))
@test_throws ArgumentError(DERIVED_MESSAGES.p) FEMDiscretization(Ω; p = [1.0])
steady = poisson_system(Ω)
@test_throws ArgumentError(DERIVED_MESSAGES.u0) SciMLBase.NonlinearProblem(steady, FEMDiscretization(Ω); u0 = zeros(9))
@test_throws ArgumentError(DERIVED_MESSAGES.u0) discretize(steady, FEMDiscretization(Ω); u0 = zeros(9))
end

Expand Down
35 changes: 35 additions & 0 deletions test/shared/frontend_tests/end_to_end.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# solution. Each manufactured solution lies in the finite-element space and the quadrature is exact
# for its data, so the nodal error is at the level of the time-integration tolerance.
using Test
using Base.CoreLogging: with_logger, NullLogger
using FiniteElementMethod: FiniteElementMethod, rectangle_grid
using Ferrite: Triangle, Quadrilateral, Line

Expand Down Expand Up @@ -82,6 +83,40 @@ dirichlet_all(Ω) = [u(t, Γ) ~ (1 + x + 2y) * (1 + t) for Γ in pieces(Ω)]
@test sys isa ModelingToolkit.System
end

@testset "DAEProblem(pdesys, disc) is deprecated in favour of discretize" begin
Ω = MeshDomain(rectangle_grid(Triangle, (2, 2)))
@named pdesys = PDESystem([heat_eq], [dirichlet_all(Ω); ic], [(x, y) ∈ Ω, t ∈ Interval(0.0, 1.0)], [t, x, y], [u(t, x, y)], [])
disc = FEMDiscretization(Ω, t; saveat = 0.25)
@test_deprecated r"discretize" SciMLBase.DAEProblem(pdesys, disc; tstops = [0.5])
old = with_logger(NullLogger()) do
SciMLBase.DAEProblem(pdesys, disc; tstops = [0.5])
end
new = discretize(pdesys, disc; tstops = [0.5])
@test old isa SciMLBase.DAEProblem
@test old.u0 == new.u0 && old.du0 == new.du0 && old.tspan == new.tspan && old.kwargs == new.kwargs
@named steady = PDESystem([Dx(Dx(u(x, y))) + Dy(Dy(u(x, y))) ~ 0], [u(Γ) ~ 0 for Γ in boundarypiece.(Ω, (:left, :right, :bottom, :top))], [(x, y) ∈ Ω], [x, y], [u(x, y)], [])
with_logger(NullLogger()) do
@test_throws "the FEMDiscretization has no time variable, so it lowers to a NonlinearProblem" SciMLBase.DAEProblem(steady, FEMDiscretization(Ω))
end
end

@testset "NonlinearProblem(pdesys, disc) is deprecated in favour of discretize" begin
Ω = MeshDomain(rectangle_grid(Triangle, (2, 2)))
@named steady = PDESystem([Dx(Dx(u(x, y))) + Dy(Dy(u(x, y))) ~ 0], [u(Γ) ~ 0 for Γ in boundarypiece.(Ω, (:left, :right, :bottom, :top))], [(x, y) ∈ Ω], [x, y], [u(x, y)], [])
disc = FEMDiscretization(Ω; abstol = 1.0e-12)
@test_deprecated r"discretize" SciMLBase.NonlinearProblem(steady, disc)
old = with_logger(NullLogger()) do
SciMLBase.NonlinearProblem(steady, disc)
end
new = discretize(steady, disc)
@test old isa SciMLBase.NonlinearProblem
@test old.u0 == new.u0 && old.kwargs == new.kwargs
@named pdesys = PDESystem([heat_eq], [dirichlet_all(Ω); ic], [(x, y) ∈ Ω, t ∈ Interval(0.0, 1.0)], [t, x, y], [u(t, x, y)], [])
with_logger(NullLogger()) do
@test_throws "the FEMDiscretization has the time variable t, so it lowers to a DAEProblem" SciMLBase.NonlinearProblem(pdesys, FEMDiscretization(Ω, t))
end
end

@testset "PDESystem without parameters" begin
# Leaving out `ps` gives the system `NullParameters()`, which means no parameters.
Ω = MeshDomain(rectangle_grid(Triangle, (4, 4)))
Expand Down
8 changes: 0 additions & 8 deletions test/shared/frontend_tests/errors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,7 @@ dirichlet(Ω) = [u(t, Γ) ~ 0 for Γ in sides(Ω)]
@named steady = PDESystem(
[Dx(Dx(u(x, y))) + Dy(Dy(u(x, y))) ~ 0], [u(Γ) ~ 0 for Γ in sides(Ω)], [(x, y) ∈ Ω], [x, y], [u(x, y)], []
)
@test_throws ArgumentError(
"the FEMDiscretization has no time variable, so it lowers to a NonlinearProblem, not a DAEProblem; " *
"call discretize(pdesys, disc) or NonlinearProblem(pdesys, disc)"
) DAEProblem(steady, FEMDiscretization(Ω))
@named transient = PDESystem([heat], [dirichlet(Ω); ic], [(x, y) ∈ Ω, t ∈ Interval(0.0, 1.0)], [t, x, y], [u(t, x, y)], [])
@test_throws ArgumentError(
"the FEMDiscretization has the time variable t, so it lowers to a DAEProblem; " *
"for a steady problem write FEMDiscretization(Ω) without a time variable"
) SciMLBase.NonlinearProblem(transient, FEMDiscretization(Ω, t))
@test_throws ArgumentError(
"the PDESystem pairs t with an interval, but the FEMDiscretization has no time variable; " *
"for a time-dependent problem write FEMDiscretization(Ω, t)"
Expand Down
12 changes: 6 additions & 6 deletions test/shared/frontend_tests/nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ end
@testset "Jacobian against finite differences, Dirichlet rows" begin
Ω = square()
eq = Dt(U) + U * Dx(U) ~ Dx((1 + U^2) * Dx(U)) + Dy(Dy(U)) - U^2 + (1 + x + 2y)
prob = DAEProblem(pdesystem([eq], [dirichlet(Ω); ic], Ω), FEMDiscretization(Ω, t))
prob = discretize(pdesystem([eq], [dirichlet(Ω); ic], Ω), FEMDiscretization(Ω, t))
n = length(prob.u0)
rng_u = [0.3 + 0.1 * sin(3i) for i in 1:n]
rng_du = [0.2 * cos(5i) for i in 1:n]
Expand All @@ -141,7 +141,7 @@ end
# see Dual states; the time-dependent source exercises the load path too.
Ω = square()
eq = Dt(U) ~ Dx((1 + U^2) * Dx(U)) + Dy((1 + U^2) * Dy(U)) + (1 + x + 2y) - 10 * ustar * (1 + t)^2
prob = DAEProblem(pdesystem([eq], [dirichlet(Ω); ic], Ω), FEMDiscretization(Ω, t))
prob = discretize(pdesystem([eq], [dirichlet(Ω); ic], Ω), FEMDiscretization(Ω, t))
dofs = prob.problem_type.space.ch.prescribed_dofs
u0 = copy(prob.u0)
u0[dofs] .+= 0.5
Expand All @@ -154,7 +154,7 @@ end
# Every evaluation borrows its own copy of the nonlinear operator's buffers and values objects.
Ω = MeshDomain(rectangle_grid(Triangle, (16, 16)))
eq = Dt(U) ~ Dx((1 + U^2) * Dx(U)) + Dy((1 + U^2) * Dy(U)) + (1 + x + 2y) - 10 * ustar * (1 + t)^2
prob = DAEProblem(pdesystem([eq], [dirichlet(Ω); ic], Ω), FEMDiscretization(Ω, t))
prob = discretize(pdesystem([eq], [dirichlet(Ω); ic], Ω), FEMDiscretization(Ω, t))
probs = [remake(prob; u0 = s * prob.u0) for s in (1.0, 1.3, 0.7)]
final(q) = solve(q, EXACT_ROWS; abstol = 1.0e-8, reltol = 1.0e-8).original_sol.u[end]
serial = final.(probs)
Expand All @@ -167,7 +167,7 @@ end
@testset "AbstractFloat states and their Duals are evaluated, nested Duals are rejected" begin
Ω = square()
eq = Dt(U) ~ Dx((1 + U^2) * Dx(U)) + Dy(Dy(U)) + (1 + x + 2y) - 2 * ustar * (1 + t)^2
prob = DAEProblem(pdesystem([eq], [dirichlet(Ω); ic], Ω), FEMDiscretization(Ω, t))
prob = discretize(pdesystem([eq], [dirichlet(Ω); ic], Ω), FEMDiscretization(Ω, t))
n = length(prob.u0)
Dual = FiniteElementMethod.ForwardDiff.Dual
value = FiniteElementMethod.ForwardDiff.value
Expand Down Expand Up @@ -255,7 +255,7 @@ end
# The operators, the nonlinear one included, hold the values they were assembled with (spec §8.3).
Ω = square()
eq = Dt(U) ~ Dx((1 + k * U^2) * Dx(U)) + Dy(Dy(U)) + (1 + x + 2y) - 2k * ustar * (1 + t)^2
prob = DAEProblem(pdesystem([eq], [dirichlet(Ω); ic], Ω; ps = [k => 1.0]), FEMDiscretization(Ω, t))
prob = discretize(pdesystem([eq], [dirichlet(Ω); ic], Ω; ps = [k => 1.0]), FEMDiscretization(Ω, t))
sol = solve(prob, EXACT_ROWS; abstol = 1.0e-10, reltol = 1.0e-10)
@test nodal_error(sol, U, uexact) <= tol
@test SciMLBase.successful_retcode(solve(remake(prob; p = [k => 1.0]), EXACT_ROWS))
Expand All @@ -266,7 +266,7 @@ end

@testset verbose = true "still-rejected nonlinear classes" begin
Ω = square()
lower(eqs, bcs = [dirichlet(Ω); ic]) = DAEProblem(pdesystem(eqs, bcs, Ω), FEMDiscretization(Ω, t))
lower(eqs, bcs = [dirichlet(Ω); ic]) = discretize(pdesystem(eqs, bcs, Ω), FEMDiscretization(Ω, t))
# Raised by PDEBase's strong-form analysis with the messages of spec Appendix A.
@test_throws ErrorException("state-dependent mass in Differential(t, 1)(u(t, x, y))*(u(t, x, y)^2) needs residual-form DAE") lower(
[(1 + U^2) * Dt(U) ~ Dx(Dx(U)) + Dy(Dy(U))]
Expand Down
4 changes: 2 additions & 2 deletions test/shared/frontend_tests/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ end
# u0 is the full dof vector, so it lines up with the rows of M and K.
@test length(ops.u0) == size(ops.K, 1)
# Off the Dirichlet rows the operators are those of the DAEProblem.
prob = DAEProblem(pdesys, disc)
prob = discretize(pdesys, disc)
free = setdiff(1:size(ops.K, 1), ops.prescribed)
res = zeros(length(prob.u0))
du = randn(length(prob.u0))
Expand All @@ -66,7 +66,7 @@ end
@test ops.u0[facet_dofs(ops.metadata, left, u)] ≈ fill(2.0, 4)
@test ops.u0[facet_dofs(ops.metadata, right, u)] ≈ zeros(4)
# The eliminated problem drops exactly the prescribed dofs.
prob = DAEProblem(pdesys, disc)
prob = discretize(pdesys, disc)
@test length(prob.u0) == length(ops.u0) - length(ops.prescribed)
end

Expand Down
1 change: 0 additions & 1 deletion test/shared/frontend_tests/steady.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ end
prob = discretize(pdesys, FEMDiscretization(Ω; abstol = 1.0e-12))
@test prob isa SciMLBase.NonlinearProblem
@test prob.kwargs[:abstol] == 1.0e-12
@test SciMLBase.NonlinearProblem(pdesys, FEMDiscretization(Ω)).u0 == prob.u0
# The initial guess is zero except on the Dirichlet dofs, which hold the data.
space = prob.problem_type.space
pd = space.ch.prescribed_dofs
Expand Down
2 changes: 1 addition & 1 deletion test/shared/frontend_tests/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const Dt, Dx, Dy = Differential(t), Differential(x), Differential(y)
@named steady = PDESystem(
[Dx(Dx(u(x, y))) + Dy(Dy(u(x, y))) ~ 0], [u(Γ) ~ 0 for Γ in sides], [(x, y) ∈ Ω], [x, y], [u(x, y)], []
)
@test NonlinearProblem(steady, FEMDiscretization(Ω)).f.sys === nothing
@test discretize(steady, FEMDiscretization(Ω)).f.sys === nothing
@test_throws ArgumentError(
"a steady FEMDiscretization builds no symbolic System, so it has no symbolic_discretize; " *
"call discretize(pdesys, disc) for its NonlinearProblem"
Expand Down
Loading