Opened 14 years ago

Last modified 14 years ago

#1297 closed task (invalid)

Flattening of functions without output does not work

Reported by: Jan Brugård Owned by: Jan Brugård
Priority: low Milestone: Red October
Component: Frontend Version:
Keywords: Cc: Jan Brugård, Adrian Pop

Description

If a function without output is used within an if clause in an equation clause flattening fails, see example below.

function noOutput
  input Real X;
end noOutput;

model Model1
  Real x(start=1);
equation
  x=der(x);
  if time>10 then
    noOutput(x);
  end if;
end Model1;

This type of functions is often used, e.g. by the Visualization library, to set different environment variables as in this example:

function setParticleEffect
  input Visualization.Internal.VisShapeID ID;
  input Real pos[3];
  input Real T[6];
  input Real scale[3];
  input Real intensity;
  input Integer effectType;
  input Real particleSize;
  input Real particleLifeTime;
  input Real wind[3];

  external "C" SetParticleEffect(ID,pos,T,scale,intensity,effectType,particleSize,particleLifeTime,wind) ;

end setParticleEffect;}}}

From MathCore: http://intranet/trac/mathmodelica/ticket/2924

Change History (5)

comment:1 by Adrian Pop, 14 years ago

This is colaboration of front-end and back-end issue.
I'll look into the front-end part, but you need to be
able to handle if equations in the back-end to be able
to checkModel/simulate this.

Before the back-end OpenModelica tries to convert if equation:

if time>10 then
    noOutput(x);
end if;

into an if expression but as noOutput(x) has no output
it cannot do it, so it will keep it as an IF equation.

Besides this OpenModelica also enforces that if equations
that have non-parameter as condition need to have the same
number of equations in the then/else part. We can relax this
check so that we can pass the if-equations as they are to
the back-end where you should be able to handle them.

This check is done via:

DAEUtil.transformIfEqToExpr(daelist, constantEvalOnly) ->
DAEUtil.ifEqToExpr(element, constantEvalOnly) ->
DAEUtil.countEquationsInBranches // the check is done here

One thing to notice is that if constantEvalOnly is true
the transformation from if-equations to if-expressions
is *not done*, so the if-expression should stay the same
and be passed to the back-end as it is.

In OpenModelica, the call to DAEUtil.transformIfEqToExpr(list, false)
is done in CevalScript.mo.

As far as I know the MathModelica kernel calls:

DAEUtil.transformIfEqToExpr(daelist, true)

in your own CevalScript.mo which is different from ours.
so it should actually work fine for you. That of course
if you can handle the if-equations it in the back-end.

comment:2 by Adrian Pop, 14 years ago

Jan, could you tell me what is the actual error that you get, i.e. error message?

I really thing this should work for you as we do this transformation as the last
step before the back-end, and I really think you don't do this transformation at all.

comment:3 by Jan Brugård, 14 years ago

I get the following error in MathModelica

[25] 14:08:07 Validation of class Model1
Error: In equation equation if time > 10.0 then noOutput(x); end if;. If-equation with conditions that are not parameter expressions must have an else branch, in equation.

Check of Model1 failed.
Error
Class Model1 has 1 equation(s) and 1 variable(s).
0 of these are trivial equation(s)

If I change the function so it returns a value:

function hasOutput
  input Real X;
 output Boolean y=true;
end hasOutput;

model Model1
  Real x(start=1);
Boolean y;
equation
  x=der(x);
  if time>10 then
    y=hasOutput(x);
else
y=false;
  end if;
end Model1;

it works

[27] 14:11:33 Validation of class Model1
Check of Model1 completed successfully.
Class Model1 has 2 equation(s) and 2 variable(s).
0 of these are trivial equation(s).

comment:4 by Adrian Pop, 14 years ago

Then it means that somewhere in your code (before or in the back-end) you are calling:
DAEUtil.transformIfEqToExpr(list, false)
when you should call it with:
DAEUtil.transformIfEqToExpr(list, true)
You should ask one of your developers to look into this.
Most likely this is called somewhere in CevalScript.mo or your back-end.

comment:5 by Adrian Pop, 14 years ago

This is a back-end issue.

Note: See TracTickets for help on using tickets.