- Index
- » Programming
- » Modelica Language
- » Using inner/outer between functions...
Using inner/outer between functions and their containing classes
Using inner/outer between functions and their containing classes
Hello,
According to the Modelica language specification, functions are "specialized classes" [Ch. 12.1] with certain restrictions [Ch. 12.2]. Since the restrictions do not tell anything about inner/outer declarations, I assumed that it should be possible to access outer variables from within a function:
Code:
class myModel2
function circlePerimeter
input Real radius;
output Real perimeter;
outer Real pi; // --> case1: with inner/outer: Wrong value for pi inside function: 0
algorithm
perimeter := 2* radius * pi;
end circlePerimeter;
//Real pi = 3.1415; // --> case 2: without inner/outer: Error: Error building simulator.
inner constant Real pi = 3.1415; // --> case 1: with inner/outer: Wrong value for pi inside function: 0
Real x;
equation
x = circlePerimeter(time);
end myModel2;
Trying to do so I noticed, that an outer value is always set to 0, if adressed from within a function (case1). Just out of courisitiy I also tried to access the outer variable without declaring it as "inside" in the outer class and without declaring it anyhow in the inner class (which is the function actually). I was pretty surprised that the model can be flattened then, but the result is an g++-compilation error due to the undeclared variable (case 2).
Do I always have to pass "anything needed inside a function" as a parameter, or am I missing something here?
Regards,
Hannes
EDIT: Using 1.5.0 RC on Win7/64bit here.
- edes
- 38 Posts
Re: Using inner/outer between functions and their containing classes
Hi,
Well, well, well. Functions are a totally different kind of animal than normal classes.
If you think about it, an outer component in a function is just another input.
Truth is I never thought about using inner/outer in this fashion.
You could easily use constants from the current scope directly in the function
algorithm section (remove outer Real pi; and "inner" from your model to achieve this).
Also, you can use full qualified paths to constants in algorithm sections in functions if all
the names from the path are packages, i.e. Modelica.Constants.pi.
You could create a ticket here:
http://trac.modelica.org/Modelica/newticket
and select component Modelica Specification and ask about this.
This should be clarified in the Modelica specification, so it would
be really good if you create the ticket.
I could create the ticket myself (let me know) but it would not be
fair as credits for this should go to you.
Cheers,
Adrian Pop/
- adrpo
- 885 Posts
Re: Using inner/outer between functions and their containing classes
Man, you spend your saturday nights in front of the screen too? ...We definitively must change somthing in our lives! ...Anyway - Thanks for the answer!
Talking about constants: Shouldn't a variable which is declared with the parameter-prefix behave like one declared with the constant-prefix in this context? (It doesn't, and I accepted this already, but I still ask myself why it does not. In the language specification [Ch. 4.4.4, 1st of the 3 dots] it is described like both, parameter- and constant-declarations, would result in a constant.)
I just created that ticket.
Cheers
Hannes
- edes
- 38 Posts
Re: Using inner/outer between functions and their containing classes
Yes, parameters and constants should. But the backend can't handle it properly yet.
When you start adding parameters into the picture things get more complicated, since you can no longer constant-evaluate the function call (since it depends on some parameter that may be changed at initialization). I'm pretty sure parameters are used inside functions (see Modelica.Media), so it will be fixed sometime in the future (when we can actually flatten Modelica.Media).
- sjoelund.se
- 1700 Posts
Re: Using inner/outer between functions and their containing classes
Clear now. Thanks a lot!
- edes
- 38 Posts
Re: Using inner/outer between functions and their containing classes
To whom it may concern:
The created ticket is that one:
https://trac.modelica.org/Modelica/ticket/350
They decided, that inner/outer is NOT allowed in functions, as functions are not a part of the object-instance tree and inner/outer works only on that tree.
- Index
- » Programming
- » Modelica Language
- » Using inner/outer between functions...