Skip to content

Commit 403829e

Browse files
authored
refactor: cut down the description of variables (#194)
- or the formatter turns them into a function
1 parent 41de51e commit 403829e

File tree

7 files changed

+183
-172
lines changed

7 files changed

+183
-172
lines changed

docs/src/tutorials/thermal_model.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ using ModelingToolkitStandardLibrary.Thermal, ModelingToolkit, OrdinaryDiffEq, P
1313
1414
C1 = 15
1515
C2 = 15
16-
@named mass1 = HeatCapacitor(C = C1, T_start = 373.15)
17-
@named mass2 = HeatCapacitor(C = C2, T_start = 273.15)
16+
@named mass1 = HeatCapacitor(C = C1, T = 373.15)
17+
@named mass2 = HeatCapacitor(C = C2, T = 273.15)
1818
@named conduction = ThermalConductor(G = 10)
1919
@named Tsensor1 = TemperatureSensor()
2020
@named Tsensor2 = TemperatureSensor()

src/Thermal/HeatTransfer/ideal_components.jl

+62-60
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
2-
HeatCapacitor(; name, C, T_start=273.15 + 20)
2+
HeatCapacitor(; name, C, T = 273.15 + 20)
33
44
Lumped thermal element storing heat
55
66
# States:
77
8-
- `T`: [`K`] Temperature of element
8+
- `T`: [`K`] Temperature of element. It accepts an initial value, which defaults to 273.15 + 20.
99
- `der_T`: [`K/s`] Time derivative of temperature
1010
1111
# Connectors:
@@ -15,25 +15,28 @@ Lumped thermal element storing heat
1515
# Parameters:
1616
1717
- `C`: [`J/K`] Heat capacity of element (= cp*m)
18-
- `T_start`: [`K`] Initial temperature of element
1918
"""
20-
@component function HeatCapacitor(; name, C, T_start = 273.15 + 20)
21-
@named port = HeatPort()
22-
@parameters C = C
23-
sts = @variables begin
24-
T(t) = T_start
19+
@mtkmodel HeatCapacitor begin
20+
@components begin
21+
port = HeatPort()
22+
end
23+
@parameters begin
24+
C, [description = "Heat capacity of element"]
25+
end
26+
@variables begin
27+
T(t) = 273.15 + 20
2528
der_T(t) = 0.0
2629
end
2730

28-
D = Differential(t)
29-
eqs = [T ~ port.T
31+
@equations begin
32+
T ~ port.T
3033
der_T ~ port.Q_flow / C
31-
D(T) ~ der_T]
32-
ODESystem(eqs, t, sts, [C]; systems = [port], name = name)
34+
D(T) ~ der_T
35+
end
3336
end
3437

3538
"""
36-
ThermalConductor(;name, G)
39+
ThermalConductor(; name, G)
3740
3841
Lumped thermal element transporting heat without storing it.
3942
@@ -50,14 +53,14 @@ see [`Element1D`](@ref)
5053
5154
- `G`: [`W/K`] Constant thermal conductance of material
5255
"""
53-
@component function ThermalConductor(; name, G)
54-
@named element1d = Element1D()
55-
@unpack Q_flow, dT = element1d
56-
pars = @parameters G = G
57-
eqs = [
58-
Q_flow ~ G * dT,
59-
]
60-
extend(ODESystem(eqs, t, [], pars; name = name), element1d)
56+
@mtkmodel ThermalConductor begin
57+
@extend Q_flow, dT = element1d = Element1D()
58+
@parameters begin
59+
G
60+
end
61+
@equations begin
62+
Q_flow ~ G * dT
63+
end
6164
end
6265

6366
"""
@@ -79,15 +82,14 @@ Lumped thermal element transporting heat without storing it.
7982
8083
- `R`: [`K/W`] Constant thermal resistance of material
8184
"""
82-
@component function ThermalResistor(; name, R)
83-
@named element1d = Element1D()
84-
@unpack Q_flow, dT = element1d
85-
pars = @parameters R = R
86-
eqs = [
87-
dT ~ R * Q_flow,
88-
]
89-
90-
extend(ODESystem(eqs, t, [], pars; name = name), element1d)
85+
@mtkmodel ThermalResistor begin
86+
@extend Q_flow, dT = element1d = Element1D()
87+
@parameters begin
88+
R
89+
end
90+
@equations begin
91+
dT ~ R * Q_flow
92+
end
9193
end
9294

9395
"""
@@ -109,14 +111,14 @@ Lumped thermal element for heat convection.
109111
110112
- `G`: [W/K] Convective thermal conductance
111113
"""
112-
@component function ConvectiveConductor(; name, G)
113-
@named convective_element1d = ConvectiveElement1D()
114-
@unpack Q_flow, dT = convective_element1d
115-
@parameters G = G
116-
eqs = [
117-
Q_flow ~ G * dT,
118-
]
119-
extend(ODESystem(eqs, t, [], [G]; name = name), convective_element1d)
114+
@mtkmodel ConvectiveConductor begin
115+
@extend Q_flow, dT = convective_element1d = ConvectiveElement1D()
116+
@parameters begin
117+
G
118+
end
119+
@equations begin
120+
Q_flow ~ G * dT
121+
end
120122
end
121123

122124
"""
@@ -126,7 +128,7 @@ Lumped thermal element for heat convection.
126128
127129
# States:
128130
129-
- `dT`: [`K`] Temperature difference across the component `solid.T` - `fluid.T`
131+
- `dT`: [`K`] Temperature difference across the component `solid.T` - `fluid.T`
130132
- `Q_flow`: [`W`] Heat flow rate from `solid` -> `fluid`
131133
132134
# Connectors:
@@ -138,14 +140,14 @@ Lumped thermal element for heat convection.
138140
139141
- `R`: [`K/W`] Constant thermal resistance of material
140142
"""
141-
@component function ConvectiveResistor(; name, R)
142-
@named convective_element1d = ConvectiveElement1D()
143-
@unpack Q_flow, dT = convective_element1d
144-
@parameters R = R
145-
eqs = [
146-
dT ~ R * Q_flow,
147-
]
148-
extend(ODESystem(eqs, t, [], [R]; name = name), convective_element1d)
143+
@mtkmodel ConvectiveResistor begin
144+
@extend Q_flow, dT = convective_element1d = ConvectiveElement1D()
145+
@parameters begin
146+
R
147+
end
148+
@equations begin
149+
dT ~ R * Q_flow
150+
end
149151
end
150152

151153
"""
@@ -167,22 +169,22 @@ Lumped thermal element for radiation heat transfer.
167169
168170
- `G`: [m^2] Net radiation conductance between two surfaces # Stefan-Boltzmann constant TODO: extract into physical constants module or use existing one
169171
"""
170-
@component function BodyRadiation(; name, G)
171-
sigma = 5.6703744191844294e-8 # Stefan-Boltzmann constant TODO: extract into physical constants module or use existing one
172-
173-
@named element1d = Element1D()
174-
@unpack Q_flow, dT = element1d
175-
@unpack port_a, port_b = element1d
176-
pars = @parameters G = G
177-
eqs = [
178-
Q_flow ~ G * sigma * (port_a.T^4 - port_b.T^4),
179-
]
180-
181-
extend(ODESystem(eqs, t, [], pars; name = name), element1d)
172+
@mtkmodel BodyRadiation begin
173+
begin
174+
sigma = 5.6703744191844294e-8 # Stefan-Boltzmann constant TODO: extract into physical constants module or use existing one
175+
end
176+
177+
@extend Q_flow, dT, port_a, port_b = element1d = Element1D()
178+
@parameters begin
179+
G
180+
end
181+
@equations begin
182+
Q_flow ~ G * sigma * (port_a.T^4 - port_b.T^4)
183+
end
182184
end
183185

184186
"""
185-
ThermalCollector(; name, m=1)
187+
ThermalCollector(; name, m = 1)
186188
187189
Collects `m` heat flows
188190

src/Thermal/HeatTransfer/sensors.jl

+35-20
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ lags are associated with this sensor model.
1515
1616
- `port`
1717
"""
18-
@component function TemperatureSensor(; name)
19-
@named port = HeatPort()
20-
@variables T(t)
21-
eqs = [T ~ port.T
22-
port.Q_flow ~ 0]
23-
ODESystem(eqs, t, [T], [], systems = [port], name = name)
18+
@mtkmodel TemperatureSensor begin
19+
@components begin
20+
port = HeatPort()
21+
end
22+
@variables begin
23+
T(t)
24+
end
25+
@equations begin
26+
T ~ port.T
27+
port.Q_flow ~ 0
28+
end
2429
end
2530

2631
"""
@@ -40,14 +45,19 @@ output signal in kelvin.
4045
- `port_a`
4146
- `port_b`
4247
"""
43-
@component function RelativeTemperatureSensor(; name)
44-
@named port_a = HeatPort()
45-
@named port_b = HeatPort()
46-
@variables T(t)
47-
eqs = [T ~ port_a.T - port_b.T
48+
@mtkmodel RelativeTemperatureSensor begin
49+
@components begin
50+
port_a = HeatPort()
51+
port_b = HeatPort()
52+
end
53+
@variables begin
54+
T(t)
55+
end
56+
@equations begin
57+
T ~ port_a.T - port_b.T
4858
port_a.Q_flow ~ 0
49-
port_b.Q_flow ~ 0]
50-
ODESystem(eqs, t, [T], [], systems = [port_a, port_b], name = name)
59+
port_b.Q_flow ~ 0
60+
end
5161
end
5262

5363
"""
@@ -69,12 +79,17 @@ The output signal is positive, if the heat flows from `port_a` to `port_b`.
6979
- `port_a`
7080
- `port_b`
7181
"""
72-
@component function HeatFlowSensor(; name)
73-
@named port_a = HeatPort()
74-
@named port_b = HeatPort()
75-
@variables Q_flow(t)
76-
eqs = [port_a.T ~ port_b.T
82+
@mtkmodel HeatFlowSensor begin
83+
@components begin
84+
port_a = HeatPort()
85+
port_b = HeatPort()
86+
end
87+
@variables begin
88+
Q_flow(t)
89+
end
90+
@equations begin
91+
port_a.T ~ port_b.T
7792
port_a.Q_flow + port_b.Q_flow ~ 0
78-
Q_flow ~ port_a.Q_flow]
79-
ODESystem(eqs, t, [Q_flow], [], systems = [port_a, port_b], name = name)
93+
Q_flow ~ port_a.Q_flow
94+
end
8095
end

src/Thermal/HeatTransfer/sources.jl

+42-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
FixedHeatFlow(; name, Q_flow=1.0, T_ref=293.15, alpha=0.0)
2+
FixedHeatFlow(; name, Q_flow = 1.0, T_ref = 293.15, alpha = 0.0)
33
44
Fixed heat flow boundary condition.
55
@@ -17,18 +17,19 @@ the component FixedHeatFlow is connected, if parameter `Q_flow` is positive.
1717
- `T_ref`: [K] Reference temperature
1818
- `alpha`: [1/K] Temperature coefficient of heat flow rate
1919
"""
20-
@component function FixedHeatFlow(; name, Q_flow = 1.0, T_ref = 293.15, alpha = 0.0)
21-
pars = @parameters begin
22-
Q_flow = Q_flow
23-
T_ref = T_ref
24-
alpha = alpha
20+
@mtkmodel FixedHeatFlow begin
21+
@parameters begin
22+
Q_flow = 1.0, [description = "Fixed heat flow rate at port"]
23+
T_ref = 293.15, [description = "Reference temperature"]
24+
alpha = 0.0, [description = "Temperature coefficient of heat flow rate"]
25+
end
26+
@components begin
27+
port = HeatPort()
2528
end
26-
@named port = HeatPort()
2729

28-
eqs = [
29-
port.Q_flow ~ -Q_flow * (1 + alpha * (port.T - T_ref)),
30-
]
31-
ODESystem(eqs, t, [], pars; systems = [port], name = name)
30+
@equations begin
31+
port.Q_flow ~ -Q_flow * (1 + alpha * (port.T - T_ref))
32+
end
3233
end
3334

3435
"""
@@ -46,17 +47,20 @@ This model defines a fixed temperature `T` at its port in kelvin, i.e., it defin
4647
4748
- `T`: [K] Fixed temperature boundary condition
4849
"""
49-
@component function FixedTemperature(; name, T)
50-
@named port = HeatPort()
51-
pars = @parameters T = T
52-
eqs = [
53-
port.T ~ T,
54-
]
55-
ODESystem(eqs, t, [], pars; systems = [port], name = name)
50+
@mtkmodel FixedTemperature begin
51+
@components begin
52+
port = HeatPort()
53+
end
54+
@parameters begin
55+
T, [description = "Fixed temperature boundary condition"]
56+
end
57+
@equations begin
58+
port.T ~ T
59+
end
5660
end
5761

5862
"""
59-
PrescribedHeatFlow(; name, T_ref=293.15, alpha=0.0)
63+
PrescribedHeatFlow(; name, T_ref = 293.15, alpha = 0.0)
6064
6165
Prescribed heat flow boundary condition.
6266
@@ -76,18 +80,18 @@ dependent losses (which are given a reference temperature T_ref).
7680
- `T_ref`: [K] Reference temperature
7781
- `alpha`: [1/K] Temperature coefficient of heat flow rate
7882
"""
79-
@component function PrescribedHeatFlow(; name, T_ref = 293.15, alpha = 0.0)
80-
pars = @parameters begin
81-
T_ref = T_ref
82-
alpha = alpha
83+
@mtkmodel PrescribedHeatFlow begin
84+
@parameters begin
85+
T_ref = 293.15, [description = "Reference temperature"]
86+
alpha = 0.0, [description = "Temperature coefficient of heat flow rate"]
87+
end
88+
@components begin
89+
port = HeatPort()
90+
Q_flow = RealInput()
91+
end
92+
@equations begin
93+
port.Q_flow ~ -Q_flow.u * (1 + alpha * (port.T - T_ref))
8394
end
84-
@named port = HeatPort()
85-
@named Q_flow = RealInput()
86-
87-
eqs = [
88-
port.Q_flow ~ -Q_flow.u * (1 + alpha * (port.T - T_ref)),
89-
]
90-
ODESystem(eqs, t, [], pars; systems = [port, Q_flow], name = name)
9195
end
9296

9397
"""
@@ -104,11 +108,12 @@ the temperature at the specified value.
104108
- `port`
105109
- `RealInput` `T` input for the temperature
106110
"""
107-
@component function PrescribedTemperature(; name)
108-
@named port = HeatPort()
109-
@named T = RealInput()
110-
eqs = [
111-
port.T ~ T.u,
112-
]
113-
ODESystem(eqs, t, [], []; systems = [port, T], name = name)
111+
@mtkmodel PrescribedTemperature begin
112+
@components begin
113+
port = HeatPort()
114+
T = RealInput()
115+
end
116+
@equations begin
117+
port.T ~ T.u
118+
end
114119
end

0 commit comments

Comments
 (0)