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 src/FiniteElementMethod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using PDEBase: PDEBase
using PrecompileTools: PrecompileTools
using RuntimeGeneratedFunctions: RuntimeGeneratedFunctions
using SciMLBase: SciMLBase, DAEFunction, DAEProblem
using SciMLBase.SymbolicIndexingInterface: getp, variable_index
using SciMLBase.SymbolicIndexingInterface: SymbolicIndexingInterface, getp, parameter_index, variable_index
using SparseArrays: SparseMatrixCSC, nonzeros, nzrange, rowvals
using SymbolicUtils: SymbolicUtils
using Symbolics: Symbolics, Equation, Num, @register_array_symbolic, substitute, unwrap
Expand Down
27 changes: 25 additions & 2 deletions src/frontend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,26 +386,49 @@ function _lower(pdesys, disc::FEMDiscretization)
return (; ops, u0 = unknown_u0, tspan = (t0, t1), md, iv, unknowns, name = nameof(pdesys), parameters, selfnonlinear, raw, full_u0 = u0)
end

# The parameters of a steady problem as a symbolic container, so `remake(prob; p = [k => 3.0])` resolves `k` to its
# slot in `p` and reaches the same check as `remake(prob; p = [3.0])`, instead of leaving `p` unchanged.
struct SteadyParameters
syms::Vector{Any}
end

SymbolicIndexingInterface.parameter_symbols(sys::SteadyParameters) = sys.syms
SymbolicIndexingInterface.is_parameter(sys::SteadyParameters, sym) = parameter_index(sys, sym) !== nothing
SymbolicIndexingInterface.parameter_index(sys::SteadyParameters, sym) = findfirst(q -> isequal(q, sym), sys.syms)
SymbolicIndexingInterface.variable_symbols(::SteadyParameters) = []
SymbolicIndexingInterface.is_variable(::SteadyParameters, sym) = false
SymbolicIndexingInterface.variable_index(::SteadyParameters, sym) = nothing
SymbolicIndexingInterface.is_independent_variable(::SteadyParameters, sym) = false
SymbolicIndexingInterface.independent_variable_symbols(::SteadyParameters) = []
SymbolicIndexingInterface.is_time_dependent(::SteadyParameters) = false
SymbolicIndexingInterface.is_observed(::SteadyParameters, sym) = false
SymbolicIndexingInterface.constant_structure(::SteadyParameters) = true

# `K u + N(u, t0) - F` with the Dirichlet rows `u_d - g_d`: `K` holds `e_d`, `F` holds `g_d` and `N` is zero there.
function _steady_problem(parts; kwargs...)
(; ops) = parts
t0 = first(parts.tspan)
F = load!(zeros(length(parts.u0)), ops.load, t0)
N = ops.nonlinear
syms = [q for (q, _) in parts.parameters]
assembled = [v for (_, v) in parts.parameters]
check_parameters(p) = isempty(syms) || check_assembled_values(syms, assembled, p)
function f!(res, u, p)
check_parameters(p)
mul!(res, ops.K, u)
res .-= F
N === nothing || add_nonlinear!(res, N, u, t0)
return nothing
end
function jac!(J, u, p)
check_parameters(p)
check_pattern(J, ops.K, "the Jacobian")
copyto!(nonzeros(J), nonzeros(ops.K))
N === nothing || add_nonlinear_jacobian!(J, N, u, t0)
return nothing
end
f = SciMLBase.NonlinearFunction{true}(f!; jac = jac!, jac_prototype = copy(ops.K))
return SciMLBase.NonlinearProblem{true}(f, parts.u0, SciMLBase.NullParameters(), parts.md; kwargs...)
f = SciMLBase.NonlinearFunction{true}(f!; jac = jac!, jac_prototype = copy(ops.K), sys = isempty(syms) ? nothing : SteadyParameters(syms))
return SciMLBase.NonlinearProblem{true}(f, parts.u0, isempty(syms) ? SciMLBase.NullParameters() : copy(assembled), parts.md; kwargs...)
end

"""
Expand Down
19 changes: 12 additions & 7 deletions src/numeric_dae.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,22 @@ function assembled_parameter_check(sys, parameters)
get = getp(sys, syms)
return function (p)
current = get(p)
for (q, a, c) in zip(syms, assembled, current)
isequal(a, c) || throw(
ArgumentError(
"parameter $q was $a when the operators were assembled but is $c in this problem; operators are not reassembled for new parameter values, so build a new problem with discretize(pdesys, disc)"
)
)
end
check_assembled_values(syms, assembled, current)
return nothing
end
end

function check_assembled_values(syms, assembled, current)
for (q, a, c) in zip(syms, assembled, current)
isequal(a, c) || throw(
ArgumentError(
"parameter $q was $a when the operators were assembled but is $c in this problem; operators are not reassembled for new parameter values, so build a new problem with discretize(pdesys, disc)"
)
)
end
return nothing
end

default_initializealg() = BrownFullBasicInit(nlsolve = NewtonRaphson(autodiff = AutoForwardDiff()))

"""
Expand Down
2 changes: 2 additions & 0 deletions test/Frontend/remake_p.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
using FiniteElementMethod, Test
include(joinpath(@__DIR__, "..", "shared", "frontend_tests", "remake_p.jl"))
74 changes: 74 additions & 0 deletions test/shared/frontend_tests/remake_p.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# `remake(prob; p)` on a problem from `discretize`: the operators are assembled at the PDESystem's parameter
# values, so solving a problem with other values raises an ArgumentError naming the parameter, for the steady
# `NonlinearProblem` as for the transient `DAEProblem`; keeping `p` (or remaking `u0`) still solves.
using Test
using FiniteElementMethod: FiniteElementMethod, rectangle_grid
using Ferrite: Triangle
using SciMLBase: SciMLBase, remake
using NonlinearSolveFirstOrder: NewtonRaphson

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

using ModelingToolkit: t_nounits as t

@parameters D
@variables x y u(..)
const Dt, Dx, Dy = Differential(t), Differential(x), Differential(y)

square() = MeshDomain(rectangle_grid(Triangle, (3, 3)))
sides(Ω) = boundarypiece.(Ω, (:left, :right, :bottom, :top))

function steady_problem()
Ω = square()
eq = D * (Dx(Dx(u(x, y))) + Dy(Dy(u(x, y)))) ~ 6 * D
bcs = [u(Γ) ~ 1 + x^2 + 2y^2 for Γ in sides(Ω)]
@named pdesys = PDESystem([eq], bcs, [(x, y) ∈ Ω], [x, y], [u(x, y)], [D => 2.0])
return discretize(pdesys, FEMDiscretization(Ω))
end

function transient_problem()
Ω = square()
eq = Dt(u(t, x, y)) ~ D * (Dx(Dx(u(t, x, y))) + Dy(Dy(u(t, x, y))))
bcs = [[u(t, Γ) ~ 0 for Γ in sides(Ω)]; u(0, x, y) ~ x * (1 - x) * y * (1 - y)]
@named pdesys = PDESystem([eq], bcs, [(x, y) ∈ Ω, t ∈ Interval(0.0, 0.1)], [t, x, y], [u(t, x, y)], [D => 2.0])
return discretize(pdesys, FEMDiscretization(Ω, t))
end

@testset verbose = true "remake(prob; p) is rejected, not ignored" for (kind, make) in
(("steady", steady_problem), ("transient", transient_problem))
prob = make()
alg = kind == "steady" ? NewtonRaphson() : EXACT_ROWS
@test SciMLBase.successful_retcode(solve(prob, alg))
@testset "the same p still solves" begin
@test SciMLBase.successful_retcode(solve(remake(prob; p = [D => 2.0]), alg))
@test SciMLBase.successful_retcode(solve(remake(prob; u0 = copy(prob.u0)), alg))
end
@testset "a numeric p vector" begin
err = try
solve(remake(prob; p = [3.0]), alg)
nothing
catch e
e
end
@test err isa ArgumentError
@test occursin("parameter D", sprint(showerror, err))
@test occursin("discretize", sprint(showerror, err))
end
@testset "a symbolic p pair" begin
err = try
solve(remake(prob; p = [D => 3.0]), alg)
nothing
catch e
e
end
@test err isa ArgumentError
@test occursin("parameter D", sprint(showerror, err))
end
@testset "editing prob.p in place is rejected too" begin
if kind == "steady"
prob = make()
prob.p[1] = 3.0
@test_throws ArgumentError solve(prob, alg)
end
end
end
Loading