Skip to content

Commit ad27b80

Browse files
committed
docs: refactor Heat Conduction Model with @mtkmodel
1 parent 70f767c commit ad27b80

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

docs/src/tutorials/thermal_model.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,34 @@ from dividing the total initial energy in the system by the sum of the heat capa
1010
using ModelingToolkitStandardLibrary.Thermal, ModelingToolkit, OrdinaryDiffEq, Plots
1111
using ModelingToolkit: t_nounits as t
1212
13-
C1 = 15
14-
C2 = 15
15-
systems = @named begin
16-
mass1 = HeatCapacitor(C = C1, T = 373.15)
17-
mass2 = HeatCapacitor(C = C2, T = 273.15)
18-
conduction = ThermalConductor(G = 10)
19-
Tsensor1 = TemperatureSensor()
20-
Tsensor2 = TemperatureSensor()
13+
@mtkmodel HeatConductionModel begin
14+
@parameters begin
15+
C1 = 15
16+
C2 = 15
17+
end
18+
@components begin
19+
mass1 = HeatCapacitor(C = C1, T = 373.15)
20+
mass2 = HeatCapacitor(C = C2, T = 273.15)
21+
conduction = ThermalConductor(G = 10)
22+
Tsensor1 = TemperatureSensor()
23+
Tsensor2 = TemperatureSensor()
24+
end
25+
@equations begin
26+
connect(mass1.port, conduction.port_a)
27+
connect(conduction.port_b, mass2.port)
28+
connect(mass1.port, Tsensor1.port)
29+
connect(mass2.port, Tsensor2.port)
30+
end
2131
end
2232
23-
connections = [
24-
connect(mass1.port, conduction.port_a),
25-
connect(conduction.port_b, mass2.port),
26-
connect(mass1.port, Tsensor1.port),
27-
connect(mass2.port, Tsensor2.port)
28-
]
29-
30-
@named model = ODESystem(connections, t; systems)
31-
sys = structural_simplify(model)
33+
@mtkbuild sys = HeatConductionModel()
3234
prob = ODEProblem(sys, Pair[], (0, 5.0))
33-
sol = solve(prob, Tsit5())
35+
sol = solve(prob)
3436
35-
T_final_K = sol[(mass1.T * C1 + mass2.T * C2) / (C1 + C2)]
37+
T_final_K = sol[(sys.mass1.T * sys.C1 + sys.mass2.T * sys.C2) / (sys.C1 + sys.C2)]
3638
3739
plot(title = "Thermal Conduction Demonstration")
38-
plot!(sol, idxs = [mass1.T, mass2.T], labels = ["Mass 1 Temperature" "Mass 2 Temperature"])
40+
plot!(sol, idxs = [sys.mass1.T, sys.mass2.T],
41+
labels = ["Mass 1 Temperature" "Mass 2 Temperature"])
3942
plot!(sol.t, T_final_K, label = "Steady-State Temperature")
4043
```

0 commit comments

Comments
 (0)