From add7059c8a52df7401aee9af3f7e6d8833b59db4 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Oct 2024 17:14:30 -0400 Subject: [PATCH 1/6] make Observable definition comply with new record_from_solution definition --- ext/MTKBifurcationKitExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/MTKBifurcationKitExt.jl b/ext/MTKBifurcationKitExt.jl index a0bd3bfc02..e5b57efc5a 100644 --- a/ext/MTKBifurcationKitExt.jl +++ b/ext/MTKBifurcationKitExt.jl @@ -59,7 +59,7 @@ struct ObservableRecordFromSolution{S, T} end end # Functor function that computes the value. -function (orfs::ObservableRecordFromSolution)(x, p) +function (orfs::ObservableRecordFromSolution)(x, p; k...) # Updates the state values (in subs_vals). for state_idx in 1:(orfs.state_end_idxs) orfs.subs_vals[state_idx] = orfs.subs_vals[state_idx][1] => x[state_idx] From eb681c10403031e3a1ee28c96f9d4b2defc3e389 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 28 Oct 2024 17:26:25 -0400 Subject: [PATCH 2/6] add test --- test/extensions/bifurcationkit.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/extensions/bifurcationkit.jl b/test/extensions/bifurcationkit.jl index b8c95dca99..629edf46a6 100644 --- a/test/extensions/bifurcationkit.jl +++ b/test/extensions/bifurcationkit.jl @@ -162,4 +162,12 @@ let bf = bifurcationdiagram(bp, PALC(), 2, opts_br) @test bf.γ.specialpoint[1].param≈0.1 atol=1e-4 rtol=1e-4 + + # Test with plot variable as observable + pvar = ModelingToolkit.get_var_to_name(fol)[:RHS] + bp = BifurcationProblem(fol, u0, par, bif_par; plot_var = pvar) + opts_br = ContinuationPar(p_min = -1.0, + p_max = 1.0) + bf = bifurcationdiagram(bp, PALC(), 2, opts_br) + @test bf.γ.specialpoint[1].param≈0.1 atol=1e-4 rtol=1e-4 end From 0a4fca7025004218018481818fc29c9e17808251 Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 13 Jan 2025 11:21:15 -0500 Subject: [PATCH 3/6] add error --- src/systems/diffeqs/sdesystem.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 366e8a6d09..95b5a4cc59 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -737,6 +737,7 @@ function DiffEqBase.SDEProblem{iip, specialize}( if !iscomplete(sys) error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEProblem`") end + f, u0, p = process_SciMLProblem( SDEFunction{iip, specialize}, sys, u0map, parammap; check_length, t = tspan === nothing ? nothing : tspan[1], kwargs...) @@ -767,6 +768,19 @@ function DiffEqBase.SDEProblem{iip, specialize}( noise_rate_prototype = noise_rate_prototype, kwargs...) end +function DiffEqBase.SDEProblem{iip, specialize}( + sys::ODESystem, u0map = [], tspan = get_tspan(sys), + parammap = DiffEqBase.NullParameters(); + sparsenoise = nothing, check_length = true, + callback = nothing, kwargs...) where {iip, specialize} + + if any(ModelingToolkit.isbrownian, unknowns(sys)) + error("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") + else + error("Cannot construct SDEProblem from an ODESystem.") + end +end + """ ```julia DiffEqBase.SDEProblem{iip}(sys::SDESystem, u0map, tspan, p = parammap; From 9eee7fd0c895612558b997be37e985f61e3f0f6c Mon Sep 17 00:00:00 2001 From: vyudu Date: Mon, 13 Jan 2025 11:26:56 -0500 Subject: [PATCH 4/6] up --- src/systems/diffeqs/sdesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 95b5a4cc59..26f26b4263 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -777,7 +777,7 @@ function DiffEqBase.SDEProblem{iip, specialize}( if any(ModelingToolkit.isbrownian, unknowns(sys)) error("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") else - error("Cannot construct SDEProblem from an ODESystem.") + error("Cannot construct SDEProblem from a normal ODESystem.") end end From 25f9ed84d0480298a8c9e60bff06922141bf661a Mon Sep 17 00:00:00 2001 From: vyudu Date: Fri, 17 Jan 2025 14:28:19 -0500 Subject: [PATCH 5/6] add test, fix --- src/systems/diffeqs/sdesystem.jl | 6 +----- test/sdesystem.jl | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/sdesystem.jl b/src/systems/diffeqs/sdesystem.jl index 26f26b4263..fb2eb1d804 100644 --- a/src/systems/diffeqs/sdesystem.jl +++ b/src/systems/diffeqs/sdesystem.jl @@ -768,11 +768,7 @@ function DiffEqBase.SDEProblem{iip, specialize}( noise_rate_prototype = noise_rate_prototype, kwargs...) end -function DiffEqBase.SDEProblem{iip, specialize}( - sys::ODESystem, u0map = [], tspan = get_tspan(sys), - parammap = DiffEqBase.NullParameters(); - sparsenoise = nothing, check_length = true, - callback = nothing, kwargs...) where {iip, specialize} +function DiffEqBase.SDEProblem(sys::ODESystem, args...; kwargs...) if any(ModelingToolkit.isbrownian, unknowns(sys)) error("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 8069581dcc..1a22a3501d 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -868,3 +868,24 @@ end @test length(ModelingToolkit.get_noiseeqs(sys)) == 1 @test length(observed(sys)) == 1 end +using ModelingToolkit, StochasticDiffEq +using ModelingToolkit: t_nounits as t, D_nounits as D + +@testset "Error when constructing SDESystem without `structural_simplify`" begin + @parameters σ ρ β + @variables x(t) y(t) z(t) + @brownian a + eqs = [D(x) ~ σ * (y - x) + 0.1a * x, + D(y) ~ x * (ρ - z) - y + 0.1a * y, + D(z) ~ x * y - β * z + 0.1a * z] + + @named de = System(eqs, t) + de = complete(de) + + u0map = [x => 1.0, y => 0.0, z => 0.0] + parammap = [σ => 10.0, β => 26.0, ρ => 2.33] + + @test_throws ErrorException("SDESystem constructed by defining Brownian variables with @brownian must be simplified by calling `structural_simplify` before a SDEProblem can be constructed.") SDEProblem(de, u0map, (0.0, 100.0), parammap) + de = structural_simplify(de) + @test SDEProblem(de, u0map, (0.0, 100.0), parammap) isa SDEProblem +end From ea95965838ae66eda3623d4e7a83dd511db5cc74 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sat, 18 Jan 2025 09:20:18 +0100 Subject: [PATCH 6/6] Update test/sdesystem.jl --- test/sdesystem.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/sdesystem.jl b/test/sdesystem.jl index 1a22a3501d..94f3e9cbfc 100644 --- a/test/sdesystem.jl +++ b/test/sdesystem.jl @@ -868,12 +868,10 @@ end @test length(ModelingToolkit.get_noiseeqs(sys)) == 1 @test length(observed(sys)) == 1 end -using ModelingToolkit, StochasticDiffEq -using ModelingToolkit: t_nounits as t, D_nounits as D @testset "Error when constructing SDESystem without `structural_simplify`" begin @parameters σ ρ β - @variables x(t) y(t) z(t) + @variables x(tt) y(tt) z(tt) @brownian a eqs = [D(x) ~ σ * (y - x) + 0.1a * x, D(y) ~ x * (ρ - z) - y + 0.1a * y,