Skip to content

Simple macro-based frontend #2174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
Expand All @@ -46,6 +47,12 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[weakdeps]
DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6"

[extensions]
MTKDeepDiffsExt = "DeepDiffs"

[compat]
AbstractTrees = "0.3, 0.4"
ArrayInterface = "6, 7"
Expand All @@ -67,6 +74,7 @@ JuliaFormatter = "1"
JumpProcesses = "9.1"
LabelledArrays = "1.3"
Latexify = "0.11, 0.12, 0.13, 0.14, 0.15, 0.16"
MLStyle = "0.4.17"
MacroTools = "0.5"
NaNMath = "0.3, 1"
RecursiveArrayTools = "2.3"
Expand All @@ -84,9 +92,6 @@ UnPack = "0.1, 1.0"
Unitful = "1.1"
julia = "1.6"

[extensions]
MTKDeepDiffsExt = "DeepDiffs"

[extras]
AmplNLWriter = "7c4d4715-977e-5154-bfe0-e096adeac482"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Expand All @@ -113,6 +118,3 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["AmplNLWriter", "BenchmarkTools", "ControlSystemsMTK", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials"]

[weakdeps]
DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6"
3 changes: 2 additions & 1 deletion src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ include("utils.jl")
include("domains.jl")

include("systems/abstractsystem.jl")
include("systems/model_parsing.jl")
include("systems/connectors.jl")
include("systems/callbacks.jl")

Expand Down Expand Up @@ -181,7 +182,7 @@ export JumpProblem, DiscreteProblem
export NonlinearSystem, OptimizationSystem, ConstraintsSystem
export alias_elimination, flatten
export connect, @connector, Connection, Flow, Stream, instream
export @component
export @component, @model
export isinput, isoutput, getbounds, hasbounds, isdisturbance, istunable, getdist, hasdist,
tunable_parameters, isirreducible, getdescription, hasdescription, isbinaryvar,
isintegervar
Expand Down
235 changes: 235 additions & 0 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
macro connector(name::Symbol, body)
esc(connector_macro(__module__, name, body))
end

struct Model{F, S}
f::F
structure::S
end
(m::Model)(args...; kw...) = m.f(args...; kw...)

using MLStyle
function connector_macro(mod, name, body)
if !Meta.isexpr(body, :block)
err = """
connector body must be a block! It should be in the form of
```
@connector Pin begin
v(t) = 1
(i(t) = 1), [connect = Flow]
end
```
"""
error(err)
end
vs = Num[]
dict = Dict{Symbol, Any}()
for arg in body.args
arg isa LineNumberNode && continue
push!(vs, Num(parse_variable_def!(dict, mod, arg, :variables)))
end
iv = get(dict, :independent_variable, nothing)
if iv === nothing
error("$name doesn't have a independent variable")
end
quote
$name = $Model((; name) -> begin
var"#___sys___" = $ODESystem($(Equation[]), $iv, $vs, $([]);
name)
$Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___"))
end, $dict)
end
end

function parse_variable_def!(dict, mod, arg, varclass)
MLStyle.@match arg begin
::Symbol => generate_var!(dict, arg, varclass)
Expr(:call, a, b) => generate_var!(dict, a, b, varclass)
Expr(:(=), a, b) => begin
var = parse_variable_def!(dict, mod, a, varclass)
def = parse_default(mod, b)
dict[varclass][getname(var)][:default] = def
setdefault(var, def)
end
Expr(:tuple, a, b) => begin
var = parse_variable_def!(dict, mod, a, varclass)
meta = parse_metadata(mod, b)
if (ct = get(meta, VariableConnectType, nothing)) !== nothing
dict[varclass][getname(var)][:connection_type] = nameof(ct)
end
set_var_metadata(var, meta)
end
_ => error("$arg cannot be parsed")
end
end

function generate_var(a, varclass)
var = Symbolics.variable(a)
if varclass == :parameters
var = toparam(var)
end
var
end
function generate_var!(dict, a, varclass)
var = generate_var(a, varclass)
vd = get!(dict, varclass) do
Dict{Symbol, Dict{Symbol, Any}}()
end
vd[a] = Dict{Symbol, Any}()
var
end
function generate_var!(dict, a, b, varclass)
iv = generate_var(b, :variables)
prev_iv = get!(dict, :independent_variable) do
iv
end
@assert isequal(iv, prev_iv)
vd = get!(dict, varclass) do
Dict{Symbol, Dict{Symbol, Any}}()
end
vd[a] = Dict{Symbol, Any}()
var = Symbolics.variable(a, T = SymbolicUtils.FnType{Tuple{Real}, Real})(iv)
if varclass == :parameters
var = toparam(var)
end
var
end
function parse_default(mod, a)
a = Base.remove_linenums!(deepcopy(a))
MLStyle.@match a begin
Expr(:block, a) => get_var(mod, a)
::Symbol => get_var(mod, a)
::Number => a
_ => error("Cannot parse default $a")
end
end
function parse_metadata(mod, a)
MLStyle.@match a begin
Expr(:vect, eles...) => Dict(parse_metadata(mod, e) for e in eles)
Expr(:(=), a, b) => Symbolics.option_to_metadata_type(Val(a)) => get_var(mod, b)
_ => error("Cannot parse metadata $a")
end
end
function set_var_metadata(a, ms)
for (m, v) in ms
a = setmetadata(a, m, v)
end
a
end
function get_var(mod::Module, b)
b isa Symbol ? getproperty(mod, b) : b
end

macro model(name::Symbol, expr)
esc(model_macro(__module__, name, expr))
end
function model_macro(mod, name, expr)
exprs = Expr(:block)
dict = Dict{Symbol, Any}()
comps = Symbol[]
ext = Ref{Any}(nothing)
vs = Symbol[]
ps = Symbol[]
eqs = Expr[]
for arg in expr.args
arg isa LineNumberNode && continue
arg.head == :macrocall || error("$arg is not valid syntax. Expected a macro call.")
parse_model!(exprs.args, comps, ext, eqs, vs, ps, dict, mod, arg)
end
iv = get(dict, :independent_variable, nothing)
if iv === nothing
iv = dict[:independent_variable] = variable(:t)
end
sys = :($ODESystem($Equation[$(eqs...)], $iv, [$(vs...)], [$(ps...)];
systems = [$(comps...)], name))
if ext[] === nothing
push!(exprs.args, sys)
else
push!(exprs.args, :($extend($sys, $(ext[]))))
end
:($name = $Model((; name) -> $exprs, $dict))
end
function parse_model!(exprs, comps, ext, eqs, vs, ps, dict, mod, arg)
mname = arg.args[1]
body = arg.args[end]
if mname == Symbol("@components")
parse_components!(exprs, comps, dict, body)
elseif mname == Symbol("@extend")
parse_extend!(exprs, ext, dict, body)
elseif mname == Symbol("@variables")
parse_variables!(exprs, vs, dict, mod, body, :variables)
elseif mname == Symbol("@parameters")
parse_variables!(exprs, ps, dict, mod, body, :parameters)
elseif mname == Symbol("@equations")
parse_equations!(exprs, eqs, dict, body)
else
error("$mname is not handled.")
end
end
function parse_components!(exprs, cs, dict, body)
expr = Expr(:block)
push!(exprs, expr)
comps = Vector{String}[]
for arg in body.args
arg isa LineNumberNode && continue
MLStyle.@match arg begin
Expr(:(=), a, b) => begin
push!(cs, a)
push!(comps, [String(a), String(b.args[1])])
arg = deepcopy(arg)
b = deepcopy(arg.args[2])
push!(b.args, Expr(:kw, :name, Meta.quot(a)))
arg.args[2] = b
push!(expr.args, arg)
end
_ => error("`@components` only takes assignment expressions. Got $arg")
end
end
dict[:components] = comps
end
function parse_extend!(exprs, ext, dict, body)
expr = Expr(:block)
push!(exprs, expr)
body = deepcopy(body)
MLStyle.@match body begin
Expr(:(=), a, b) => begin
vars = nothing
if Meta.isexpr(b, :(=))
vars = a
if !Meta.isexpr(vars, :tuple)
error("`@extend` destructuring only takes an tuple as LHS. Got $body")
end
a, b = b.args
vars, a, b
end
ext[] = a
push!(b.args, Expr(:kw, :name, Meta.quot(a)))
dict[:extend] = [Symbol.(vars.args), a, b.args[1]]
push!(expr.args, :($a = $b))
if vars !== nothing
push!(expr.args, :(@unpack $vars = $a))
end
end
_ => error("`@extend` only takes an assignment expression. Got $body")
end
end
function parse_variables!(exprs, vs, dict, mod, body, varclass)
expr = Expr(:block)
push!(exprs, expr)
for arg in body.args
arg isa LineNumberNode && continue
vv = parse_variable_def!(dict, mod, arg, varclass)
v = Num(vv)
name = getname(v)
push!(vs, name)
push!(expr.args, :($name = $v))
end
end
function parse_equations!(exprs, eqs, dict, body)
for arg in body.args
arg isa LineNumberNode && continue
push!(eqs, arg)
end
# TODO: does this work with TOML?
dict[:equations] = readable_code.(eqs)
end
74 changes: 74 additions & 0 deletions test/model_parsing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using ModelingToolkit, Test

@connector RealInput begin u(t), [input = true] end
@connector RealOutput begin u(t), [output = true] end
@model Constant begin
@components begin output = RealOutput() end
@parameters begin k, [description = "Constant output value of block"] end
@equations begin output.u ~ k end
end

@variables t
D = Differential(t)

@connector Pin begin
v(t) = 0 # Potential at the pin [V]
i(t), [connect = Flow] # Current flowing into the pin [A]
end

@model OnePort begin
@components begin
p = Pin()
n = Pin()
end
@variables begin
v(t)
i(t)
end
@equations begin
v ~ p.v - n.v
0 ~ p.i + n.i
i ~ p.i
end
end

@model Ground begin
@components begin g = Pin() end
@equations begin g.v ~ 0 end
end

@model Resistor begin
@extend v, i = oneport = OnePort()
@parameters begin R = 1 end
@equations begin v ~ i * R end
end

@model Capacitor begin
@extend v, i = oneport = OnePort()
@parameters begin C = 1 end
@equations begin D(v) ~ i / C end
end

@model Voltage begin
@extend v, i = oneport = OnePort()
@components begin V = RealInput() end
@equations begin v ~ V.u end
end

@model RC begin
@components begin
resistor = Resistor()
capacitor = Capacitor()
source = Voltage()
constant = Constant()
ground = Ground()
end
@equations begin
connect(constant.output, source.V)
connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)
end
end
@named rc = RC()
@test length(equations(structural_simplify(rc))) == 1
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using SafeTestsets, Test
@safetestset "Reduction Test" begin include("reduction.jl") end
@safetestset "ODAEProblem Test" begin include("odaeproblem.jl") end
@safetestset "Components Test" begin include("components.jl") end
@safetestset "Model Parsing Test" begin include("model_parsing.jl") end
@safetestset "print_tree" begin include("print_tree.jl") end
@safetestset "Error Handling" begin include("error_handling.jl") end
@safetestset "StructuralTransformations" begin include("structural_transformation/runtests.jl") end
Expand Down