Question

I was wondering how the following is possible in Modelica:

suppose variables a,b
Timetable object c

    equation
    if a>c.y then
      b = f(a) // with f a mathematical function
    else
      b = g(a) // with g a mathematical function
    end if;

    der(a) = h(a,b) //with h a mathematical function

How can Modelica determine which case of the if statement is true? It cannot compute the value of "a" without the value of "b", which is determined in the if statement.

Was it helpful?

Solution

First, note that a is state (at least based on the equations you have shown). This means that at the current time a will be known. From your description, c is also known (it is a function of time, apparently). So at any point in time, we know which branch of the if statement will be taken.

So for any given time we can compute b and therefore der(a). The real question is at what point does the condition in the if statement change.

The answer is that a "monitor" function is established (by the Modelica compiler) and when that monitor function crosses zero then the Modelica runtime will react by stopping integration at that point and then restarting with the integration (using the other branch). This is because the conditional expression in an if statement implicitly generates events.

Another way to think about this is that there is a "hidden" boolean variable that indicates whether we take on branch or the other. At first, this sounds crazy because you assume that the Modelica runtime will take the branch based on whether a>c.y but that isn't actually the case. What it does is determine the initial value of the boolean based on the value of a>c.y at the start of the simulation and then it tries to figure out when it actually changes. It doesn't really evaluate a>c.y all the time. This leads to strange situations where one branch is executed even though it shouldn't. This will happen in Modelica and is related to candidate solutions generated while the Modelica runtime is trying to determine where the event occurred.

I know it sounds confusing, but if you embrace this "hidden boolean variable" notion and understand that it doesn't change until the Modelica runtime can definitively identify the point at which the change should occur (based on some candidate solution trajectory), it all makes sense.

I hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top