Skip to content

Commit 38f8723

Browse files
committed
Add at component
1 parent aff4ae3 commit 38f8723

File tree

6 files changed

+59
-45
lines changed

6 files changed

+59
-45
lines changed

docs/src/tutorials/acausal_components.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ using ModelingToolkit, Plots, DifferentialEquations
2828
ODESystem(Equation[], t, sts, []; name = name)
2929
end
3030
31-
function Ground(; name)
31+
@component function Ground(; name)
3232
@named g = Pin()
3333
eqs = [g.v ~ 0]
3434
compose(ODESystem(eqs, t, [], []; name = name), g)
3535
end
3636
37-
function OnePort(; name)
37+
@component function OnePort(; name)
3838
@named p = Pin()
3939
@named n = Pin()
4040
sts = @variables v(t)=1.0 i(t)=1.0
@@ -44,7 +44,7 @@ function OnePort(; name)
4444
compose(ODESystem(eqs, t, sts, []; name = name), p, n)
4545
end
4646
47-
function Resistor(; name, R = 1.0)
47+
@component function Resistor(; name, R = 1.0)
4848
@named oneport = OnePort()
4949
@unpack v, i = oneport
5050
ps = @parameters R = R
@@ -54,7 +54,7 @@ function Resistor(; name, R = 1.0)
5454
extend(ODESystem(eqs, t, [], ps; name = name), oneport)
5555
end
5656
57-
function Capacitor(; name, C = 1.0)
57+
@component function Capacitor(; name, C = 1.0)
5858
@named oneport = OnePort()
5959
@unpack v, i = oneport
6060
ps = @parameters C = C
@@ -65,7 +65,7 @@ function Capacitor(; name, C = 1.0)
6565
extend(ODESystem(eqs, t, [], ps; name = name), oneport)
6666
end
6767
68-
function ConstantVoltage(; name, V = 1.0)
68+
@component function ConstantVoltage(; name, V = 1.0)
6969
@named oneport = OnePort()
7070
@unpack v = oneport
7171
ps = @parameters V = V
@@ -149,7 +149,7 @@ this component, we generate an `ODESystem` with a `Pin` subcomponent and specify
149149
that the voltage in such a `Pin` is equal to zero. This gives:
150150

151151
```@example acausal
152-
function Ground(; name)
152+
@component function Ground(; name)
153153
@named g = Pin()
154154
eqs = [g.v ~ 0]
155155
compose(ODESystem(eqs, t, [], []; name = name), g)
@@ -163,7 +163,7 @@ zero, and the current of the component equals to the current of the positive
163163
pin.
164164

165165
```@example acausal
166-
function OnePort(; name)
166+
@component function OnePort(; name)
167167
@named p = Pin()
168168
@named n = Pin()
169169
sts = @variables v(t)=1.0 i(t)=1.0
@@ -182,7 +182,7 @@ of charge we know that the current in must equal the current out, which means
182182
zero. This leads to our resistor equations:
183183

184184
```@example acausal
185-
function Resistor(; name, R = 1.0)
185+
@component function Resistor(; name, R = 1.0)
186186
@named oneport = OnePort()
187187
@unpack v, i = oneport
188188
ps = @parameters R = R
@@ -205,7 +205,7 @@ we can use `@unpack` to avoid the namespacing.
205205
Using our knowledge of circuits, we similarly construct the `Capacitor`:
206206

207207
```@example acausal
208-
function Capacitor(; name, C = 1.0)
208+
@component function Capacitor(; name, C = 1.0)
209209
@named oneport = OnePort()
210210
@unpack v, i = oneport
211211
ps = @parameters C = C
@@ -223,7 +223,7 @@ constant voltage, essentially generating the electric current. We would then
223223
model this as:
224224

225225
```@example acausal
226-
function ConstantVoltage(; name, V = 1.0)
226+
@component function ConstantVoltage(; name, V = 1.0)
227227
@named oneport = OnePort()
228228
@unpack v = oneport
229229
ps = @parameters V = V

examples/electrical_components.jl

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ using ModelingToolkit, OrdinaryDiffEq
77
ODESystem(Equation[], t, sts, []; name = name)
88
end
99

10-
function Ground(; name)
10+
@component function Ground(; name)
1111
@named g = Pin()
1212
eqs = [g.v ~ 0]
1313
compose(ODESystem(eqs, t, [], []; name = name), g)
1414
end
1515

16-
function OnePort(; name)
16+
@component function OnePort(; name)
1717
@named p = Pin()
1818
@named n = Pin()
1919
sts = @variables v(t)=1.0 i(t)=1.0
@@ -23,7 +23,7 @@ function OnePort(; name)
2323
compose(ODESystem(eqs, t, sts, []; name = name), p, n)
2424
end
2525

26-
function Resistor(; name, R = 1.0)
26+
@component function Resistor(; name, R = 1.0)
2727
@named oneport = OnePort()
2828
@unpack v, i = oneport
2929
ps = @parameters R = R
@@ -33,7 +33,7 @@ function Resistor(; name, R = 1.0)
3333
extend(ODESystem(eqs, t, [], ps; name = name), oneport)
3434
end
3535

36-
function Capacitor(; name, C = 1.0)
36+
@component function Capacitor(; name, C = 1.0)
3737
@named oneport = OnePort()
3838
@unpack v, i = oneport
3939
ps = @parameters C = C
@@ -44,7 +44,7 @@ function Capacitor(; name, C = 1.0)
4444
extend(ODESystem(eqs, t, [], ps; name = name), oneport)
4545
end
4646

47-
function ConstantVoltage(; name, V = 1.0)
47+
@component function ConstantVoltage(; name, V = 1.0)
4848
@named oneport = OnePort()
4949
@unpack v = oneport
5050
ps = @parameters V = V
@@ -54,7 +54,7 @@ function ConstantVoltage(; name, V = 1.0)
5454
extend(ODESystem(eqs, t, [], ps; name = name), oneport)
5555
end
5656

57-
function Inductor(; name, L = 1.0)
57+
@component function Inductor(; name, L = 1.0)
5858
@named oneport = OnePort()
5959
@unpack v, i = oneport
6060
ps = @parameters L = L
@@ -70,7 +70,7 @@ end
7070
ODESystem(Equation[], t, [T, Q_flow], [], name = name)
7171
end
7272

73-
function HeatingResistor(; name, R = 1.0, TAmbient = 293.15, alpha = 1.0)
73+
@component function HeatingResistor(; name, R = 1.0, TAmbient = 293.15, alpha = 1.0)
7474
@named p = Pin()
7575
@named n = Pin()
7676
@named h = HeatPort()
@@ -85,7 +85,7 @@ function HeatingResistor(; name, R = 1.0, TAmbient = 293.15, alpha = 1.0)
8585
name = name), p, n, h)
8686
end
8787

88-
function HeatCapacitor(; name, rho = 8050, V = 1, cp = 460, TAmbient = 293.15)
88+
@component function HeatCapacitor(; name, rho = 8050, V = 1, cp = 460, TAmbient = 293.15)
8989
@parameters rho=rho V=V cp=cp
9090
C = rho * V * cp
9191
@named h = HeatPort()

src/ModelingToolkit.jl

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export JumpProblem, DiscreteProblem
181181
export NonlinearSystem, OptimizationSystem, ConstraintsSystem
182182
export alias_elimination, flatten
183183
export connect, @connector, Connection, Flow, Stream, instream
184+
export @component
184185
export isinput, isoutput, getbounds, hasbounds, isdisturbance, istunable, getdist, hasdist,
185186
tunable_parameters, isirreducible, getdescription, hasdescription, isbinaryvar,
186187
isintegervar

src/systems/abstractsystem.jl

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const SYSTEM_COUNT = Threads.Atomic{UInt}(0)
22

33
struct GUIMetadata
4-
type::String
4+
type::Symbol
55
layout::Any
66
end
77

@@ -1117,6 +1117,40 @@ macro namespace(expr)
11171117
esc(_config(expr, true))
11181118
end
11191119

1120+
function component_post_processing(expr, isconnector)
1121+
@assert expr isa Expr && (expr.head == :function || (expr.head == :(=) &&
1122+
expr.args[1] isa Expr &&
1123+
expr.args[1].head == :call))
1124+
1125+
sig = expr.args[1]
1126+
body = expr.args[2]
1127+
1128+
fname = sig.args[1]
1129+
args = sig.args[2:end]
1130+
1131+
quote
1132+
function $fname($(args...))
1133+
function f()
1134+
$body
1135+
end
1136+
res = f()
1137+
if $isdefined(res, :gui_metadata) && $getfield(res, :gui_metadata) === nothing
1138+
name = $(Meta.quot(fname))
1139+
if $isconnector
1140+
$Setfield.@set!(res.connector_type=$connector_type(res))
1141+
end
1142+
$Setfield.@set!(res.gui_metadata=$GUIMetadata(name))
1143+
else
1144+
res
1145+
end
1146+
end
1147+
end
1148+
end
1149+
1150+
macro component(expr)
1151+
esc(component_post_processing(expr, false))
1152+
end
1153+
11201154
"""
11211155
$(SIGNATURES)
11221156

src/systems/connectors.jl

+1-25
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,8 @@ function get_connection_type(s)
66
getmetadata(s, VariableConnectType, Equality)
77
end
88

9-
function with_connector_type(expr)
10-
@assert expr isa Expr && (expr.head == :function || (expr.head == :(=) &&
11-
expr.args[1] isa Expr &&
12-
expr.args[1].head == :call))
13-
14-
sig = expr.args[1]
15-
body = expr.args[2]
16-
17-
fname = sig.args[1]
18-
args = sig.args[2:end]
19-
20-
quote
21-
function $fname($(args...))
22-
function f()
23-
$body
24-
end
25-
res = f()
26-
$isdefined(res, :connector_type) &&
27-
$getfield(res, :connector_type) === nothing ?
28-
$Setfield.@set!(res.connector_type=$connector_type(res)) : res
29-
end
30-
end
31-
end
32-
339
macro connector(expr)
34-
esc(with_connector_type(expr))
10+
esc(component_post_processing(expr, true))
3511
end
3612

3713
abstract type AbstractConnectorType end

test/components.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Test
22
using ModelingToolkit, OrdinaryDiffEq
3+
using ModelingToolkit: get_gui_metadata
34
using ModelingToolkit.BipartiteGraphs
45
using ModelingToolkit.StructuralTransformations
56
include("../examples/rc_model.jl")
@@ -33,7 +34,9 @@ function check_rc_sol(sol)
3334
sol[source.p.v] - sol[capacitor.p.v]
3435
end
3536

36-
include("../examples/rc_model.jl")
37+
@named pin = Pin()
38+
@test get_gui_metadata(pin).type == :Pin
39+
@test get_gui_metadata(rc_model.resistor).type == :Resistor
3740

3841
completed_rc_model = complete(rc_model)
3942
@test isequal(completed_rc_model.resistor.n.i, resistor.n.i)

0 commit comments

Comments
 (0)