Skip to content

refactor: directly solve initialization problem in linearization_function #2762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 12, 2024
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
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Expand Down Expand Up @@ -92,6 +92,7 @@ Libdl = "1"
LinearAlgebra = "1"
MLStyle = "0.4.17"
NaNMath = "0.3, 1"
NonlinearSolve = "3.12"
OrderedCollections = "1"
OrdinaryDiffEq = "6.82.0"
PrecompileTools = "1"
Expand Down Expand Up @@ -129,6 +130,7 @@ NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1"
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
Expand All @@ -142,4 +144,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"]
test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsBase", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET"]
2 changes: 1 addition & 1 deletion src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ using SciMLBase: StandardODEProblem, StandardNonlinearProblem, handle_varmap
using Distributed
import JuliaFormatter
using MLStyle
import OrdinaryDiffEq
using NonlinearSolve
using Reexport
using RecursiveArrayTools
import Graphs: SimpleDiGraph, add_edge!, incidence_matrix
Expand Down
22 changes: 13 additions & 9 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,7 @@
end

"""
lin_fun, simplified_sys = linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false, initialize = true, kwargs...)
lin_fun, simplified_sys = linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false, initialize = true, initialization_solver_alg = TrustRegion(), kwargs...)

Return a function that linearizes the system `sys`. The function [`linearize`](@ref) provides a higher-level and easier to use interface.

Expand All @@ -1820,6 +1820,7 @@
- `outputs`: A vector of variables that indicate the outputs of the linearized input-output model.
- `simplify`: Apply simplification in tearing.
- `initialize`: If true, a check is performed to ensure that the operating point is consistent (satisfies algebraic equations). If the op is not consistent, initialization is performed.
- `initialization_solver_alg`: A NonlinearSolve algorithm to use for solving for a feasible set of state and algebraic variables that satisfies the specified operating point.
- `kwargs`: Are passed on to `find_solvables!`

See also [`linearize`](@ref) which provides a higher-level interface.
Expand All @@ -1830,6 +1831,7 @@
op = Dict(),
p = DiffEqBase.NullParameters(),
zero_dummy_der = false,
initialization_solver_alg = TrustRegion(),
kwargs...)
inputs isa AbstractVector || (inputs = [inputs])
outputs isa AbstractVector || (outputs = [outputs])
Expand All @@ -1843,8 +1845,10 @@
op = merge(defs, op)
end
sys = ssys
initsys = complete(generate_initializesystem(
sys, guesses = guesses(sys), algebraic_only = true))
initsys = structural_simplify(
generate_initializesystem(
sys, guesses = guesses(sys), algebraic_only = true),
fully_determined = false)
if p isa SciMLBase.NullParameters
p = Dict()
else
Expand Down Expand Up @@ -1927,12 +1931,14 @@
sts = unknowns(sys),
get_initprob_u_p = get_initprob_u_p,
fun = ODEFunction{true, SciMLBase.FullSpecialize}(
sys, unknowns(sys), ps; initializeprobmap = initprobmap),
sys, unknowns(sys), ps),
initfn = initfn,
initprobmap = initprobmap,
h = build_explicit_observed_function(sys, outputs),
chunk = ForwardDiff.Chunk(input_idxs),
sys_ps = sys_ps,
initialize = initialize,
initialization_solver_alg = initialization_solver_alg,
sys = sys

function (u, p, t)
Expand All @@ -1953,10 +1959,8 @@
if norm(residual[alge_idxs]) > √(eps(eltype(residual)))
initu0, initp = get_initprob_u_p(u, p, t)
initprob = NonlinearLeastSquaresProblem(initfn, initu0, initp)
@set! fun.initializeprob = initprob
prob = ODEProblem(fun, u, (t, t + 1), p)
integ = init(prob, OrdinaryDiffEq.Rodas5P())
u = integ.u
nlsol = solve(initprob, initialization_solver_alg)
u = initprobmap(nlsol)

Check warning on line 1963 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L1963

Added line #L1963 was not covered by tests
end
end
uf = SciMLBase.UJacobianWrapper(fun, t, p)
Expand Down Expand Up @@ -2225,7 +2229,7 @@
u0, defs = get_u0(sys, x0, p)
if has_index_cache(sys) && get_index_cache(sys) !== nothing
if p isa SciMLBase.NullParameters
p = Dict()
p = op
elseif p isa Dict
p = merge(p, op)
elseif p isa Vector && eltype(p) <: Pair
Expand Down
28 changes: 18 additions & 10 deletions test/downstream/inversemodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,27 @@ sol = solve(prob, Rodas5P())
Sf, simplified_sys = Blocks.get_sensitivity_function(model, :y) # This should work without providing an operating opint containing a dummy derivative
x, _ = ModelingToolkit.get_u0_p(simplified_sys, op)
p = ModelingToolkit.MTKParameters(simplified_sys, op)
matrices1 = Sf(x, p, 0)
matrices2, _ = Blocks.get_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API
@test_broken matrices1.f_x ≈ matrices2.A[1:7, 1:7]
nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API
@test matrices2.A ≈ nsys.A
# If this somehow passes, mention it on
# https://github.com/SciML/ModelingToolkit.jl/issues/2786
@test_broken begin
matrices1 = Sf(x, p, 0)
matrices2, _ = Blocks.get_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API
@test matrices1.f_x ≈ matrices2.A[1:7, 1:7]
nsys = get_named_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API
@test matrices2.A ≈ nsys.A
end

# Test the same thing for comp sensitivities

Sf, simplified_sys = Blocks.get_comp_sensitivity_function(model, :y) # This should work without providing an operating opint containing a dummy derivative
x, _ = ModelingToolkit.get_u0_p(simplified_sys, op)
p = ModelingToolkit.MTKParameters(simplified_sys, op)
matrices1 = Sf(x, p, 0)
matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API
@test_broken matrices1.f_x ≈ matrices2.A[1:7, 1:7]
nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API
@test matrices2.A ≈ nsys.A
# If this somehow passes, mention it on
# https://github.com/SciML/ModelingToolkit.jl/issues/2786
@test_broken begin
matrices1 = Sf(x, p, 0)
matrices2, _ = Blocks.get_comp_sensitivity(model, :y; op) # Test that we get the same result when calling the higher-level API
@test matrices1.f_x ≈ matrices2.A[1:7, 1:7]
nsys = get_named_comp_sensitivity(model, :y; op) # Test that we get the same result when calling an even higher-level API
@test matrices2.A ≈ nsys.A
end
Loading