Opened 12 years ago

Closed 12 years ago

#1850 closed defect (invalid)

Discrete variables are counted for over/underconstrained system

Reported by: Christoph Höger Owned by: Willi Braun
Priority: high Milestone: 1.9.0
Component: Backend Version:
Keywords: Cc:

Description

The model below (developed by Bill Jansen) is not really underconstrained, but omc claims it to be.

model Test
     Real v (start = 10, fixed = true);

     type operatingModes = enumeration(up, down);
     operatingModes operating_mode (start = operatingModes.up, fixed = true);

   equation
     if operating_mode == operatingModes.up then
       der(v) = 1;
     else
       der(v) = -1;
     end if;

end Test;

note, that adding a dummy algorithm (equation seems not to work) works around this bug:

algorithm
    when false then
        operating_mode := operatingModes.up;
    end when;

Change History (6)

comment:1 by Willi Braun, 12 years ago

Owner: changed from somebody to Willi Braun
Status: newaccepted

If you consider the following basic rule of Modelica:

Modelica Language Specification 3.3 (Page 88):

  1. The total number of equations is identical to the total “number of unknown variables” (= single assignment rule).

Of course, you need one equation for every variable, also for the discrete ones.

But a constant condition in a when-equation is really a bug.

Last edited 12 years ago by Willi Braun (previous) (diff)

in reply to:  1 ; comment:2 by Christoph Höger, 12 years ago

Replying to wbraun:

If you consider the following basic rule of Modelica:

Modelica Language Specification 3.3 (Page 88):

  1. The total number of equations is identical to the total “number of unknown variables” (= single assignment rule).

Of course, you need one equation for every variable, also for the discrete ones.

But a constant condition in a when-equation is really a bug.

The point is, that a discrete variable is never unknown (once it has a start value). A discrete variable without an equation changing it is just the same as a parameter IMO.

Additionally, if you have more than one when-equations changing a discrete variable, this is also not an overconstrained model.

To me it looks like OM should simply stop counting discrete variables in the process.

in reply to:  2 ; comment:3 by Willi Braun, 12 years ago

Replying to choeger:

Replying to wbraun:

If you consider the following basic rule of Modelica:

Modelica Language Specification 3.3 (Page 88):

  1. The total number of equations is identical to the total “number of unknown variables” (= single assignment rule).

Of course, you need one equation for every variable, also for the discrete ones.

But a constant condition in a when-equation is really a bug.

The point is, that a discrete variable is never unknown (once it has a start value). A discrete variable without an equation changing it is just the same as a parameter IMO.

A start value is not more than an initial guess for the initial values problem, so
a discrete variable is still unknown and you need an equation to describe the behaviour of this variable.


Replying to choeger:

Additionally, if you have more than one when-equations changing a discrete variable, this is also not an overconstrained model.

This would conflict with the single assignment rules, since you can't prioritise the equations
when more than one is activated. The specification says the following:

Modelica Language Specification 3.3 (Page 85):

  • Two when-equations may *not* define the same variable.


Replying to choeger:

To me it looks like OM should simply stop counting discrete variables in the process.

No, we can't stop the counting of discrete variables, since otherwise we can't ensure
the single assignment rule.

in reply to:  3 ; comment:4 by Christoph Höger, 12 years ago

Replying to wbraun:

A start value is not more than an initial guess for the initial values problem, so
a discrete variable is still unknown and you need an equation to describe the behaviour of this variable.

A start value is the same as an initial equation, if fixed=true.

This would conflict with the single assignment rules, since you can't prioritise the equations
when more than one is activated. The specification says the following:

Modelica Language Specification 3.3 (Page 85):

  • Two when-equations may *not* define the same variable.

The point is that omc counts when equations regardless of being activated or not. In my example, the when equation is never activated, but counted. If I had two such equations, omc would complain again. This is pointless. A discrete variable needs an initial value. After there is one initial value, only the single assignment rule needs to be validated (which can only be done at runtime). During model instantiation there is simply nothing to do with discrete equations.

Replying to choeger:

To me it looks like OM should simply stop counting discrete variables in the process.

No, we can't stop the counting of discrete variables, since otherwise we can't ensure
the single assignment rule.

You cannot ensure the single assignment rule during instantiation at all. This is because you cannot check if two when equations are active at the same time. You have to simulate and see what happens. If two conflicting when equations are active, this is, of course, a runtime error.

in reply to:  4 comment:5 by Willi Braun, 12 years ago

Replying to choeger:

Replying to wbraun:

A start value is not more than an initial guess for the initial values problem, so
a discrete variable is still unknown and you need an equation to describe the behaviour of this variable.

A start value is the same as an initial equation, if fixed=true.

Okay, but then it's still just an initial equation and not an
ordinary equation, that is needed for the simulation.


This would conflict with the single assignment rules, since you can't prioritise the equations
when more than one is activated. The specification says the following:

Modelica Language Specification 3.3 (Page 85):

  • Two when-equations may *not* define the same variable.

The point is that omc counts when equations regardless of being activated or not. In my example, the when equation is never activated, but counted. If I had two such equations, omc would complain again. This is pointless. A discrete variable needs an initial value. After there is one initial value, only the single assignment rule needs to be validated (which can only be done at runtime). During model instantiation there is simply nothing to do with discrete equations.

No, the point is that the specification is quite clear at that point. It's not allowed
to define more than one when equation for one variable. It doesn't matter if the equation
is never activated, this fact can be checked without the runtime as we do it.

Where does the specification defines that an discrete variable needs an initial value?


Replying to choeger:

To me it looks like OM should simply stop counting discrete variables in the process.

No, we can't stop the counting of discrete variables, since otherwise we can't ensure
the single assignment rule.

You cannot ensure the single assignment rule during instantiation at all. This is because you cannot check if two when equations are active at the same time. You have to simulate and see what happens. If two conflicting when equations are active, this is, of course, a runtime error.

But we do it after the instantiation, where we need to ensure the single assignment rule for a correct matching. The case you are taking about is explicit excluded by the specification.

comment:6 by Christoph Höger, 12 years ago

Resolution: invalid
Status: acceptedclosed

I just read 8.3.5.3 in the spec:

Two when-equations may not define the same variable.

So, yes, omc is right about rejecting the model above. Although I still think this is a unsubstantiated restriction and will open a language ticket about this.

Note: See TracTickets for help on using tickets.