ModelicaSpecification icon indicating copy to clipboard operation
ModelicaSpecification copied to clipboard

Time, modifiers and short class definitions.

Open qlambert-pro opened this issue 1 year ago • 7 comments

The specs say that:

The variable time is a built-in variable available in all models and block, ...

In light of that is the following allowed:

package P
  function f
    input Real i;
    output Real[2] y = {i, i};
  end f;

  model Base
    Real[2] y;
  end Base;

  model Test = Base(y = 2.0 * (-f(time)));
end P;

qlambert-pro avatar Nov 06 '23 10:11 qlambert-pro

It is allowed in MapleSim.

eshmoylova avatar Nov 14 '23 16:11 eshmoylova

It is allowed in MapleSim.

After looking more Dymola also accepts it.

I was first fooled by the error message for modifying y as such modifiers are restricted to parameters, inputs, and variables with default (and then there's some additional weird issue when translating).

I hadn't expected it, but I guess it makes sense.

HansOlsson avatar Nov 14 '23 16:11 HansOlsson

OpenModelica also allows it, we only forbid the use of time inside of functions.

perost avatar Nov 14 '23 16:11 perost

OpenModelica also allows it, we only forbid the use of time inside of functions.

The question of inside leads to:

package P
  function f
    input Real i;
    output Real[2] y = {i, i};
  end f;

  model Base
    Real[2] y;
  end Base;

  model Test = Base(y = 2.0 * (-f(time)));

  function f2=f(i=time);
end P;

What happens for P.f2()?

HansOlsson avatar Nov 14 '23 16:11 HansOlsson

OpenModelica also allows it, we only forbid the use of time inside of functions.

The question of inside leads to:

package P
  function f
    input Real i;
    output Real[2] y = {i, i};
  end f;

  model Base
    Real[2] y;
  end Base;

  model Test = Base(y = 2.0 * (-f(time)));

  function f2=f(i=time);
end P;

What happens for P.f2()?

The check happens after the modifiers have been applied, so it's treated the same as input Real i=time in the function and gives an error.

perost avatar Nov 14 '23 17:11 perost

The check in MapleSim is performed on the algorithm of a function, and not on the inputs.

So, there are different interpretations of what "The variable time is a built-in variable available in all models and blocks, which is treated as an input variable" means.

Arguably, f2 is not going to be used by itself. It will be used in some model or block. If used in a model f2() is equivalent to f2(time), and time is available. So, I think that time may appear in default argument specifications in functions.

eshmoylova avatar Nov 14 '23 18:11 eshmoylova

The check in MapleSim is performed on the algorithm of a function, and not on the inputs.

So, there are different interpretations of what "The variable time is a built-in variable available in all models and blocks, which is treated as an input variable" means.

Arguably, f2 is not going to be used by itself. It will be used in some model or block. If used in a model f2() is equivalent to f2(time), and time is available. So, I think that time may appear in default argument specifications in functions.

Yes, but if f2() is used inside another function then it wouldn't work, right? Saying that a function in a package can only be called in a model and not in a function seems odd to me.

For a short function defined in a model there's also the lookup of variables in the enclosing scope - but those functions are local to the model:

model A
  Real x;
  function f2=  f(i=time+x);
  function f
    input Real i;
    output Real y=i;
  algorithm 
  end f;
equation 
  der(x)=f2();
end A;

To me it would make sense that both x and time were looked up in the scope enclosing f2 - and the same logic would imply that the first model would be invalid since it tries to us time in the package scope.

HansOlsson avatar Dec 21 '23 09:12 HansOlsson