Skip to content

Commit 968185b

Browse files
committed
Update generate_function
1 parent 925bd85 commit 968185b

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,36 @@ function generate_function(
8787
has_difference=false,
8888
kwargs...
8989
)
90-
# optimization
91-
#obsvars = map(eq->eq.lhs, observed(sys))
92-
#fulldvs = [dvs; obsvars]
9390

9491
eqs = [eq for eq in equations(sys) if !isdifferenceeq(eq)]
9592
foreach(check_derivative_variables, eqs)
9693
# substitute x(t) by just x
9794
rhss = implicit_dae ? [_iszero(eq.lhs) ? eq.rhs : eq.rhs - eq.lhs for eq in eqs] :
9895
[eq.rhs for eq in eqs]
99-
#rhss = Let(obss, rhss)
10096

10197
# TODO: add an optional check on the ordering of observed equations
10298
u = map(x->time_varying_as_func(value(x), sys), dvs)
10399
p = map(x->time_varying_as_func(value(x), sys), ps)
104100
t = get_iv(sys)
105101

106-
pre = has_difference ? (ex -> ex) : get_postprocess_fbody(sys)
102+
if empty_substitutions(sys)
103+
bf_states = Code.LazyState()
104+
pre = has_difference ? (ex -> ex) : get_postprocess_fbody(sys)
105+
else
106+
subs = get_substitutions(sys)
107+
bf_states = Code.NameState(Dict(eq.lhs => Symbol(eq.lhs) for eq in subs))
108+
if has_difference
109+
pre = ex -> Let(Assignment[Assignment(eq.lhs, eq.rhs) for eq in subs], ex)
110+
else
111+
process = get_postprocess_fbody(sys)
112+
pre = ex -> Let(Assignment[Assignment(eq.lhs, eq.rhs) for eq in subs], process(ex))
113+
end
114+
end
107115

108116
if implicit_dae
109-
build_function(rhss, ddvs, u, p, t; postprocess_fbody=pre, kwargs...)
117+
build_function(rhss, ddvs, u, p, t; postprocess_fbody=pre, states=bf_states, kwargs...)
110118
else
111-
build_function(rhss, u, p, t; postprocess_fbody=pre, kwargs...)
119+
build_function(rhss, u, p, t; postprocess_fbody=pre, states=bf_states, kwargs...)
112120
end
113121
end
114122

0 commit comments

Comments
 (0)