Description
Here is an MWE that shows this:
using DifferentialEquations, ModelingToolkit
using ModelingToolkitStandardLibrary: Thermal
@variables t
@named temp = Thermal.FixedTemperature(; T=300)
@named heatflow = Thermal.FixedHeatFlow(; Q_flow=-1.0)
@named wall = Thermal.ThermalResistor(; R=1)
eqs = [
connect(temp.port, wall.port_a),
connect(wall.port_b, heatflow.port),
]
@named test_model = compose(ODESystem(eqs, t; name=:_test), [temp, heatflow, wall])
sys = structural_simplify(test_model)
sol = solve(ODAEProblem(sys, [], (0, 1.0)))
sol[wall.Q_flow]
The last line gives the following error:
ERROR: The nonlinear solver failed with the return code MaxIters.
...
The problem stems from the temperature dependence of the heat flow implemented in Thermal.FixedHeatFlow
. observed(sys)
gives the following list for the observed variables.
julia> observed(sys)
9-element Vector{Equation}:
temp₊port₊T(t) ~ temp₊T
heatflow₊port₊Q_flow(t) ~ wall₊Q_flow(t)
wall₊port_a₊Q_flow(t) ~ wall₊Q_flow(t)
wall₊port_b₊Q_flow(t) ~ -wall₊Q_flow(t)
temp₊port₊Q_flow(t) ~ -wall₊Q_flow(t)
wall₊port_a₊T(t) ~ temp₊port₊T(t)
heatflow₊port₊T(t) ~ (-heatflow₊port₊Q_flow(t) + heatflow₊Q_flow*(-1 + heatflow₊T_ref*heatflow₊alpha)) / (heatflow₊Q_flow*heatflow₊alpha)
wall₊dT(t) ~ temp₊port₊T(t) - heatflow₊port₊T(t)
wall₊port_b₊T(t) ~ heatflow₊port₊T(t)
The culprit is the 7th equation in which alpha
is in denominator and for constant heat source is zero. Also if the source is modeling an insulated point where Q_flow = 0
, the same symptom occurs regardless of the value of alpha
.
The remedy is to correctly define the Thermal.FixedHeatFlow
equation as port.Q_flow ~ -Q_flow
and ditch the temperature dependence, which is meaningless for a fixed heat flow source anyway. Note that the same argument applies to Thermal.PrescribedHeatFlow
for which the correct equation should be port.Q_flow ~ -Q_flow.u
.
PS. I know that Modelica Standard Library defines the above sources the same way that they are currently implemented in ModelingToolkit Standard Library, but I think the correct way to define them is as discussed above. When a temperature dependent heat flow source is desired, it can be achieved by combining Thermal.PrescribedHeatFlow
, Thermal.TemperatureSensor
and some function block.