Skip to content

fix: infer oop form for SDEProblem/SDEFunction with StaticArrays #2834

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 2 commits into from
Jul 3, 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
42 changes: 36 additions & 6 deletions src/systems/diffeqs/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ function generate_diffusion_function(sys::SDESystem, dvs = unknowns(sys),
if isdde
eqs = delay_to_function(sys, eqs)
end
if eqs isa AbstractMatrix && isdiag(eqs)
eqs = diag(eqs)
end
Comment on lines +234 to +236
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisRackauckas @AayushSabharwal I don't think this is correct code for symbolics. The problem is that isdiag calls iszero which for a non-Num will create a symbolic boolean expression, i.e.

julia> @variables A
e1-element Vector{Num}:
 A

julia> eq = 5*A^2 + 2*A + 65
65 + 2A + 5(A^2)

julia> iszero(eq)
false

julia> iszero(ModelingToolkit.unwrap(eq))
(65 + 2A + 5(A^2)) == 0

So this now explicitly breaks creating an SDESystem with unwrapped expressions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, as @TorkelE mentioned Catalyst master tests should now generally pass, so if Catalyst tests fail when running downstream tests on an MTK PR that is meaningful again (unlike before we updated Catalyst to MTK 9).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Catalyst v14 release hasn't been made yet, so it's still not testing the updated code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that isdiag calls iszero which for a non-Num will create a symbolic boolean expression, i.e.

Thanks, we need to have a different isdiag that uses isequal. @AayushSabharwal can you prioritize this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isaacsas is there an issue open on this? If so, let's assign @AayushSabharwal to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #2868

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Catalyst v14 release hasn't been made yet, so it's still not testing the updated code?

Also, doesn't this:

https://github.com/SciML/ModelingToolkit.jl/actions/runs/9772401615/job/26976712250#step:6:579

indicate it was tested against Catalyst master?

u = map(x -> time_varying_as_func(value(x), sys), dvs)
p = if has_index_cache(sys) && get_index_cache(sys) !== nothing
reorder_parameters(get_index_cache(sys), ps)
Expand Down Expand Up @@ -403,14 +406,14 @@ function Girsanov_transform(sys::SDESystem, u; θ0 = 1.0)
checks = false)
end

function DiffEqBase.SDEFunction{iip}(sys::SDESystem, dvs = unknowns(sys),
function DiffEqBase.SDEFunction{iip, specialize}(sys::SDESystem, dvs = unknowns(sys),
ps = parameters(sys),
u0 = nothing;
version = nothing, tgrad = false, sparse = false,
jac = false, Wfact = false, eval_expression = false,
eval_module = @__MODULE__,
checkbounds = false,
kwargs...) where {iip}
kwargs...) where {iip, specialize}
if !iscomplete(sys)
error("A completed `SDESystem` is required. Call `complete` or `structural_simplify` on the system before creating an `SDEFunction`")
end
Expand Down Expand Up @@ -480,7 +483,7 @@ function DiffEqBase.SDEFunction{iip}(sys::SDESystem, dvs = unknowns(sys),

observedfun = ObservedFunctionCache(sys; eval_expression, eval_module)

SDEFunction{iip}(f, g,
SDEFunction{iip, specialize}(f, g,
sys = sys,
jac = _jac === nothing ? nothing : _jac,
tgrad = _tgrad === nothing ? nothing : _tgrad,
Expand All @@ -505,6 +508,16 @@ function DiffEqBase.SDEFunction(sys::SDESystem, args...; kwargs...)
SDEFunction{true}(sys, args...; kwargs...)
end

function DiffEqBase.SDEFunction{true}(sys::SDESystem, args...;
kwargs...)
SDEFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...)
end

function DiffEqBase.SDEFunction{false}(sys::SDESystem, args...;
kwargs...)
SDEFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...)
end

"""
```julia
DiffEqBase.SDEFunctionExpr{iip}(sys::AbstractODESystem, dvs = unknowns(sys),
Expand Down Expand Up @@ -583,14 +596,16 @@ function SDEFunctionExpr(sys::SDESystem, args...; kwargs...)
SDEFunctionExpr{true}(sys, args...; kwargs...)
end

function DiffEqBase.SDEProblem{iip}(sys::SDESystem, u0map = [], tspan = get_tspan(sys),
function DiffEqBase.SDEProblem{iip, specialize}(
sys::SDESystem, u0map = [], tspan = get_tspan(sys),
parammap = DiffEqBase.NullParameters();
sparsenoise = nothing, check_length = true,
callback = nothing, kwargs...) where {iip}
callback = nothing, kwargs...) where {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_DEProblem(SDEFunction{iip}, sys, u0map, parammap; check_length,
f, u0, p = process_DEProblem(
SDEFunction{iip, specialize}, sys, u0map, parammap; check_length,
kwargs...)
cbs = process_events(sys; callback, kwargs...)
sparsenoise === nothing && (sparsenoise = get(kwargs, :sparse, false))
Expand Down Expand Up @@ -628,6 +643,21 @@ function DiffEqBase.SDEProblem(sys::SDESystem, args...; kwargs...)
SDEProblem{true}(sys, args...; kwargs...)
end

function DiffEqBase.SDEProblem(sys::SDESystem,
u0map::StaticArray,
args...;
kwargs...)
SDEProblem{false, SciMLBase.FullSpecialize}(sys, u0map, args...; kwargs...)
end

function DiffEqBase.SDEProblem{true}(sys::SDESystem, args...; kwargs...)
SDEProblem{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...)
end

function DiffEqBase.SDEProblem{false}(sys::SDESystem, args...; kwargs...)
SDEProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...)
end

"""
```julia
DiffEqBase.SDEProblemExpr{iip}(sys::AbstractODESystem, u0map, tspan,
Expand Down
28 changes: 28 additions & 0 deletions test/sdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,31 @@ sys2 = complete(sys2)
prob = SDEProblem(sys1, sts .=> [1.0, 0.0, 0.0],
(0.0, 100.0), ps .=> (10.0, 26.0))
solve(prob, LambaEulerHeun(), seed = 1)

# SDEProblem construction with StaticArrays
# Issue#2814
@parameters p d
@variables x(tt)
@brownian a
eqs = [D(x) ~ p - d * x + a * sqrt(p)]
@mtkbuild sys = System(eqs, tt)
u0 = @SVector[x => 10.0]
tspan = (0.0, 10.0)
ps = @SVector[p => 5.0, d => 0.5]
sprob = SDEProblem(sys, u0, tspan, ps)
@test !isinplace(sprob)
@test !isinplace(sprob.f)
@test_nowarn solve(sprob, ImplicitEM())

# Ensure diagonal noise generates vector noise function
@variables y(tt)
@brownian b
eqs = [D(x) ~ p - d * x + a * sqrt(p)
D(y) ~ p - d * y + b * sqrt(d)]
@mtkbuild sys = System(eqs, tt)
u0 = @SVector[x => 10.0, y => 20.0]
tspan = (0.0, 10.0)
ps = @SVector[p => 5.0, d => 0.5]
sprob = SDEProblem(sys, u0, tspan, ps)
@test sprob.f.g(sprob.u0, sprob.p, sprob.tspan[1]) isa SVector{2, Float64}
@test_nowarn solve(sprob, ImplicitEM())
Loading