Skip to content

Commit 14a7239

Browse files
Merge pull request #3238 from AayushSabharwal/as/sde-observed-noise
fix: fix SDEs with noise dependent on observed variables
2 parents 6ee68bb + 585de35 commit 14a7239

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/structural_transformation/symbolics_tearing.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ function tearing_sub(expr, dict, s)
8888
s ? simplify(expr) : expr
8989
end
9090

91+
function tearing_substitute_expr(sys::AbstractSystem, expr; simplify = false)
92+
empty_substitutions(sys) && return expr
93+
substitutions = get_substitutions(sys)
94+
@unpack subs = substitutions
95+
solved = Dict(eq.lhs => eq.rhs for eq in subs)
96+
return tearing_sub(expr, solved, simplify)
97+
end
98+
9199
"""
92100
$(TYPEDSIGNATURES)
93101

src/systems/systems.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal
152152
noise_eqs = sorted_g_rows
153153
is_scalar_noise = false
154154
end
155+
156+
noise_eqs = StructuralTransformations.tearing_substitute_expr(ode_sys, noise_eqs)
155157
return SDESystem(full_equations(ode_sys), noise_eqs,
156158
get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys);
157159
name = nameof(ode_sys), is_scalar_noise)

test/sdesystem.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,22 @@ end
780780
prob = @test_nowarn SDEProblem(sys, nothing, (0.0, 1.0))
781781
@test_nowarn solve(prob, ImplicitEM())
782782
end
783+
784+
@testset "Issue#3212: Noise dependent on observed" begin
785+
sts = @variables begin
786+
x(t) = 1.0
787+
input(t)
788+
[input = true]
789+
end
790+
ps = @parameters a = 2
791+
@brownian η
792+
793+
eqs = [D(x) ~ -a * x + (input + 1) * η
794+
input ~ 0.0]
795+
796+
sys = System(eqs, t, sts, ps; name = :name)
797+
sys = structural_simplify(sys)
798+
@test ModelingToolkit.get_noiseeqs(sys) [1.0]
799+
prob = SDEProblem(sys, [], (0.0, 1.0), [])
800+
@test_nowarn solve(prob, RKMil())
801+
end

0 commit comments

Comments
 (0)