Skip to content
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

Add better SDESystem error when it hasn't been structural_simplify #3316

Merged
merged 9 commits into from
Jan 18, 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
10 changes: 10 additions & 0 deletions src/systems/diffeqs/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -767,6 +768,15 @@ function DiffEqBase.SDEProblem{iip, specialize}(
noise_rate_prototype = noise_rate_prototype, kwargs...)
end

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.")
else
error("Cannot construct SDEProblem from a normal ODESystem.")
end
end

"""
```julia
DiffEqBase.SDEProblem{iip}(sys::SDESystem, u0map, tspan, p = parammap;
Expand Down
19 changes: 19 additions & 0 deletions test/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,22 @@ end
@test length(ModelingToolkit.get_noiseeqs(sys)) == 1
@test length(observed(sys)) == 1
end

@testset "Error when constructing SDESystem without `structural_simplify`" begin
@parameters σ ρ β
@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,
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
Loading