initialization of variable in pre in Modelica

1k Views Asked by At

I wrote the codes in Modelica as below:

model TestIniitial
  extends Modelica.Icons.Example;
  parameter Integer nWri= 2;
  Real u[nWri](each start= 10, fixed=false);
  Real uPre[nWri];
  parameter Real _uStart[nWri] = fill(10, nWri);
  parameter Modelica.SIunits.Time startTime = 0;
  parameter Modelica.SIunits.Time samplePeriod = 1;
  Boolean sampleTrigger "True, if sample time instant";
initial equation 
  u[1] = 1;
  u[2] = 2;
equation 
   sampleTrigger = sample(startTime, samplePeriod);

  when sampleTrigger then
    for i in 1: nWri loop
      uPre[i] = pre(u[i]);
    end for;
  end when;

  for i in 1:nWri loop
    u[i] = (i+1)*time;
  end for;

end TestIniitial;

Basically I want to initialize the u before simulation. However, I got below complaints(the initialization of u is over-specified) from translation:

The Modelica Language Specification 3.2.1 specifies that if a real variable, v,
is appearing in an expression as pre(v), but not assigned by a when equation,
then the equation v = pre(v) should be added to the initialization problem.

For this problem the following equations were added:
u[1] = pre(u[1]);
u[2] = pre(u[2]);

I can't understand the complaints since pre(v) was assigned in when equation already. What can I do if I want to initialize the u in above codes?

Thanks.

1

There are 1 best solutions below

3
On

Looking at this, my guess is that the error message is trying to provide you some diagnostics but it is incorrect about the source. I suspect (again, I do not know for sure) that it sees the fact that pre(u) appears in the model and that there is an initialization problem and assumes a specific issue.

My guess is that the issue stems from the fact that you have fixed=true set on u. I see no reason to do that and my guess is that it will lead to too many constraints on the initialization problem as well. Get rid of the fixed=true and see what happens. Report back if that doesn't address the problem.

Good luck.