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

Structural simplify broken for SDEs #2614

Closed
TorkelE opened this issue Apr 4, 2024 · 3 comments
Closed

Structural simplify broken for SDEs #2614

TorkelE opened this issue Apr 4, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@TorkelE
Copy link
Member

TorkelE commented Apr 4, 2024

As the title says. This is required when e.g. combing and SDE with an algebraic equation. Example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

# Define some variables
@parameters p d 
@variables X(t)
eqs = [D(X) ~ p - d*X]
noise_eqs = [sqrt(p), sqrt(p - d*X)]

@named ssys = SDESystem(eqs, noise_eqs, t, [X], [p, d])
structural_simplify(ssys)

gives

ERROR: This should never happen. Trying to set DataType with @NamedTuple{substitutions::ModelingToolkit.Substitutions}.
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] #s300#199
    @ ~/.julia/dev/ModelingToolkit/src/systems/abstractsystem.jl:629 [inlined]
  [3] var"#s300#199"(::Any, obj::Any, patch::Any)
    @ ModelingToolkit ./none:0
  [4] (::Core.GeneratedFunctionStub)(::UInt64, ::LineNumberNode, ::Any, ::Vararg{Any})
    @ Core ./boot.jl:602
  [5] set
    @ ~/.julia/packages/Setfield/PdKfV/src/lens.jl:122 [inlined]
  [6] macro expansion
    @ ~/.julia/packages/Setfield/PdKfV/src/sugar.jl:197 [inlined]
  [7] tearing_reassemble(state::TearingState{…}, var_eq_matching::ModelingToolkit.BipartiteGraphs.Matching{…}, full_var_eq_matching::Nothing; simplify::Bool, mm::ModelingToolkit.SparseMatrixCLIL{…})
    @ ModelingToolkit.StructuralTransformations ~/.julia/dev/ModelingToolkit/src/structural_transformation/symbolics_tearing.jl:564
  [8] tearing_reassemble (repeats 2 times)
    @ ~/.julia/dev/ModelingToolkit/src/structural_transformation/symbolics_tearing.jl:220 [inlined]
  [9] #dummy_derivative#126
    @ ~/.julia/dev/ModelingToolkit/src/structural_transformation/symbolics_tearing.jl:648 [inlined]
 [10] _structural_simplify!(state::TearingState{…}, io::Nothing; simplify::Bool, check_consistency::Bool, fully_determined::Bool, warn_initialize_determined::Bool, dummy_derivative::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/systemstructure.jl:692
 [11] _structural_simplify!
    @ ~/.julia/dev/ModelingToolkit/src/systems/systemstructure.jl:672 [inlined]
 [12] #structural_simplify!#1069
    @ ~/.julia/dev/ModelingToolkit/src/systems/systemstructure.jl:665 [inlined]
 [13] __structural_simplify(sys::SDESystem, io::Nothing; simplify::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/systems.jl:76
 [14] __structural_simplify
    @ ~/.julia/dev/ModelingToolkit/src/systems/systems.jl:57 [inlined]
 [15] structural_simplify(sys::SDESystem, io::Nothing; simplify::Bool, split::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/systems.jl:22
 [16] structural_simplify
    @ ~/.julia/dev/ModelingToolkit/src/systems/systems.jl:19 [inlined]
 [17] structural_simplify(sys::SDESystem)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/systems.jl:19
 [18] top-level scope
    @ ~/Desktop/Julia Playground/Environment - Catalyst test/test_playground.jl:72
Some type information was truncated. Use `show(err)` to see complete types.

The corresponding code, but for making and ODE, works fine:

using ModelingToolkit, StochasticDiffEq
using ModelingToolkit: t_nounits as t, D_nounits as D

# Define some variables
@parameters p d 
@variables X(t)
eqs = [D(X) ~ p - d*X]
noise_eqs = [sqrt(p), sqrt(p - d*X)]

@named osys = ODESystem(eqs, t, [X], [p, d])
structural_simplify(osys)
@oameye
Copy link

oameye commented Oct 31, 2024

Having the same problem

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters begin
    α = 1.0 # parameters
    λ = 0.02
    ω = 1.0
    ω0 = 1.0
    γ = 0.005
end
@variables x(t) = 0.0
eqs = [D(D(x)) ~ -(ω0^2 - λ*cos(2*ω*t))*x - α*x^3 - γ*D(x)]

Ttr = 30_000.0
T = 100_000.0
tspan = (0.0, Ttr + T)
times = range((Ttr, Ttr + T)..., step=1)
@mtkbuild model = ODESystem(eqs, t; tspan=tspan)

@mtkbuild sdesystem = SDESystem(model, [0.0,0.0])

@oameye
Copy link

oameye commented Oct 31, 2024

Forgot, we had to use System:

@parameters begin
    α = 1.0 # parameters
    λ = 0.02
    ω = 1.0
    ω0 = 1.0
    γ = 0.005
end
@brownian β
@variables x(t) = 0.0 x_t(t) = 0.0
eqs = [ 
    D(x) ~ x_t + β,
    D(x_t) ~ -(ω0^2 - λ*cos(2*ω*t))*x - α*x^3 - γ*x_t + β,
]

Ttr = 30_000.0
T = 100_000.0
tspan = (0.0, Ttr + T)
times = range((Ttr, Ttr + T)..., step=1)
@mtkbuild model = System(eqs, t; tspan=tspan)

typeof(model)

@ChrisRackauckas
Copy link
Member

I believe this was fixed with the change to System, and pointed out in the other issue that is now closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants