Skip to content

Commit 22f7381

Browse files
Merge pull request #73 from shashi/s/mtk
fixes for new MTK
2 parents b020136 + 397a8d5 commit 22f7381

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1515
DataStructures = "0.17, 0.18"
1616
DiffEqBase = "6.5"
1717
Latexify = "0.11, 0.12, 0.13, 0.14"
18-
ModelingToolkit = "1.3, 2.0, 3.0"
18+
ModelingToolkit = "4.0"
1919
Reexport = "0.2"
2020
julia = "1.3"
2121

src/ParameterizedFunctions.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module ParameterizedFunctions
55
using DataStructures, DiffEqBase, Latexify
66
using Reexport
77
@reexport using ModelingToolkit
8+
using ModelingToolkit: Sym, FnType, tosymbol
9+
810

911
import LinearAlgebra
1012

src/ode_def_opts.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
findreplace(ex::Symbol, dict) = get(dict, ex, ex)
2+
function findreplace(ex::Expr, dict)
3+
Expr(ex.head, map(x->findreplace(x, dict), ex.args)...)
4+
end
5+
findreplace(ex, dict) = ex
6+
17
function ode_def_opts(name::Symbol,opts::Dict{Symbol,Bool},curmod,ex::Expr,params...;depvar=:t)
28
# depvar is the dependent variable. Defaults to t
39
# M is the mass matrix in RosW, must be a constant!
@@ -14,15 +20,20 @@ function ode_def_opts(name::Symbol,opts::Dict{Symbol,Bool},curmod,ex::Expr,param
1420
####
1521

1622

17-
t = ModelingToolkit.Variable(:t)()
18-
vars = [ModelingToolkit.Variable(x)(t) for x in syms]
19-
params = [ModelingToolkit.Variable(x)() for x in Symbol[params...]]
23+
t = Sym{Number}(:t)
24+
vars = [Sym{FnType{Tuple, Number}}(x)(t) for x in syms]
25+
params = [Sym{Number}(x) for x in Symbol[params...]]
26+
27+
vars_dict = Dict(x=>Symbol(v) for (x, v) in zip(syms, vars))
28+
29+
# replace x with x(t) if it's a var
30+
ex = findreplace(ex, vars_dict)
2031

2132
# Build the Expressions
2233

2334
# Run find replace to make the function expression
2435
symex = copy(ex) # Different expression for symbolic computations
25-
ode_findreplace(ex,symex,indvar_dict,params)
36+
#ode_findreplace(ex,symex,indvar_dict,params)
2637
funcs = build_component_funcs(symex)
2738
mtk_ops = modelingtoolkitize_expr.(funcs,([t;vars;params],),(curmod,))
2839

src/utils.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,25 @@ function build_component_funcs(symex)
1818
end
1919

2020
function modelingtoolkitize_expr(ex::Expr,vars,curmod)
21-
names = [x.op.name for x in vars]
21+
names = [tosymbol(x) for x in vars]
2222
ex.head === :if && (ex = Expr(:call, ifelse, ex.args...))
2323
ex.head === :call || throw(ArgumentError("internal representation does not support non-call Expr"))
24-
op = ex.args[1] names ? vars[findfirst(x->ex.args[1] == x.op.name,vars)] : getproperty(curmod,ex.args[1]) # HACK
25-
args = Expression[modelingtoolkitize_expr(x,vars,curmod) for x in ex.args[2:end]]
26-
return Operation(op, args)
24+
op = ex.args[1] names ? vars[findfirst(x->ex.args[1] == tosymbol(x),vars)] : getproperty(curmod,ex.args[1]) # HACK
25+
args = [modelingtoolkitize_expr(x,vars,curmod) for x in ex.args[2:end]]
26+
return Term(op, args)
2727
end
2828

29-
function modelingtoolkitize_expr(ex::Variable,vars,curmod)
30-
convert(Operation,ex)
29+
function modelingtoolkitize_expr(ex::Sym,vars,curmod)
30+
ex
3131
end
3232

3333
function modelingtoolkitize_expr(ex::Symbol,vars,curmod)
34-
names = [x.op.name for x in vars]
35-
op = ex names ? vars[findfirst(x->ex == x.op.name,vars)] : getproperty(curmod,ex) # HACK
36-
convert(Expression,op)
34+
names = tosymbol.(vars)
35+
idx = findfirst(x->ex == x, names)
36+
op = idx !== nothing ? vars[idx] : getproperty(curmod,ex)
37+
op
3738
end
3839

3940
function modelingtoolkitize_expr(ex::Number,vars,curmod)
40-
convert(Expression,ex)
41+
ex
4142
end

0 commit comments

Comments
 (0)