Skip to content

Commit 96e7a19

Browse files
committed
Merge branch 'main' of https://github.com/ModiaSim/ModiaBase.jl into development
2 parents a029e89 + 3de6816 commit 96e7a19

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/Symbolic.jl

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ Recursively converts der(x) to Symbol(:(der(x))) in expression `ex`
4848
* `ex`: Expression or array of expressions
4949
* `return `e`: ex with der(x) converted
5050
"""
51-
makeDerVar(ex) = ex
52-
function makeDerVar(ex::Expr)
51+
makeDerVar(ex, parameters) = if typeof(ex) in [Symbol, Expr] && ex in parameters; prepend(ex, :(_p)) else ex end
52+
53+
function makeDerVar(ex::Expr, parameters=[])
5354
if ex.head == :call && ex.args[1] == :der
5455
Symbol(ex)
56+
elseif isexpr(ex, :.) && ex in parameters
57+
prepend(ex, :(_p))
5558
elseif ex.head == :.
5659
Symbol(ex)
5760
else
58-
Expr(ex.head, [makeDerVar(arg) for arg in ex.args]...)
61+
Expr(ex.head, [makeDerVar(arg, parameters) for arg in ex.args]...)
5962
end
6063
end
6164

@@ -124,8 +127,18 @@ function findIncidence!(ex::Expr, incidence::Array{Incidence,1})
124127
end
125128
elseif ex.head == :.
126129
push!(incidence, ex)
130+
# if ex.args[2].value != :all
131+
# push!(incidence, ex.args[1])
132+
# end
133+
elseif ex.head == :generator
134+
vars = [v.args[1] for v in ex.args[2:end]]
135+
incid = Incidence[]
136+
[findIncidence!(e, incid) for e in ex.args]
137+
unique!(incid)
138+
setdiff!(incid, vars)
139+
push!(incidence, incid...)
127140
else
128-
# For example: =, vect, hcat, block
141+
# For example: =, vect, hcat, block, ref
129142
[findIncidence!(e, incidence) for e in ex.args]
130143
end
131144
nothing
@@ -227,13 +240,21 @@ function linearFactor(ex::Expr, x)
227240
rest = sub(LHS[1], RHS[1])
228241
factor = sub(LHS[2], RHS[2])
229242
(rest, factor, LHS[3] && RHS[3])
230-
elseif isexpr(ex, :vect)
243+
elseif isexpr(ex, :vect) || isexpr(ex, :vcat) || isexpr(ex, :hcat) || isexpr(ex, :row)
231244
arguments = ex.args[2:end]
232245
factored = [linearFactor(a, x) for a in arguments]
233246
linears = [f[3] for f in factored]
234247
(ex, 0, all(linears))
235248
else
236-
(ex, 0, false)
249+
# @warn "Unknown expression type" ex
250+
# dump(ex)
251+
incidence = Incidence[]
252+
findIncidence!(ex, incidence)
253+
if x in incidence
254+
(ex, 0, false)
255+
else
256+
(ex, 0, true)
257+
end
237258
end
238259
end
239260

0 commit comments

Comments
 (0)