1

I am trying to understand the Modelica semantics for a discrete signal. Given a step signal that instantaneously transitions from 0.0 to 1.0 with infinite slope at t = 0.5. Then let's say you also have a when statement as in the following code:

model test_discrete
  Modelica.Blocks.Interfaces.RealOutput q(start = -1.0);
  Modelica.Blocks.Sources.Step step(
    height=1,
    offset=0,
    startTime=0.5)

algorithm 
  when time >= 0.5 and time <= 0.5 then
    q := step.y;
  end when;
equation 
end test_discrete;

My question is whether q will be 0.0 or 1.0? Lets assume q is initialized to -1. When I implement the code, it transitions to 1.0, but my confusion is that 0.0 would also satisfy the equation. So I am just wondering if there are any rules to prevent non-determinant behavior. If someone could help or point me to any literature, that would be greatly appreciated! Thanks for your time.

jackfrost9p
  • 243
  • 3
  • 10

2 Answers2

1

Well, a few comments. First, your code is non-deterministic. There are no assurances about the order of events that are not somehow tied to each other through a common "cause". There are features in Modelica 3.3 to make your system synchronous (which removes the ambiguities). But you'll have to read the specification for that.

But I'd also like to point out what is essentially an error in your code. You say:

when time >= 0.5 and time <= 0.5 then
  q := step.y;
end when;

My guess is that you are trying to set the value of q at the same "instant" that step.y is set (and then further wondering which gets resolved first). But this is not how a when statement works. I suspect you are trying to represent the "instant" that step.y gets set as time >= 0.5 and time <= 0.5. But that is not correct. The instant it gets set is time >= 0.5 alone. The key point here is the when is not the same as if.

To put this a bit more formally, the equation inside the when clause becomes active when the condition associated with the when clause becomes true. This is really beyond the scope of your question, but you should familiarize yourself with the differences between when and if, because this are very important semantics.

Michael Tiller
  • 9,291
  • 3
  • 26
  • 41
  • Thanks for your reply. That was my thinking as well, it is non-deterministic. Regarding the when statement, yes, I agree and understand the difference between an if statement. I am new to Modelica and was naturally thinking in terms of a traditional if statement without realizing it. Thanks for pointing it out. – jackfrost9p Aug 05 '13 at 07:38
0

My instinct tells me that you may need a pre() for the conditions, but it's just my guess.

Hang Yu
  • 476
  • 2
  • 14
  • 1
    It turns out `pre` doesn't play a role here. The `pre` operator always applies to the current event. Because of the asynchronous nature of `when` in this case, the event that sets `step.y` and the event that sets `q` are decoupled. As such, `pre(step.y)` is the same as `step.y` in this context. – Michael Tiller Aug 05 '13 at 00:59