|
| 1 | +macro connector(name::Symbol, body) |
| 2 | + esc(connector_macro(__module__, name, body)) |
| 3 | +end |
| 4 | + |
| 5 | +struct Model{F, S} |
| 6 | + f::F |
| 7 | + structure::S |
| 8 | +end |
| 9 | +(m::Model)(args...; kw...) = m.f(args...; kw...) |
| 10 | + |
| 11 | +using MLStyle |
| 12 | +function connector_macro(mod, name, body) |
| 13 | + if !Meta.isexpr(body, :block) |
| 14 | + err = """ |
| 15 | + connector body must be a block! It should be in the form of |
| 16 | + ``` |
| 17 | + @connector Pin begin |
| 18 | + v(t) = 1 |
| 19 | + (i(t) = 1), [connect = Flow] |
| 20 | + end |
| 21 | + ``` |
| 22 | + """ |
| 23 | + error(err) |
| 24 | + end |
| 25 | + vs = Num[] |
| 26 | + dict = Dict{Symbol, Any}() |
| 27 | + for arg in body.args |
| 28 | + arg isa LineNumberNode && continue |
| 29 | + push!(vs, Num(parse_variable_def!(dict, mod, arg, :variables))) |
| 30 | + end |
| 31 | + iv = get(dict, :independent_variable, nothing) |
| 32 | + if iv === nothing |
| 33 | + error("$name doesn't have a independent variable") |
| 34 | + end |
| 35 | + quote |
| 36 | + $name = $Model((; name) -> begin |
| 37 | + var"#___sys___" = $ODESystem($(Equation[]), $iv, $vs, $([]); |
| 38 | + name) |
| 39 | + $Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___")) |
| 40 | + end, $dict) |
| 41 | + end |
| 42 | +end |
| 43 | + |
| 44 | +function parse_variable_def!(dict, mod, arg, varclass) |
| 45 | + MLStyle.@match arg begin |
| 46 | + ::Symbol => generate_var!(dict, arg, varclass) |
| 47 | + Expr(:call, a, b) => generate_var!(dict, a, b, varclass) |
| 48 | + Expr(:(=), a, b) => begin |
| 49 | + var = parse_variable_def!(dict, mod, a, varclass) |
| 50 | + def = parse_default(mod, b) |
| 51 | + dict[varclass][getname(var)][:default] = def |
| 52 | + setdefault(var, def) |
| 53 | + end |
| 54 | + Expr(:tuple, a, b) => begin |
| 55 | + var = parse_variable_def!(dict, mod, a, varclass) |
| 56 | + meta = parse_metadata(mod, b) |
| 57 | + if (ct = get(meta, VariableConnectType, nothing)) !== nothing |
| 58 | + dict[varclass][getname(var)][:connection_type] = nameof(ct) |
| 59 | + end |
| 60 | + set_var_metadata(var, meta) |
| 61 | + end |
| 62 | + _ => error("$arg cannot be parsed") |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +function generate_var(a, varclass) |
| 67 | + var = Symbolics.variable(a) |
| 68 | + if varclass == :parameters |
| 69 | + var = toparam(var) |
| 70 | + end |
| 71 | + var |
| 72 | +end |
| 73 | +function generate_var!(dict, a, varclass) |
| 74 | + var = generate_var(a, varclass) |
| 75 | + vd = get!(dict, varclass) do |
| 76 | + Dict{Symbol, Dict{Symbol, Any}}() |
| 77 | + end |
| 78 | + vd[a] = Dict{Symbol, Any}() |
| 79 | + var |
| 80 | +end |
| 81 | +function generate_var!(dict, a, b, varclass) |
| 82 | + iv = generate_var(b, :variables) |
| 83 | + prev_iv = get!(dict, :independent_variable) do |
| 84 | + iv |
| 85 | + end |
| 86 | + @assert isequal(iv, prev_iv) |
| 87 | + vd = get!(dict, varclass) do |
| 88 | + Dict{Symbol, Dict{Symbol, Any}}() |
| 89 | + end |
| 90 | + vd[a] = Dict{Symbol, Any}() |
| 91 | + var = Symbolics.variable(a, T = SymbolicUtils.FnType{Tuple{Real}, Real})(iv) |
| 92 | + if varclass == :parameters |
| 93 | + var = toparam(var) |
| 94 | + end |
| 95 | + var |
| 96 | +end |
| 97 | +function parse_default(mod, a) |
| 98 | + a = Base.remove_linenums!(deepcopy(a)) |
| 99 | + MLStyle.@match a begin |
| 100 | + Expr(:block, a) => get_var(mod, a) |
| 101 | + ::Symbol => get_var(mod, a) |
| 102 | + ::Number => a |
| 103 | + _ => error("Cannot parse default $a") |
| 104 | + end |
| 105 | +end |
| 106 | +function parse_metadata(mod, a) |
| 107 | + MLStyle.@match a begin |
| 108 | + Expr(:vect, eles...) => Dict(parse_metadata(mod, e) for e in eles) |
| 109 | + Expr(:(=), a, b) => Symbolics.option_to_metadata_type(Val(a)) => get_var(mod, b) |
| 110 | + _ => error("Cannot parse metadata $a") |
| 111 | + end |
| 112 | +end |
| 113 | +function set_var_metadata(a, ms) |
| 114 | + for (m, v) in ms |
| 115 | + a = setmetadata(a, m, v) |
| 116 | + end |
| 117 | + a |
| 118 | +end |
| 119 | +function get_var(mod::Module, b) |
| 120 | + b isa Symbol ? getproperty(mod, b) : b |
| 121 | +end |
| 122 | + |
| 123 | +macro model(name::Symbol, expr) |
| 124 | + esc(model_macro(__module__, name, expr)) |
| 125 | +end |
| 126 | +function model_macro(mod, name, expr) |
| 127 | + exprs = Expr(:block) |
| 128 | + dict = Dict{Symbol, Any}() |
| 129 | + comps = Symbol[] |
| 130 | + ext = Ref{Any}(nothing) |
| 131 | + vs = Symbol[] |
| 132 | + ps = Symbol[] |
| 133 | + eqs = Expr[] |
| 134 | + for arg in expr.args |
| 135 | + arg isa LineNumberNode && continue |
| 136 | + arg.head == :macrocall || error("$arg is not valid syntax. Expected a macro call.") |
| 137 | + parse_model!(exprs.args, comps, ext, eqs, vs, ps, dict, mod, arg) |
| 138 | + end |
| 139 | + iv = get(dict, :independent_variable, nothing) |
| 140 | + if iv === nothing |
| 141 | + iv = dict[:independent_variable] = variable(:t) |
| 142 | + end |
| 143 | + sys = :($ODESystem($Equation[$(eqs...)], $iv, [$(vs...)], [$(ps...)]; |
| 144 | + systems = [$(comps...)], name)) |
| 145 | + if ext[] === nothing |
| 146 | + push!(exprs.args, sys) |
| 147 | + else |
| 148 | + push!(exprs.args, :($extend($sys, $(ext[])))) |
| 149 | + end |
| 150 | + :($name = $Model((; name) -> $exprs, $dict)) |
| 151 | +end |
| 152 | +function parse_model!(exprs, comps, ext, eqs, vs, ps, dict, mod, arg) |
| 153 | + mname = arg.args[1] |
| 154 | + body = arg.args[end] |
| 155 | + if mname == Symbol("@components") |
| 156 | + parse_components!(exprs, comps, dict, body) |
| 157 | + elseif mname == Symbol("@extend") |
| 158 | + parse_extend!(exprs, ext, dict, body) |
| 159 | + elseif mname == Symbol("@variables") |
| 160 | + parse_variables!(exprs, vs, dict, mod, body, :variables) |
| 161 | + elseif mname == Symbol("@parameters") |
| 162 | + parse_variables!(exprs, ps, dict, mod, body, :parameters) |
| 163 | + elseif mname == Symbol("@equations") |
| 164 | + parse_equations!(exprs, eqs, dict, body) |
| 165 | + else |
| 166 | + error("$mname is not handled.") |
| 167 | + end |
| 168 | +end |
| 169 | +function parse_components!(exprs, cs, dict, body) |
| 170 | + expr = Expr(:block) |
| 171 | + push!(exprs, expr) |
| 172 | + comps = Vector{String}[] |
| 173 | + for arg in body.args |
| 174 | + arg isa LineNumberNode && continue |
| 175 | + MLStyle.@match arg begin |
| 176 | + Expr(:(=), a, b) => begin |
| 177 | + push!(cs, a) |
| 178 | + push!(comps, [String(a), String(b.args[1])]) |
| 179 | + arg = deepcopy(arg) |
| 180 | + b = deepcopy(arg.args[2]) |
| 181 | + push!(b.args, Expr(:kw, :name, Meta.quot(a))) |
| 182 | + arg.args[2] = b |
| 183 | + push!(expr.args, arg) |
| 184 | + end |
| 185 | + _ => error("`@components` only takes assignment expressions. Got $arg") |
| 186 | + end |
| 187 | + end |
| 188 | + dict[:components] = comps |
| 189 | +end |
| 190 | +function parse_extend!(exprs, ext, dict, body) |
| 191 | + expr = Expr(:block) |
| 192 | + push!(exprs, expr) |
| 193 | + body = deepcopy(body) |
| 194 | + MLStyle.@match body begin |
| 195 | + Expr(:(=), a, b) => begin |
| 196 | + vars = nothing |
| 197 | + if Meta.isexpr(b, :(=)) |
| 198 | + vars = a |
| 199 | + if !Meta.isexpr(vars, :tuple) |
| 200 | + error("`@extend` destructuring only takes an tuple as LHS. Got $body") |
| 201 | + end |
| 202 | + a, b = b.args |
| 203 | + vars, a, b |
| 204 | + end |
| 205 | + ext[] = a |
| 206 | + push!(b.args, Expr(:kw, :name, Meta.quot(a))) |
| 207 | + dict[:extend] = [Symbol.(vars.args), a, b.args[1]] |
| 208 | + push!(expr.args, :($a = $b)) |
| 209 | + if vars !== nothing |
| 210 | + push!(expr.args, :(@unpack $vars = $a)) |
| 211 | + end |
| 212 | + end |
| 213 | + _ => error("`@extend` only takes an assignment expression. Got $body") |
| 214 | + end |
| 215 | +end |
| 216 | +function parse_variables!(exprs, vs, dict, mod, body, varclass) |
| 217 | + expr = Expr(:block) |
| 218 | + push!(exprs, expr) |
| 219 | + for arg in body.args |
| 220 | + arg isa LineNumberNode && continue |
| 221 | + vv = parse_variable_def!(dict, mod, arg, varclass) |
| 222 | + v = Num(vv) |
| 223 | + name = getname(v) |
| 224 | + push!(vs, name) |
| 225 | + push!(expr.args, :($name = $v)) |
| 226 | + end |
| 227 | +end |
| 228 | +function parse_equations!(exprs, eqs, dict, body) |
| 229 | + for arg in body.args |
| 230 | + arg isa LineNumberNode && continue |
| 231 | + push!(eqs, arg) |
| 232 | + end |
| 233 | + # TODO: does this work with TOML? |
| 234 | + dict[:equations] = readable_code.(eqs) |
| 235 | +end |
0 commit comments