Skip to content

Commit 6655276

Browse files
authored
add observed to var_to_name Dict (#1343)
1 parent cf73577 commit 6655276

File tree

12 files changed

+63
-1
lines changed

12 files changed

+63
-1
lines changed

src/systems/diffeqs/odesystem.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ function ODESystem(
141141
var_to_name = Dict()
142142
process_variables!(var_to_name, defaults, dvs′)
143143
process_variables!(var_to_name, defaults, ps′)
144+
isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed))
144145

145146
tgrad = RefValue(Vector{Num}(undef, 0))
146147
jac = RefValue{Any}(Matrix{Num}(undef, 0, 0))

src/systems/diffeqs/sdesystem.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs, iv, dvs, ps;
128128
var_to_name = Dict()
129129
process_variables!(var_to_name, defaults, dvs′)
130130
process_variables!(var_to_name, defaults, ps′)
131+
isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed))
131132

132133
tgrad = RefValue(Vector{Num}(undef, 0))
133134
jac = RefValue{Any}(Matrix{Num}(undef, 0, 0))

src/systems/discrete_system/discrete_system.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ function DiscreteSystem(
9898
var_to_name = Dict()
9999
process_variables!(var_to_name, defaults, dvs′)
100100
process_variables!(var_to_name, defaults, ps′)
101-
101+
isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed))
102+
102103
sysnames = nameof.(systems)
103104
if length(unique(sysnames)) != length(sysnames)
104105
throw(ArgumentError("System names must be unique."))

src/systems/jumps/jumpsystem.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ function JumpSystem(eqs, iv, states, ps;
101101
var_to_name = Dict()
102102
process_variables!(var_to_name, defaults, states)
103103
process_variables!(var_to_name, defaults, ps)
104+
isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed))
104105

105106
JumpSystem{typeof(ap)}(ap, value(iv), states, ps, var_to_name, observed, name, systems, defaults, connector_type, checks = checks)
106107
end

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ function NonlinearSystem(eqs, states, ps;
9898
var_to_name = Dict()
9999
process_variables!(var_to_name, defaults, states)
100100
process_variables!(var_to_name, defaults, ps)
101+
isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed))
101102

102103
NonlinearSystem(eqs, states, ps, var_to_name, observed, jac, name, systems, defaults, nothing, connector_type, checks = checks)
103104
end

src/systems/optimization/optimizationsystem.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function OptimizationSystem(op, states, ps;
7777
var_to_name = Dict()
7878
process_variables!(var_to_name, defaults, states)
7979
process_variables!(var_to_name, defaults, ps)
80+
isempty(observed) || collect_var_to_name!(var_to_name, (eq.lhs for eq in observed))
8081

8182
OptimizationSystem(
8283
value(op), states, ps, var_to_name,

test/discretesystem.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,12 @@ linearized_eqs = [
112112
y(t - 2.0) ~ y(t)
113113
]
114114
@test all(eqs2 .== linearized_eqs)
115+
116+
# observed variable handling
117+
@variables t x(t) RHS(t)
118+
@parameters τ
119+
@named fol = DiscreteSystem([D(x) ~ (1 - x)/τ]; observed=[RHS ~ (1 - x)/τ])
120+
@test isequal(RHS, @nonamespace fol.RHS)
121+
RHS2 = RHS
122+
@unpack RHS = fol
123+
@test isequal(RHS, RHS2)

test/jumpsystem.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,11 @@ function paffect!(integrator)
175175
end
176176
sol = solve(jprob, SSAStepper(), tstops=[1000.0], callback=DiscreteCallback(pcondit,paffect!))
177177
@test sol[1,end] == 100
178+
179+
# observed variable handling
180+
@variables OBS(t)
181+
@named js5 = JumpSystem([maj1,maj2], t, [S], [β,γ]; observed=[OBS ~ 2*S])
182+
OBS2 = OBS
183+
@test isequal(OBS2, @nonamespace js5.OBS)
184+
@unpack OBS = js5
185+
@test isequal(OBS2, OBS)

test/nonlinearsystem.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,12 @@ end
156156
@test isequal(union(Set(states(sys1)), Set(states(sys2))), Set(states(sys3)))
157157
@test isequal(union(Set(equations(sys1)), Set(equations(sys2))), Set(equations(sys3)))
158158
end
159+
160+
# observed variable handling
161+
@variables t x(t) RHS(t)
162+
@parameters τ
163+
@named fol = NonlinearSystem([0 ~ (1 - x)/τ], [x], [τ]; observed=[RHS ~ (1 - x)/τ])
164+
@test isequal(RHS, @nonamespace fol.RHS)
165+
RHS2 = RHS
166+
@unpack RHS = fol
167+
@test isequal(RHS, RHS2)

test/odesystem.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,14 @@ prob = ODEProblem(outersys, [sys.x=>1.0; collect(sys.ms).=>1:3], (0, 1.0))
504504
@variables t x(t)
505505
@named sys = ODESystem([D(x) ~ x/x], t)
506506
@test equations(alias_elimination(sys)) == [D(x) ~ 1]
507+
508+
# observed variable handling
509+
@variables t x(t) RHS(t)
510+
@parameters τ
511+
D = Differential(t)
512+
@named fol = ODESystem([D(x) ~ (1 - x)/τ]; observed=[RHS ~ (1 - x)/τ])
513+
@test isequal(RHS, @nonamespace fol.RHS)
514+
RHS2 = RHS
515+
@unpack RHS = fol
516+
@test isequal(RHS, RHS2)
517+

0 commit comments

Comments
 (0)