Skip to content

Commit 11976ee

Browse files
authored
Merge pull request #1258 from SciML/s/test-hash-1
Try out new Symbolics
2 parents f6b1444 + 0b1f182 commit 11976ee

File tree

14 files changed

+85
-82
lines changed

14 files changed

+85
-82
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "6.4.9"
4+
version = "6.4.10"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -72,8 +72,8 @@ SciMLBase = "1.3"
7272
Setfield = "0.7"
7373
SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0"
7474
StaticArrays = "0.10, 0.11, 0.12, 1.0"
75-
SymbolicUtils = "0.13.4, 0.15"
76-
Symbolics = "~3.2.0"
75+
SymbolicUtils = "0.16"
76+
Symbolics = "3.3.0"
7777
UnPack = "0.1, 1.0"
7878
Unitful = "1.1"
7979
julia = "1.2"

src/systems/abstractsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ end
760760
function _config(expr, namespace)
761761
cn = Base.Fix2(_config, namespace)
762762
if Meta.isexpr(expr, :.)
763-
return :($getvar($(map(cn, expr.args)...); namespace=$namespace))
763+
return :($getproperty($(map(cn, expr.args)...); namespace=$namespace))
764764
elseif Meta.isexpr(expr, :function)
765765
def = splitdef(expr)
766766
def[:args] = map(cn, def[:args])

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ function DiffEqBase.ODEFunction{iip}(sys::AbstractODESystem, dvs = states(sys),
261261

262262
obs = observed(sys)
263263
observedfun = if steady_state
264-
isempty(obs) ? SciMLBase.DEFAULT_OBSERVED_NO_TIME : let sys = sys, dict = Dict()
264+
let sys = sys, dict = Dict()
265265
function generated_observed(obsvar, u, p, t=Inf)
266266
obs = get!(dict, value(obsvar)) do
267267
build_explicit_observed_function(sys, obsvar)
@@ -270,7 +270,7 @@ function DiffEqBase.ODEFunction{iip}(sys::AbstractODESystem, dvs = states(sys),
270270
end
271271
end
272272
else
273-
isempty(obs) ? SciMLBase.DEFAULT_OBSERVED : let sys = sys, dict = Dict()
273+
let sys = sys, dict = Dict()
274274
function generated_observed(obsvar, u, p, t)
275275
obs = get!(dict, value(obsvar)) do
276276
build_explicit_observed_function(sys, obsvar; checkbounds=checkbounds)
@@ -338,16 +338,7 @@ function DiffEqBase.DAEFunction{iip}(sys::AbstractODESystem, dvs = states(sys),
338338
# TODO: Jacobian sparsity / sparse Jacobian / dense Jacobian
339339

340340
#=
341-
observedfun = let sys = sys, dict = Dict()
342341
# TODO: We don't have enought information to reconstruct arbitrary state
343-
# in general from `(u, p, t)`, e.g. `a ~ D(x)`.
344-
function generated_observed(obsvar, u, p, t)
345-
obs = get!(dict, value(obsvar)) do
346-
build_explicit_observed_function(sys, obsvar)
347-
end
348-
obs(u, p, t)
349-
end
350-
end
351342
=#
352343

353344
DAEFunction{iip}(
@@ -394,23 +385,6 @@ function ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = states(sys),
394385
f_oop, f_iip = generate_function(sys, dvs, ps; expression=Val{true}, kwargs...)
395386

396387
dict = Dict()
397-
#=
398-
observedfun = if steady_state
399-
:(function generated_observed(obsvar, u, p, t=Inf)
400-
obs = get!($dict, value(obsvar)) do
401-
build_explicit_observed_function($sys, obsvar)
402-
end
403-
obs(u, p, t)
404-
end)
405-
else
406-
:(function generated_observed(obsvar, u, p, t)
407-
obs = get!($dict, value(obsvar)) do
408-
build_explicit_observed_function($sys, obsvar)
409-
end
410-
obs(u, p, t)
411-
end)
412-
end
413-
=#
414388

415389
fsym = gensym(:f)
416390
_f = :($fsym = $ODEFunctionClosure($f_oop, $f_iip))

src/systems/diffeqs/modelingtoolkitize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function modelingtoolkitize(prob::DiffEqBase.ODEProblem; kwargs...)
5151

5252
sts = vec(collect(vars))
5353

54-
params = if params isa Array && ndims(params) == 0
54+
params = if params isa Number || (params isa Array && ndims(params) == 0)
5555
[params[1]]
5656
else
5757
vec(collect(params))

src/systems/diffeqs/odesystem.jl

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,42 +225,52 @@ Build the observed function assuming the observed equations are all explicit,
225225
i.e. there are no cycles.
226226
"""
227227
function build_explicit_observed_function(
228-
sys, syms;
228+
sys, ts;
229229
expression=false,
230230
output_type=Array,
231231
checkbounds=true)
232232

233-
if (isscalar = !(syms isa Vector))
234-
syms = [syms]
233+
if (isscalar = !(ts isa AbstractVector))
234+
ts = [ts]
235235
end
236-
syms = value.(syms)
236+
ts = Symbolics.scalarize.(value.(ts))
237+
238+
vars = Set()
239+
syms = foreach(Base.Fix1(vars!, vars), ts)
240+
ivs = independent_variables(sys)
241+
dep_vars = collect(setdiff(vars, ivs))
237242

238243
obs = observed(sys)
244+
sts = Set(states(sys))
239245
observed_idx = Dict(map(x->x.lhs, obs) .=> 1:length(obs))
240-
output = similar(syms, Any)
241-
# FIXME: this is a rather rough estimate of dependencies.
246+
247+
# FIXME: This is a rather rough estimate of dependencies. We assume
248+
# the expression depends on everything before the `maxidx`.
242249
maxidx = 0
243-
for (i, s) in enumerate(syms)
250+
for (i, s) in enumerate(dep_vars)
244251
idx = get(observed_idx, s, nothing)
245-
idx === nothing && throw(ArgumentError("$s is not an observed variable."))
252+
if idx === nothing
253+
if !(s in sts)
254+
throw(ArgumentError("$s is either an observed nor a state variable."))
255+
end
256+
continue
257+
end
246258
idx > maxidx && (maxidx = idx)
247-
output[i] = obs[idx].rhs
248259
end
260+
obsexprs = map(eq -> eq.lhseq.rhs, obs[1:maxidx])
249261

250262
dvs = DestructuredArgs(states(sys), inbounds=!checkbounds)
251263
ps = DestructuredArgs(parameters(sys), inbounds=!checkbounds)
252-
ivs = independent_variables(sys)
253264
args = [dvs, ps, ivs...]
254265
pre = get_postprocess_fbody(sys)
255266

256267
ex = Func(
257268
args, [],
258269
pre(Let(
259-
map(eq -> eq.lhseq.rhs, obs[1:maxidx]),
260-
isscalar ? output[1] : MakeArray(output, output_type)
270+
obsexprs,
271+
isscalar ? ts[1] : MakeArray(ts, output_type)
261272
))
262273
) |> toexpr
263-
264274
expression ? ex : @RuntimeGeneratedFunction(ex)
265275
end
266276

src/systems/systemstructure.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,11 @@ function find_linear_equations(sys)
260260
return is_linear_equations, eadj, cadj
261261
end
262262

263-
function Base.show(io::IO, s::SystemStructure)
263+
function Base.show(io::IO, mime::MIME"text/plain", s::SystemStructure)
264264
@unpack graph = s
265265
S = incidence_matrix(graph, Num(Sym{Real}(:×)))
266266
print(io, "Incidence matrix:")
267-
show(io, S)
267+
show(io, mime, S)
268268
end
269269

270270
end # module

test/direct.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ simpexpr = [
4040
:($(+)($(*)(x, y), $(*)(-1, z, β)))
4141
]
4242

43+
σ, β, ρ = 2//3, 3//4, 4//5
44+
x, y, z = 6//7, 7//8, 8//9
4345
for i in 1:3
44-
@test ModelingToolkit.toexpr.(eqs)[i] == simpexpr[i]
45-
@test ModelingToolkit.toexpr.(eqs)[i] == simpexpr[i]
46+
@test eval(ModelingToolkit.toexpr.(eqs)[i]) == eval(simpexpr[i])
47+
@test eval(ModelingToolkit.toexpr.(eqs)[i]) == eval(simpexpr[i])
4648
end
4749

50+
@parameters t σ ρ β
51+
@variables x y z
4852
= ModelingToolkit.jacobian(eqs,[x,y,z])
4953
for i in 1:3
5054
= ModelingToolkit.gradient(eqs[i],[x,y,z])

test/latexify/10.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
\begin{align}
2-
\frac{dx(t)}{dt} =& \frac{\sigma \mathrm{\frac{d}{d t}}\left( x\left( t \right) - y\left( t \right) \right) \left( y\left( t \right) - x\left( t \right) \right)}{\frac{dz(t)}{dt}} \\
3-
0 =& - y\left( t \right) + \frac{1}{10} \sigma x\left( t \right) \left( \rho - z\left( t \right) \right) \\
2+
\frac{dx(t)}{dt} =& \frac{\sigma \left( - x\left( t \right) + y\left( t \right) \right) \mathrm{\frac{d}{d t}}\left( - y\left( t \right) + x\left( t \right) \right)}{\frac{dz(t)}{dt}} \\
3+
0 =& - y\left( t \right) + \frac{1}{10} \sigma \left( \rho - z\left( t \right) \right) x\left( t \right) \\
44
\frac{dz(t)}{dt} =& \left( y\left( t \right) \right)^{\frac{2}{3}} x\left( t \right) - \beta z\left( t \right)
55
\end{align}

test/latexify/20.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
\begin{align}
2-
\frac{du{_1}(t)}{dt} =& p{_3} \left( \mathrm{u{_2}}\left( t \right) - \mathrm{u{_1}}\left( t \right) \right) \\
3-
0 =& - \mathrm{u{_2}}\left( t \right) + \frac{1}{10} \mathrm{u{_1}}\left( t \right) p{_2} p{_3} \left( p{_1} - \mathrm{u{_1}}\left( t \right) \right) \\
2+
\frac{du{_1}(t)}{dt} =& \left( - \mathrm{u{_1}}\left( t \right) + \mathrm{u{_2}}\left( t \right) \right) p{_3} \\
3+
0 =& - \mathrm{u{_2}}\left( t \right) + \frac{1}{10} \left( - \mathrm{u{_1}}\left( t \right) + p{_1} \right) \mathrm{u{_1}}\left( t \right) p{_2} p{_3} \\
44
\frac{du{_3}(t)}{dt} =& \left( \mathrm{u{_2}}\left( t \right) \right)^{\frac{2}{3}} \mathrm{u{_1}}\left( t \right) - \mathrm{u{_3}}\left( t \right) p{_3}
55
\end{align}

test/latexify/30.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
\begin{align}
2-
\frac{du{_1}(t)}{dt} =& p{_3} \left( \mathrm{u{_2}}\left( t \right) - \mathrm{u{_1}}\left( t \right) \right) \\
3-
\frac{du{_2}(t)}{dt} =& - \mathrm{u{_2}}\left( t \right) + \frac{1}{10} \mathrm{u{_1}}\left( t \right) p{_2} p{_3} \left( p{_1} - \mathrm{u{_1}}\left( t \right) \right) \\
2+
\frac{du{_1}(t)}{dt} =& \left( - \mathrm{u{_1}}\left( t \right) + \mathrm{u{_2}}\left( t \right) \right) p{_3} \\
3+
\frac{du{_2}(t)}{dt} =& - \mathrm{u{_2}}\left( t \right) + \frac{1}{10} \left( - \mathrm{u{_1}}\left( t \right) + p{_1} \right) \mathrm{u{_1}}\left( t \right) p{_2} p{_3} \\
44
\frac{du{_3}(t)}{dt} =& \left( \mathrm{u{_2}}\left( t \right) \right)^{\frac{2}{3}} \mathrm{u{_1}}\left( t \right) - \mathrm{u{_3}}\left( t \right) p{_3}
55
\end{align}

0 commit comments

Comments
 (0)