modelica / fmi-standard

Specification of the Functional Mock-up Interface (FMI)
https://fmi-standard.org/
Other
274 stars 85 forks source link

Is Feedthrough supported in HCS, SCS? #793

Closed clagms closed 4 years ago

clagms commented 4 years ago

This issue is an adaptation of https://github.com/modelica/fmi-standard/issues/623#issuecomment-536629923 for fmi3.

First we need a definition of feedthrough.

Given an Co-simulation FMU instance $c$ , the input $u_c$ feeds-through to output $y_c$ when there exist two different values $v_1, v_2$ and FMU state $s_c$, such that $$get_c(set_c(s_c, u_c, v_1), y_c) \neq get_c(set_c(s_c, u_c, v_2), y_c)$$

where:

Here are some properties of the feed-through:

Note that this definition of feed-through is well accepted, and it means the exact same thing as "algebraic dependency between y_c and u_c".

Also note that any ODE that has an output equation of the form: $$\dot{x} = F(x, u)$$ $$y = G(x, u)$$

has feed-through.

This definition is therefore consistent with [Kubler2000], that the fmi3 document claims to be consistent with.

Now let's look at the the FMI text regarding feedthrough.

Right at the beginning, it says:

Support many and nested FMUs: A simulator may run many FMUs in a single simulation run and/or multiple instances of one FMU. The inputs and outputs of these FMUs can be connected with direct feedthrough. Moreover, an FMU may contain nested FMUs.

Setting aside the fact that "direct feedthrough" is redundant with "feedthrough", this induces the reader into thinking that FMI CS supports feedthrough. I would argue that supporting feedthrough is a necessary feature to export Modelica models, in

Expressivity: The FMI provides the necessary features that Modelica®, Simulink® and SIMPACK® models1 can be transformed to an FMU.

Then, in the definition of the causality attribute, the words algebraic relationship are used:

"output": The variable value can be used by another model or slave. The algebraic relationship to the inputs is defined via the dependencies attribute of <fmiModelDescription><ModelStructure><Outputs><Unknown>.

Leading the reader to believe that any dependency declared in the ModelStructure, is a feedthrough dependency, and therefore satisfies the definition I put above.

In fact, in the definition of the ModelStructure, it says:

Attribute dependencies defines the dependencies of the outputs from the knowns at the current super dense time instant in Event and in Continuous-Time Mode (ModelExchange) and at the current Communication Point (CoSimulation).

Again, the reader might think that this means there is an algebraic (an instant) dependency between knowns and unknowns.

Thankfully, in the mathematical definition of the FMU for co-simulation, we observe:

1569853059558

Which tells the reader that there is no feedthrough in that model (i.e., the output $y{i+1}$ does not depend algebraically on the input $u{i+1}$).

However, the text immediately after says

Definition (4.1) is consistent with the definition of co-simulation by (Kübler, Schiehlen 2000)

which is not true. Equation (4.1) clearly forbids feedthrough, and [Kubler2000] has the equation:

1569853204517

(apologies for the scribbles).

This equation clearly shows the feed-through.

Moreover, later in the standard, there is a remark:

There is the additional restriction in \emph{slaveInitialized} state that it is not allowed to call fmi2GetXXX functions after fmi2SetXXX functions without an fmi2DoStep call in between

Which makes it clear that feedthrough is not supported, which is also claimed in https://github.com/modelica/fmi-standard/issues/117#issuecomment-430378201.

If indeed the intent is not to support feedthrough in the FMI co-simulation, then I suggest that the parts I've highlighted above be rewritten to avoid giving this idea. On the other hand, feedthrough support can be standardized fairly easily (unless I don't see the full picture) by just re-defining the meaning the of the ModelStructure. If a tool provider does not want to implement feedthrough, he can just specify in the model structure that every output variable is independent of every input variable. If an FMU supports feedthrough, then the burden is on the master algorithm to comply to that restriction. This is also not hard to do (see paper below, or contact me for details).

What should we go for? Feedthrough or no feedthrough. I have a list of sections in the document that are affected by this change.

Some examples:

I hope this helps! :)

PS - This issue has been discussed with, and approved by @CThuleHansen .

Refs:

[Kubler2000] - Kübler, R., and W. Schiehlen. 2000. “Two Methods of Simulator Coupling.” Mathematical and Computer Modelling of Dynamical Systems 6 (2): 93–113. https://doi.org/10.1076/1387-3954(200006)6:2;1-M;FT093.

Gomes, Cláudio, Casper Thule, Levi Lucio, Hans Vangheluwe, and Peter Gorm Larsen. 2019. “Generation of Co-Simulation Algorithms Subject to Simulator Contracts.” In 3rd Workshop on Formal Co-Simulation of Cyber-Physical Systems, to be published. Oslo, Norway. Download: cosimcpspaper.pdf

pmai commented 4 years ago

Mu.

Or in longer prose: Given your definition of feedthrough, the question whether FMI 2.0/3.0 co-simulation supports it, trivially cannot be answered since the operations you use are not defined in FMI, and you do not specify - in the definition - whether time may elapse during the combination of get_c(set_c()) or not.

There is no way to update (detectibly) the state of a co-simulation FMU without advancing the time, except for initialization mode.

If time passage is allowed (i.e. a call to fmi3DoStep), then trivially FMI co-simulation supports your definition of feedthrough, if not, then it trivially does not (trivially, because h may not be 0, and thus your inability to query the new state).

So I rather think a different question would have to be asked and discussed, e.g. as discussed in #117.

In the rest of this issue, it seems to me you are sometimes reading generic parts of the standard, which apply to model-exchange as well as to co-simulation, only with co-simulation semantics in focus. Similarly the distinction between model feedthrough and its handling at FMU level seems important.

I would support a PR (or multiple PRs) cleaning up the text to clearly state which definition of feedthrough of what (model vs. FMU) is meant in each place, and clarifying whether a statement applies to ME, CS, or both (and/or the new stuff as well).

As an aside: Any changes to the computational model of FMI for co-simulation would require a full-blown FCP.

clagms commented 4 years ago

Dear @pmai I'm trying my best to detect ambiguities in the standard.

I did not understand that "Mu." means.

Given your definition of feedthrough, the question whether FMI 2.0/3.0 co-simulation supports it, trivially cannot be answered since the operations you use are not defined in FMI, and you do not specify - in the definition - whether time may elapse during the combination of get_c(set_c()) or not.

I'm sorry I did not clarify what these operations do. They are abstractions of the operation in the FMI standard, and not proposals for new operations. The operations make explicit the internal state of the FMU so that the reader is able to tell whether there is a change in the state or not. These are functions in the mathematical sense (i.e., free of side effects). They are inspired by the definition advanced by Broman et al in this paper:

Time is not allowed to pass in the definition of feedthrough. I must stress that this is not my definition of feedthrough. This is just a way to explain what it is wrt to the FMI functions.

If your claim that the FMI standard does not support feedthrough is the position of the FMI standard, then I can propose a few changes to make this clearer to the reader.

Feed-through is pretty common in co-simulation of physical systems. See, for example, the following paper by Martin Arnold, where feedthrough is discussed, and methods are proposed to deal with it:

See also the survey we did on methods for co-simulation (in and outside FMI): -Gomes, Cláudio, Casper Thule, David Broman, Peter Gorm Larsen, and Hans Vangheluwe. “Co-Simulation: A Survey.” ACM Computing Surveys 51, no. 3 (2018): Article 49. https://doi.org/10.1145/3179993.

pmai commented 4 years ago

What I’m saying is that it does not make sense to formulate mathematical functions that trivially don’t match the functions that FMI supports when trying to determine properties of the standard (this goes for all standards). A standard provides a set of axioms, replacing them with different ones is never interesting in that regard.

As to feed through: A model obviously can have feed through in the classical sense, but you will not be able to differentiate that from models without feed through via the FMI co-simulation Interface because there is no access to the model outputs and state after setting the inputs without an intervening fmi3DoStep call, which causes time to advance at least an epsilon.

Which is one reason one might want to inform the master about such “internal” feed through in some other way...

Note that I’m not saying that this is the be all and end all of co-simulation, and that not having other paradigms for co-simulation might not be interesting, but I think discussion of those in the context of FMI have take the definitions of FMI into account, rather that making use of external definitions.

I.e. I would find a discussion along the lines of #117 more helpful...

Another thing I agree we might want to do is throw out all references to scientific papers (at least in the normative section), since they seem to be causing more harm than good (by causing people to place more weight on the whole content of those papers than what is explicitly written in the standard).

nickbattle commented 4 years ago

I did not understand that "Mu." means.

https://en.wikipedia.org/wiki/MU_puzzle ?

andreas-junghanns commented 4 years ago

I did not understand that "Mu." means. https://en.wikipedia.org/wiki/MU_puzzle ?

No, but: https://en.wikipedia.org/wiki/Mu_(negative)

chrbertsch commented 4 years ago

Do we talk here about (direct) feedthrough in the Co-Simulation (CS) or Hybrid-Co-Simulation (HCS) interface?

My understanding is the following:

Is my understanding correct?

Then we should focus on what has to clarified to avoid confusion

CThuleHansen commented 4 years ago

Also remove from guiding ideas - i.e.

Support many and nested FMUs A simulator may run many FMUs in a single simulation run and/or multiple instances of one FMU. The inputs and outputs of these FMUs can be connected with direct feedthrough. Moreover, an FMU may contain nested FMUs.

chrbertsch commented 4 years ago

Also remove from guiding ideas - i.e.

Support many and nested FMUs A simulator may run many FMUs in a single simulation run and/or multiple instances of one FMU. The inputs and outputs of these FMUs can be connected with direct feedthrough. Moreover, an FMU may contain nested FMUs.

I do not agree. I do not think that all requirements have to be fulfilled by all FMU types. And ME, HCS (and SES?) support direct feedthrough

CThuleHansen commented 4 years ago

So after reading the specification again I see the following: HCS supports feedthrough But it comes with the following implications:

  1. There is no way of marking in the XML whether there is an internal (input-output) feedthrough relation. This makes it unnecessarily difficult to verify the specification of a scenario. I see no reason for this.
  2. Is it required to implement the entire hybrid co-simulation interface to support this feature?

In general, I find the mix of co-simulation with early return, hybrid co-simulation, hybrid co-simulation with clock support confusing. Maybe worth a (small) group discussion...

chrbertsch commented 4 years ago

@CThuleHansen and @clagms: could you please propose a clarification/possible improvement?

CThuleHansen commented 4 years ago

@chrbertsch unfortunately we did not have time to cover this at the FMI design meeting in Berlin. We have discussed with @APillekeit that he will present the difference between clocks and events at the next fmi design WEB meeting.

I think we need this information before we can provide a sufficient clarification and possible improvement.

chrbertsch commented 4 years ago

How can we go on with this? @IZacharias : could you perhaps take up from @APillekeit regarding "the difference between clocks and events?"

chrbertsch commented 4 years ago

@clagms and @CThuleHansen : could you please also add "(direct) Feedthrough" to the glossary? (see https://github.com/modelica/fmi-standard/pull/962#issuecomment-623358886)

andreas-junghanns commented 4 years ago

@clagms and @CThuleHansen : could you please also add "(direct) Feedthrough" to the glossary? (see #962 (comment))

I added this to clagms/glossary_model_exchange PR #941.

andreas-junghanns commented 4 years ago

The original intend of this ticket is solved. Reopen if you do not agree.

chrbertsch commented 4 years ago

What IMHO is still to be clarified:

HCS:

SCS:

It is not allowed to call fmi3ActivateModelPartition for a clockReference and clockElementIndex (i.e. valueReference of clock variable and element index into that for arrays) more than once for the same activationTime ti.

Please comment!

KarlWernersson commented 4 years ago

I don't understand why this cannot be supported for BCS just as well as HCS. The argument that i have seen is:

With IVA you can do partial support (abuse?) of this feature by asking for new input at the end of the step, only limitation becomes that the fmu will have to request it and you cannot iterate a, but if you already can do this once, the iteration shouldn't be that hard. To avoid such semi solution wouldn't proper support be better?

Or am i missing something?

chrbertsch commented 4 years ago

Isn't a basic property of FMI 2.0 CS that (typically or mandatorily?) unlike in FMI 2.0 the computations are triggered by the doStep, while in FMI2.0 ME they are triggered by fmi2getXXX. Doesn't this significantly simplify things for the exporting tools regarding an efficient implementation (not having to care much about caching etc. )

andreas-junghanns commented 4 years ago
  So why do we in https://fmi-standard.org/docs/3.0-dev/#_computation_in_scheduled_co_simulation forbid to calling `fmi3ActivateModelPartition`  several times with the same activation time?

Please comment!

I am not sure about the modelPartitions for plant models, but for vECUs I can explain this: Calling a task may advance the internal (discrete) time (like a counter) of the control algorithm (most of them have no access to "time" but only know ticks).

andreas-junghanns commented 4 years ago

With IVA you can do partial support (abuse?) of this feature by asking for new input at the end of the step, only limitation becomes that the fmu will have to request it and you cannot iterate a, but if you already can do this once, the iteration shouldn't be that hard. To avoid such semi solution wouldn't proper support be better?

Or am i missing something?

Similar to my comment in #991: intermediateUpdate is not to propagate values between FMUs - communication points are for that. So intermediateUpdate is not supposed to (and I think cannot if we describe it right) allow for direct feedthrough outside of the FMU.

I think we should keep things simple inside intermediateUpdate - if you need more information, you can always stop the doStep with earlyReturn and then properly exchange values.

The final argument that comes to mind: if you need ultimate control, do ME. xCS is supposed to be simpler. If we allow all kinds of variants and complications, we will have ME2 - and we do not need another).

chrbertsch commented 4 years ago

I am not sure about the modelPartitions for plant models, but for vECUs I can explain this: Calling a task may advance the internal (discrete) time (like a counter) of the control algorithm (most of them have no access to "time" but only know ticks).

That makes sense.

So to clarify: in HCS for an event we have the idea of super dense time, so one can iterate without advancing in time by setting inputs, calling fmi3NewDiscreteStates, getting outputs several times. We had the idea that a SCS FMU can be wrapped as a HCS FMU. But if we map a modelPartition activation to a clock to a fmi3NewDiscreteStates call, we must prevent that this could be called several times. How?

andreas-junghanns commented 4 years ago

I am not sure about the modelPartitions for plant models, but for vECUs I can explain this: Calling a task may advance the internal (discrete) time (like a counter) of the control algorithm (most of them have no access to "time" but only know ticks).

That makes sense.

So to clarify: in HCS for an event we have the idea of super dense time, so one can iterate without advancing in time by setting inputs, calling fmi3NewDiscreteStates, getting outputs several times. We had the idea that a SCS FMU can be wrapped as a HCS FMU. But if we map a modelPartition activation to a clock to a fmi3NewDiscreteStates call, we must prevent that this could be called several times. How?

You have to do the same kind of book keeping any OS scheduler has to do that works with activation flags (aka triggers, activate calls,...). OS schedulers do this implicitly when operate on priorities and resources etc. So I am sure this can be done, and should be as much as we should care about here now. Right?

chrbertsch commented 4 years ago

(Discussion Andreas J, Christian B)

In SCS, there should be no direct feedthrough.

We should create an example for an event iteration with direct feedthrough for a "Coupled clutches example" split into multiple HCS FMUs in reference FMUs. There the decision which clutch is open or not depends on the other clutch and must be iterated at an event. This will showcase the use case for direct feedthrough at events in HCS. @KarlWernersson : could you perhaps prepare such an example?

andreas-junghanns commented 4 years ago

Dsicussion during web design meeting: Karl: I will try to see if the coupled clutches will work in our old prototype. It is harder to provide 3.0 FMUs for the coupled clutches. Klaus: Please have a look at examples for 2.0 ME with a more abstract content that would likely also work in showing the ideas.

Irina: We will discuss internally if SCS can have direct feedthrough. Andreas: Please come up with an example if you have one and make it ready for the Reference FMUs.