Description
A lot of chemical plants have the product flowing in only one direction. Modeling those with stream
connectors is natural, but under MTK (and vanilla Modelica) it requires also specifying the backflow version of all stream quantities.
To wit, here I have one source of product flowing into two reactors, in parallel:
using ModelingToolkit, ModelingToolkitStandardLibrary
using ModelingToolkit: t_nounits as t, D_nounits as D
@connector FlowPin begin
flow(t), [connect=Flow]
conc(t), [connect=Stream]
dummy(t) # otherwise MTK won't accept it
end
@mtkmodel Source begin
@components begin
inlet = FlowPin()
end
end
@mtkmodel Reactor begin # mass-less reactor
@components begin
inlet = FlowPin()
end
@variables begin
conc(t)
end
@equations begin
conc ~ instream(inlet.conc) + 0.1
inlet.conc ~ -999999
end
end
@mtkmodel Room begin
@components begin
source = Source()
reactor1 = Reactor()
reactor2 = Reactor()
end
@equations begin
connect(source.inlet, reactor1.inlet)
connect(source.inlet, reactor2.inlet)
source.inlet.conc ~ 0.2
reactor1.inlet.flow ~ 2
reactor2.inlet.flow ~ 3
end
end
@mtkbuild room = Room()
prob = ODEProblem(room, [], (0, 100.0), [])
sol = solve(prob)
Without the inlet.conc ~ 9999
equation, MTK complains that it has more unknowns than equations. This is understandable, but it has been a major source of headache on our side since we've started using MTK. If a reactor operates in the forward direction, are we expected to always specify a hypothetical backflow case? Consider this simple equation (in the forward direction):
D(mass_product) ~ instream(inlet.concentration) * inlet.flow - outlet.concentration * outlet.flow
Complicating it with a physically-impossible backflow is quite depressing, not to mention error-prone. The user-side alternatives are:
inlet.conc ~ -999999
inlet.conc ~ 0
inlet.conc ~ outlet.conc
I use the first one. I sprinkle inlet.conc ~ -999999
in my components when MTK complains of a lack of equations, and I delete them when there is a surplus of equations. It usually works 😱
Proposal
connect(a.inlet, b.inlet, backflow=false)
. Then expand_connection
could ignore the backflow equations, and ideally add an assertion
that the flow is in the right direction.
Related
Regarding the concept of "back-flow can be turned-off" in the MSL: This is done in many of the components of the Fluid package. One example is the pipe in Modelica.Fluid.Examples.PumpingSystem. There you can find the parameter allowFlowReversal. This should be what you are looking for.
I am quite interested to hear about how others are approaching this. Perhaps there is a simpler solution that I missed?