From 0f04a5368edd0b8ab4d69263f320df105ab4bdc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Miclu=C8=9Ba-C=C3=A2mpeanu?= Date: Mon, 17 Feb 2025 12:00:36 +0000 Subject: [PATCH 1/4] test: add test for vector parameters in function args Co-authored-by: Aayush Sabharwal --- test/scc_nonlinear_problem.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 92030a84aa..620347403a 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -282,3 +282,12 @@ end 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 = NonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) + @test_nowarn solve(prob, NewtonRaphson()) +end From 69e5c80cb65b47fc8da415ca5cc006cb283effdb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Feb 2025 12:06:13 +0530 Subject: [PATCH 2/4] fix: fix vector parameter discovery when used both unscalarized and partially scalarized --- src/systems/nonlinear/nonlinearsystem.jl | 6 ++++++ test/nonlinearsystem.jl | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/systems/nonlinear/nonlinearsystem.jl b/src/systems/nonlinear/nonlinearsystem.jl index ad3fbfdeef..8b893b6cd2 100644 --- a/src/systems/nonlinear/nonlinearsystem.jl +++ b/src/systems/nonlinear/nonlinearsystem.jl @@ -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 diff --git a/test/nonlinearsystem.jl b/test/nonlinearsystem.jl index c6318b1314..c8dda530e2 100644 --- a/test/nonlinearsystem.jl +++ b/test/nonlinearsystem.jl @@ -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 @@ -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 From d64b2126b27f093f79f766637af53ca03e01aaad Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Feb 2025 12:06:39 +0530 Subject: [PATCH 3/4] test: use `SCCNonlinearProblem` in test --- test/scc_nonlinear_problem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scc_nonlinear_problem.jl b/test/scc_nonlinear_problem.jl index 620347403a..b2b326d090 100644 --- a/test/scc_nonlinear_problem.jl +++ b/test/scc_nonlinear_problem.jl @@ -288,6 +288,6 @@ end @parameters p[1:2] (f::Function)(..) @mtkbuild sys = NonlinearSystem([x^2 - p[1]^2 ~ 0, y^2 ~ f(p)]) - prob = NonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) + prob = SCCNonlinearProblem(sys, [x => 1.0, y => 1.0], [p => ones(2), f => sum]) @test_nowarn solve(prob, NewtonRaphson()) end From a0019928f14e7e8e80fca9f2c34700d2bf1c4244 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Fri, 28 Feb 2025 16:47:02 +0530 Subject: [PATCH 4/4] fix: fix transition to Moshi.jl ADT --- src/discretedomain.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/discretedomain.jl b/src/discretedomain.jl index c7f90007c5..67bef42c9e 100644 --- a/src/discretedomain.jl +++ b/src/discretedomain.jl @@ -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)`. @@ -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) @@ -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) @@ -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 """ @@ -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() @@ -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()