Skip to content

Commit 8e1d0c9

Browse files
committed
Instead of modifying the equations, simply inject the definitions into the function body. That way the constants are not undefined.
1 parent c1ac583 commit 8e1d0c9

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,6 @@ function generate_function(sys::AbstractODESystem, dvs = states(sys), ps = param
127127
rhss = implicit_dae ? [_iszero(eq.lhs) ? eq.rhs : eq.rhs - eq.lhs for eq in eqs] :
128128
[eq.rhs for eq in eqs]
129129

130-
# Swap constants for their values
131-
cs = collect_constants(eqs)
132-
if !isempty(cs) > 0
133-
cmap = map(x -> x => getdefault(x), cs)
134-
rhss = map(x -> substitute(x, cmap), rhss)
135-
end
136-
137130
# TODO: add an optional check on the ordering of observed equations
138131
u = map(x -> time_varying_as_func(value(x), sys), dvs)
139132
p = map(x -> time_varying_as_func(value(x), sys), ps)

src/utils.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -559,18 +559,21 @@ function empty_substitutions(sys)
559559
end
560560

561561
function get_substitutions_and_solved_states(sys; no_postprocess = false)
562-
if empty_substitutions(sys)
562+
#Inject substitutions for constants => values
563+
cs = collect_constants([sys.eqs; sys.observed]) #ctrls? what else?
564+
# Swap constants for their values
565+
cmap = map(x -> x ~ getdefault(x), cs)
566+
567+
if empty_substitutions(sys) && isempty(cs)
563568
sol_states = Code.LazyState()
564569
pre = no_postprocess ? (ex -> ex) : get_postprocess_fbody(sys)
565-
else
566-
@unpack subs = get_substitutions(sys)
567-
# Swap constants for their values
568-
cs = collect_constants(subs)
569-
if !isempty(cs) > 0
570-
cmap = map(x -> x => getdefault(x), cs)
571-
subs = map(x -> x.lhs ~ substitute(x.rhs, cmap), subs)
570+
else # Have to do some work
571+
if !empty_substitutions(sys)
572+
@unpack subs = get_substitutions(sys)
573+
else
574+
subs = []
572575
end
573-
576+
subs = [cmap; subs] # The constants need to go first
574577
sol_states = Code.NameState(Dict(eq.lhs => Symbol(eq.lhs) for eq in subs))
575578
if no_postprocess
576579
pre = ex -> Let(Assignment[Assignment(eq.lhs, eq.rhs) for eq in subs], ex,

0 commit comments

Comments
 (0)