Search code examples
modelicaopenmodelica

Too many equations for discrete state machine variables in when statements in Modelica


I have a contrived Modelica model in which a have a state machine variable manipulated by multiple when statements:

model WhenExample
  type State = enumeration(first, second, third);

  State   state;
initial equation
  state = State.first;

equation
  when sample(0, 1) then
    state = State.second;
  end when;

  when sample(0, 3) then
    state = State.third;
  end when;
end WhenExample;

When compiling under OpenModelica OMC, I get the following error:

[1] 16:46:39 Symbolic Error
Too many equations, over-determined system. The model has 2 equation(s) and 1 variable(s).

This kinda makes sense as I do have two equations for my single state variable. However, those equations only apply at discrete points in time, right?

Do I need to make sure all "manipulations" of a particular variable happen only in a single when statement?


Solution

  • See Modelica Spec, section 8.5 Events and Synchronization: https://www.modelica.org/documents/ModelicaSpec33Revision1.pdf

    Just before section 8.6 there is an example that should help you. Some code based on that is given below:

    model WhenExample
      parameter Integer multiplySample = 3;
      Boolean fastSample, slowSample;
      Integer ticks(start=0);
      type State = enumeration(first, second, third);
      State state(start = State.first);
    equation
      fastSample = sample(0,1);
    algorithm
      when fastSample then
        ticks := if pre(ticks) < multiplySample then pre(ticks)+1 else 0;
        slowSample := pre(ticks) == 0;
        state := State.second;
      end when;
      when slowSample then
        state := State.third;
      end when;
    end WhenExample;