Skip to content

Commit 29c3a3a

Browse files
authored
Merge pull request #1832 from SciML/myb/obsfix
Fix observed function generation
2 parents 422c653 + e7d7513 commit 29c3a3a

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Di
2222
get_postprocess_fbody, vars!,
2323
IncrementalCycleTracker, add_edge_checked!, topological_sort,
2424
invalidate_cache!, Substitutions, get_or_construct_tearing_state,
25-
AliasGraph
25+
AliasGraph, filter_kwargs
2626

2727
using ModelingToolkit.BipartiteGraphs
2828
import .BipartiteGraphs: invview

src/structural_transformation/codegen.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ function build_observed_function(state, ts, var_eq_matching, var_sccs,
384384

385385
fullvars = state.fullvars
386386
s = state.structure
387-
graph = s.graph
388387
solver_states = fullvars[is_solver_state_idxs]
389388
algvars = fullvars[.!is_solver_state_idxs]
390389

@@ -525,6 +524,7 @@ function ODAEProblem{iip}(sys,
525524
has_difference = any(isdifferenceeq, eqs)
526525
cbs = process_events(sys; callback, has_difference, kwargs...)
527526

527+
kwargs = filter_kwargs(kwargs)
528528
if cbs === nothing
529529
ODEProblem{iip}(fun, u0, tspan, p; kwargs...)
530530
else

src/structural_transformation/symbolics_tearing.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
241241
#removed_vars = Int[]
242242
removed_obs = Int[]
243243
diff_to_var = invview(var_to_diff)
244+
dummy_sub = Dict()
244245
for var in 1:length(fullvars)
245246
dv = var_to_diff[var]
246247
dv === nothing && continue
@@ -253,7 +254,8 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
253254
push!(removed_obs, idx)
254255
end
255256
for eq in 𝑑neighbors(graph, dv)
256-
neweqs[eq] = substitute(neweqs[eq], fullvars[dv] => v_t)
257+
dummy_sub[dd] = v_t
258+
neweqs[eq] = substitute(neweqs[eq], dd => v_t)
257259
end
258260
fullvars[dv] = v_t
259261
# update the structural information
@@ -590,11 +592,17 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
590592
oldobs[idx] = (lhs ~ neweqs[eqidx].rhs)
591593
end
592594
deleteat!(oldobs, sort!(removed_obs))
593-
@set! sys.observed = [oldobs; subeqs]
594595
@set! sys.substitutions = Substitutions(subeqs, deps)
596+
597+
obs_sub = dummy_sub
598+
for eq in equations(sys)
599+
isdiffeq(eq) || continue
600+
obs_sub[eq.lhs] = eq.rhs
601+
end
602+
obs = substitute.([oldobs; subeqs], (obs_sub,))
603+
@set! sys.observed = obs
595604
@set! state.sys = sys
596605
@set! sys.tearing_state = state
597-
598606
return invalidate_cache!(sys)
599607
end
600608

src/systems/diffeqs/odesystem.jl

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -323,25 +323,10 @@ function build_explicit_observed_function(sys, ts;
323323
end
324324
ts = map(t -> substitute(t, subs), ts)
325325
obsexprs = []
326-
eqs_cache = Ref{Any}(nothing)
327326
for i in 1:maxidx
328327
eq = obs[i]
329328
lhs = eq.lhs
330329
rhs = eq.rhs
331-
vars!(vars, rhs)
332-
for v in vars
333-
isdifferential(v) || continue
334-
if eqs_cache[] === nothing
335-
eqs_cache[] = Dict(eq.lhs => eq.rhs for eq in equations(sys))
336-
end
337-
eqs_dict = eqs_cache[]
338-
rhs_diffeq = get(eqs_dict, v, nothing)
339-
push!(obsexprs, v rhs_diffeq)
340-
if rhs === nothing
341-
error("The observed variable $(eq.lhs) depends on the differentiated variable $v, but it's not explicit solved. Fix file an issue if you are sure that the system is valid.")
342-
end
343-
end
344-
empty!(vars)
345330
push!(obsexprs, lhs rhs)
346331
end
347332

0 commit comments

Comments
 (0)