Ahsan
Ahsan

Reputation: 3

Failure to handle clock inference in Dymola

The following model in Dymola gives an error,

model Test1
  Real outvar;
  Real outvarc;
  Real internal;
  parameter Real Tp=0.1;
equation 
  when Clock(Tp) then
    internal = time;
  end when;
  outvar = hold(outvarc);
algorithm 
  if (firstTick(internal)) then
    outvarc := 1;
  else
    outvarc := previous(outvarc);
    outvarc := outvarc + 1;
  end if;

end Test1;

If I modify the variable internal as follows then the model works.

model Test2
  Real outvar;
  Real outvarc;
  Boolean internal(start=true);
  parameter Real Tp=0.1;
equation 
  when Clock(Tp) then
    internal = false;
  end when;
  outvar = hold(outvarc);
algorithm 
  if (firstTick(internal)) then
    outvarc := 1;
  else
    outvarc := previous(outvarc);
    outvarc := outvarc + 1;
  end if;
end Test2;

Is there any explanation why model Test1 is giving an error?

Upvotes: 0

Views: 135

Answers (2)

Markus A.
Markus A.

Reputation: 7500

Here is a suggestion for a little more compact code of the overall model. It gives a bit different results as it uses the "discrete time" internal to make equations discrete/clocked automatically, but I guess the would be the use-case anyways (otherwise sampling time does not make a lot of sense).

model Test3
  parameter Real Tp=0.1;

  discrete Real internal;                      // "discrete" is optional in Dymola, but helps understanding
  discrete Real outvarc(start=1, fixed=true);  // setting an initial value
  Real outvar;

equation 
  internal = sample(time, Clock(Tp));    // sampling time with a locally defined clock and store it to "internal"
  outvarc = previous(outvarc)+internal;  // using a sampled/clocked variable (internal) makes the equation discrete
  outvar = hold(outvarc);                // converting to a continuous variable from a clocked one

end Test3;

Suggestions for further improvements welcome...

Upvotes: 1

Hans Olsson
Hans Olsson

Reputation: 12507

It fails in Dymola 2022x and earlier. In Dymola 2023 and later it gives the warning:

The sub clock, BaseClock_0.SubClock_1, includes pre, time, or non-clocked when, but no solver method is specified. This is not correct according to the specification and it could indicate that it was clocked by mistake. The warning can be disabled by setting Advanced.Translation.WarnForMissingSolverMethod=false;

That suggests that the simplest solution would be:

when Clock(Tp) then
  internal = sample(time);
end when;

Basically time is not a clocked variable. (Yes, I'm aware that time in clocked partitions is messier than this suggests.)

Upvotes: 1

Related Questions