Skip to content

fix(nonlinearsystem): Fix codegen issue for vector parameters #3382

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
Mar 3, 2025
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
12 changes: 6 additions & 6 deletions src/discretedomain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ $(TYPEDEF)
Represents a sample operator. A discrete-time signal is created by sampling a continuous-time signal.

# Constructors
`Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete)`
`Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete())`
`Sample(dt::Real)`

`Sample(x::Num)`, with a single argument, is shorthand for `Sample()(x)`.
Expand All @@ -106,7 +106,7 @@ julia> Δ = Sample(0.01)
"""
struct Sample <: Operator
clock::Any
Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete) = new(clock)
Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete()) = new(clock)
end

function Sample(arg::Real)
Expand Down Expand Up @@ -190,7 +190,7 @@ struct ShiftIndex
clock::Union{InferredTimeDomain, TimeDomain, IntegerSequence}
steps::Int
function ShiftIndex(
clock::Union{TimeDomain, InferredTimeDomain, IntegerSequence} = Inferred, steps::Int = 0)
clock::Union{TimeDomain, InferredTimeDomain, IntegerSequence} = Inferred(), steps::Int = 0)
new(clock, steps)
end
ShiftIndex(dt::Real, steps::Int = 0) = new(Clock(dt), steps)
Expand Down Expand Up @@ -232,7 +232,7 @@ function input_timedomain(s::Shift, arg = nothing)
if has_time_domain(arg)
return get_time_domain(arg)
end
InferredDiscrete
InferredDiscrete()
end

"""
Expand All @@ -244,7 +244,7 @@ function output_timedomain(s::Shift, arg = nothing)
if has_time_domain(t, arg)
return get_time_domain(t, arg)
end
InferredDiscrete
InferredDiscrete()
end

input_timedomain(::Sample, _ = nothing) = Continuous()
Expand All @@ -254,7 +254,7 @@ function input_timedomain(h::Hold, arg = nothing)
if has_time_domain(arg)
return get_time_domain(arg)
end
InferredDiscrete # the Hold accepts any discrete
InferredDiscrete() # the Hold accepts any discrete
end
output_timedomain(::Hold, _ = nothing) = Continuous()

Expand Down
6 changes: 6 additions & 0 deletions src/systems/nonlinear/nonlinearsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ function NonlinearSystem(eqs; kwargs...)
push!(new_ps, p)
end
else
if symbolic_type(p) == ArraySymbolic() &&
Symbolics.shape(unwrap(p)) != Symbolics.Unknown()
for i in eachindex(p)
delete!(new_ps, p[i])
end
end
push!(new_ps, p)
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/nonlinearsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using DiffEqBase, SparseArrays
using Test
using NonlinearSolve
using ForwardDiff
using SymbolicIndexingInterface
using ModelingToolkit: value
using ModelingToolkit: get_default_or_guess, MTKParameters

Expand Down Expand Up @@ -380,3 +381,12 @@ end
@test_throws ["single equation", "unknown"] IntervalNonlinearFunctionExpr(
sys, (0.0, 1.0))
end

@testset "Vector parameter used unscalarized and partially scalarized" begin
@variables x y
@parameters p[1:2] (f::Function)(..)

@mtkbuild sys = NonlinearSystem([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)])
@test !any(isequal(p[1]), parameters(sys))
@test is_parameter(sys, p)
end
9 changes: 9 additions & 0 deletions test/scc_nonlinear_problem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@
β = 1e-6
R0 = 1000
R = 9000
Ue(t) = 0.1 * sin(200 * π * t)

Check warning on line 96 in test/scc_nonlinear_problem.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Ue" should be "Use" or "Due".

function transamp(out, du, u, p, t)
g(x) = 1e-6 * (exp(x / 0.026) - 1)
y1, y2, y3, y4, y5, y6, y7, y8 = u
out[1] = -Ue(t) / R0 + y1 / R0 + C[1] * du[1] - C[1] * du[2]

Check warning on line 101 in test/scc_nonlinear_problem.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Ue" should be "Use" or "Due".
out[2] = -Ub / R + y2 * 2 / R - (α - 1) * g(y2 - y3) - C[1] * du[1] + C[1] * du[2]
out[3] = -g(y2 - y3) + y3 / R + C[2] * du[3]
out[4] = -Ub / R + y4 / R + α * g(y2 - y3) + C[3] * du[4] - C[3] * du[5]
Expand Down Expand Up @@ -282,3 +282,12 @@
sccprob = SCCNonlinearProblem(fullsys, u0, p)
@test isequal(parameters(fullsys), parameters(sccprob.f.sys))
end

@testset "Vector parameters in function arguments" begin
@variables x y
@parameters p[1:2] (f::Function)(..)

@mtkbuild sys = NonlinearSystem([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)])
prob = SCCNonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum])
@test_nowarn solve(prob, NewtonRaphson())
end
Loading