ModiaSim / Modia.jl

Modeling and simulation of multidomain engineering systems
MIT License
321 stars 38 forks source link

Flow variables and connectors #105

Closed LeonardJohard closed 4 years ago

LeonardJohard commented 4 years ago

Flow variables with connectors are not properly documented and seems different from Modelica. For example:

@model simflow begin a = Float(flow=true) b = Float(flow=true) c = Float(flow=true) d = Float(flow=true) @equations begin connect(c,b) connect(a,b) connect(a,d) end end

result = simulate(simflow, 2.0

Sets everything to 0, although I only see three equations. I would further assume that connecting flow variables should reduce and not increase the number of equations, as the above should generate something like 0 = a + b + c + d instead of the disconnected set of a = 0, b = 0, c = 0, d = 0 if they were unconnected.

Am I missing something here?

HildingElmqvist commented 4 years ago

The flattened model is: FLATTENED MODEL

@model simflow begin
  a = Variable(T = Float64, flow = true)
  b = Variable(T = Float64, flow = true)
  c = Variable(T = Float64, flow = true)
  d = Variable(T = Float64, flow = true)
@equations begin
  a + d + c + b = 0
  a = 0
  b = 0
  c = 0
  d = 0
  end
end

since this top level model has ports with flow variables and there are no external flows. The flow variables are then set to zero by additional equations.

This set of equations are over-determined, however, so one equation is removed. The log (enabled by logTranslation=true) in the simulate command states:

REMOVE SINGULARITIES
Removed equation: d = 0

Updated equations
a + d + c + b = 0
a = 0
b = 0
c = 0

If you turn off removing singularities, you get instead:

julia> result = simulate(simflow, 2.0, logTranslation=true, removeSingularities=false)
logTranslation = true
removeSingularities = false
Log file: C:\Users\hilding.elmqvist/ModiaResults/simflow.txt

Simulating model: simflow
ERROR: The number of scalarized unknowns (= 4) is not equal to the number of scalarized equations (= 5).
If option `simulate(<model>, ...; logTranslation=true)` is set, inspect <user>/ModiaResults/<model>.txt for more info.

I hope this clarifies the behavior.

LeonardJohard commented 4 years ago

My question is then how I add a source or sink to the flow? I would like to add a = 4 and use c as an unknown sink for example.

I played around with it a bit before and failed, but will make another attempt with logs now. Thanks for the logTranslation tip! I will close this if I find a way.

HildingElmqvist commented 4 years ago

You need to introduce source and sink models, which you instantiate and connect externally to the "real" model. You can not directly externally assign to flow variables since if they are not connected externally by connect-statement, the flow is zero.

Another assumption which is made is that flow variables are always combined with potential variables in order to maintain composability by using balanced model, i.e. every model has the same number unknown variables as equations.

LeonardJohard commented 4 years ago

Ok I made this work

@model Flow1 begin f = Float(flow=true) end

@model Source begin port = Flow1() f = Parameter(1.0) @equations begin port.f = f end end

@model Sink begin port = Flow1() f = Float() @equations begin port.f = f end end

@model simflow begin a = Source() d = Sink() @equations begin connect(a.port,d.port) end end

result = simulate(simflow,logTranslation=true, 2.0)

I initially tried to set the Source parameter as flow type, which failed. As I (guesstimate) understand it now the flow attribute is only for external ports and the flows themselves also need to be a submodel, similar to connector type in Modelica.

I did not use potential in these models, as I only use these models to keep track of industrial material flows and potential has no obvious meaning in this context. Avoiding it keeps things simpler and it is still nice to be able to use the connect statements, especially when a graphical editor will be available.

There are some further subtleties in how to use flow variables that I suppose I will learn over time.