ModelicaSpecification
ModelicaSpecification copied to clipboard
Time, modifiers and short class definitions.
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;
It is allowed in MapleSim.
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.
OpenModelica also allows it, we only forbid the use of time
inside of functions.
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()
?
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.
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.
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 modelf2()
is equivalent tof2(time)
, andtime
is available. So, I think thattime
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.