Skip to content

Commit b6801d4

Browse files
Merge pull request #377 from matthew-kapp/refactor-macro
Refactor thermal library to use macros
2 parents a3bdab3 + 95727f2 commit b6801d4

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/Thermal/HeatTransfer/ideal_components.jl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,19 @@ This is a model to collect the heat flows from `m` heatports to one single heatp
201201
202202
- `m`: Number of heat ports (e.g. m=2: `port_a1`, `port_a2`)
203203
"""
204-
@component function ThermalCollector(; name, m::Integer = 1)
205-
port_a = [HeatPort(name = Symbol(:port_a, i)) for i in 1:m]
206-
@named port_b = HeatPort()
207-
eqs = [port_b.Q_flow + sum(k -> k.Q_flow, port_a) ~ 0
208-
port_b.T ~ port_a[1].T]
209-
for i in 1:(m - 1)
210-
push!(eqs, port_a[i].T ~ port_a[i + 1].T)
211-
end
212-
ODESystem(eqs, t, [], []; systems = [port_a..., port_b], name = name)
204+
@mtkmodel ThermalCollector begin
205+
@structural_parameters begin
206+
m::Integer = 1
207+
end
208+
209+
@components begin
210+
port_a = [HeatPort(name = Symbol(:port_a, i)) for i in 1:m]
211+
port_b = HeatPort()
212+
end
213+
214+
@equations begin
215+
port_b.Q_flow + sum(k -> k.Q_flow, port_a) ~ 0
216+
port_b.T ~ port_a[1].T
217+
[port_a[i].T ~ port_a[i + 1].T for i in 1:(m - 1)]
218+
end
213219
end

src/Thermal/utils.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
@connector function HeatPort(;
2-
T = nothing, T_guess = 273.15 + 20, Q_flow = nothing, Q_flow_guess = 0.0, name)
3-
pars = @parameters begin
4-
T_guess = T_guess
5-
Q_flow_guess = Q_flow_guess
1+
@connector HeatPort begin
2+
@parameters begin
3+
T_guess = 273.15 + 20
4+
Q_flow_guess = 0.0
65
end
7-
vars = @variables begin
8-
T(t) = T, [guess = T_guess]
9-
Q_flow(t) = Q_flow, [guess = Q_flow_guess, connect = Flow]
6+
7+
@variables begin
8+
T(t), [guess = T_guess]
9+
Q_flow(t), [guess = Q_flow_guess, connect = Flow]
1010
end
11-
return ODESystem(Equation[], t, vars, pars; name)
1211
end
1312
Base.@doc """
1413
HeatPort(; T = nothing, T_guess = 273.15 + 20, Q_flow = nothing, Q_flow_guess = 0.0, name)

0 commit comments

Comments
 (0)