Skip to content

RealOutputs for thermal sensors #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 14 additions & 29 deletions src/Thermal/HeatTransfer/sensors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ This is an ideal absolute temperature sensor which returns the temperature of th
signal. The sensor itself has no thermal interaction with whatever it is connected to. Furthermore, no thermocouple-like
lags are associated with this sensor model.

# States:

- `T(t)`: [`K`] Absolute temperature

# Connectors:

- `port`
- `port`: [HeatPort](@ref) Thermal port from which sensor information shall be measured
- `T`: [RealOutput](@ref) [K] Absolute temperature of port
"""
@mtkmodel TemperatureSensor begin
@components begin
port = HeatPort()
end
@variables begin
T(t)
T = RealOutput()
end
@equations begin
T ~ port.T
T.u ~ port.T
port.Q_flow ~ 0
end
end
Expand All @@ -36,25 +31,20 @@ Relative Temperature sensor.
The relative temperature `port_a.T - port_b.T` is determined between the two ports of this component and is provided as
output signal in kelvin.

# States:

- `T(t)`: [`K`] Relative temperature `a.T - b.T`

# Connectors:

- `port_a`
- `port_b`
- `port_a`: [HeatPort](@ref) Thermal port from which sensor information shall be measured
- `port_b`: [HeatPort](@ref) Thermal port from which sensor information shall be measured
- `T`: [RealOutput](@ref) [K] Relative temperature `a.T - b.T`
"""
@mtkmodel RelativeTemperatureSensor begin
@components begin
port_a = HeatPort()
port_b = HeatPort()
end
@variables begin
T(t)
T = RealOutput()
end
@equations begin
T ~ port_a.T - port_b.T
T.u ~ port_a.T - port_b.T
port_a.Q_flow ~ 0
port_b.Q_flow ~ 0
end
Expand All @@ -70,26 +60,21 @@ is the amount that passes through this sensor while keeping the temperature drop
model, so it does not absorb any energy, and it has no direct effect on the thermal response of a system it is included in.
The output signal is positive, if the heat flows from `port_a` to `port_b`.

# States:

- `Q_flow(t)`: [`W`] Heat flow from `port_a` to `port_b`

# Connectors:

- `port_a`
- `port_b`
- `port_a`: [HeatPort](@ref) Thermal port from which sensor information shall be measured
- `port_b`: [HeatPort](@ref) Thermal port from which sensor information shall be measured
- `Q_flow`: [RealOutput](@ref) [W] Heat flow from `port_a` to `port_b`
"""
@mtkmodel HeatFlowSensor begin
@components begin
port_a = HeatPort()
port_b = HeatPort()
end
@variables begin
Q_flow(t)
Q_flow = RealOutput()
end
@equations begin
port_a.T ~ port_b.T
port_a.Q_flow + port_b.Q_flow ~ 0
Q_flow ~ port_a.Q_flow
Q_flow.u ~ port_a.Q_flow
end
end
10 changes: 5 additions & 5 deletions test/Thermal/motor.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ModelingToolkit, OrdinaryDiffEqDefault, Test
using ModelingToolkit, Test
using ModelingToolkitStandardLibrary.Thermal
using ModelingToolkitStandardLibrary.Blocks

Expand Down Expand Up @@ -47,10 +47,10 @@ using ModelingToolkitStandardLibrary.Blocks

# plot(sol; vars=[T_winding.T, T_core.T])
@test SciMLBase.successful_retcode(sol)
@test sol[motor.T_winding.T] == sol[motor.winding.T]
@test sol[motor.T_core.T] == sol[motor.core.T]
@test sol[motor.T_winding.T.u] == sol[motor.winding.T]
@test sol[motor.T_core.T.u] == sol[motor.core.T]
@test sol[-motor.core.port.Q_flow] ≈
sol[motor.coreLosses.port.Q_flow + motor.convection.solid.Q_flow + motor.winding2core.port_b.Q_flow]
@test sol[motor.T_winding.T][end] >= 500 # not good but better than nothing
@test sol[motor.T_core.T] <= sol[motor.T_winding.T]
@test sol[motor.T_winding.T.u][end] >= 500 # not good but better than nothing
@test sol[motor.T_core.T.u] <= sol[motor.T_winding.T.u]
end
2 changes: 1 addition & 1 deletion test/Thermal/piston.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ModelingToolkit, OrdinaryDiffEqDefault, Test
using ModelingToolkit, Test
using ModelingToolkitStandardLibrary.Thermal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the point of the removal if OrdinaryDiffEq is directly used in others?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely didn't understand some complexities here. When I instantiated based on Project.toml, I didn't get OrdinaryDiffEq in my environment at all. I manually added it to run some of the other tests (though perhaps it wasn't really needed there). But even so, the tests that had this uses annotation with OrdinaryDiffEqDefault were complaining that this package was missing So I just removed in these tests. I am definitely interested in knowing what the best way to approach would be.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use TestEnv.jl yoninstantiate the test environment, which is generally different from the package environment, notably, tests often require additional dependencies.

using ModelingToolkitStandardLibrary.Blocks

Expand Down
8 changes: 4 additions & 4 deletions test/Thermal/thermal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using OrdinaryDiffEq: ReturnCode.Success
# Check if Relative temperature sensor reads the temperature of heat capacitor
# when connected to a thermal conductor and a fixed temperature source
@test SciMLBase.successful_retcode(sol)
@test sol[reltem_sensor.T] + sol[tem_src.port.T] == sol[mass1.T] + sol[th_conductor.dT]
@test sol[reltem_sensor.T.u] + sol[tem_src.port.T] == sol[mass1.T] + sol[th_conductor.dT]

@info "Building a two-body system..."
eqs = [connect(T_sensor1.port, mass1.port, th_conductor.port_a)
Expand All @@ -49,8 +49,8 @@ using OrdinaryDiffEq: ReturnCode.Success
m1, m2 = sol.u[end]
@test m1≈m2 atol=1e-1
mass_T = reduce(hcat, sol.u)
@test sol[T_sensor1.T] == mass_T[1, :]
@test sol[T_sensor2.T] == mass_T[2, :]
@test sol[T_sensor1.T.u] == mass_T[1, :]
@test sol[T_sensor2.T.u] == mass_T[2, :]
end

# Test HeatFlowSensor, FixedHeatFlow, ThermalResistor, ThermalConductor
Expand Down Expand Up @@ -81,7 +81,7 @@ end

@test SciMLBase.successful_retcode(sol)
@test sol[th_conductor.dT] .* G == sol[th_conductor.Q_flow]
@test sol[th_conductor.Q_flow] ≈ sol[hf_sensor1.Q_flow] + sol[flow_src.port.Q_flow]
@test sol[th_conductor.Q_flow] ≈ sol[hf_sensor1.Q_flow.u] + sol[flow_src.port.Q_flow]

@test sol[mass1.T] == sol[th_resistor.port_a.T]
@test sol[th_resistor.dT] ./ R ≈ sol[th_resistor.Q_flow]
Expand Down
Loading