- Index
- » Programming
- » Modelica Language
- » keep modelinstances in an array
keep modelinstances in an array
keep modelinstances in an array
Hi all,
I read that one could hold variables of any type in arrays.
suppose I have a model called ModelA which has a Real variable b and a Real parameter p.
I now want to hold several instances of such a model in an array, setting the parameter different for different instances
Is something like the following possible:
parameter Integer n=2;
//get a container
ModelA modelsInArray[n];
//fill in modelInstances
modelsInArray[1]=ModelA(p=1.2);
modelsInArray[2]=ModelA(p=1.4);
//access a variable
modelssInArray[1].b=42;
Is there something similar to c/c++ pointers and references so one could hold the references to modeinstances in an array?
Tanks
alex
- alexp
- 24 Posts
Re: keep modelinstances in an array
You can do it like this:
ModelA modelsInArray[2](p = {1.2, 1.4});
There aren't any "references" yet in Modelica, everything is static!
The arrays have predetermined size (at compile time).
Cheers,
Adrian Pop/
- adrpo
- 885 Posts
Re: keep modelinstances in an array
the instatiation
ModelA modelsInArray[2](p = {1.2, 1.4});
works.
but how about accessing the instances 'inner' variables:
ModelArray[1].b=2.0; // does not work
alex
- alexp
- 24 Posts
Re: keep modelinstances in an array
Hi,
modelsInArray[1].b should work to be accessible in the equation/algorithm sections or in the variable bindings.
Can you give a full model that doesn't work?
Cheers,
Adrian Pop/
- adrpo
- 885 Posts
Re: keep modelinstances in an array
It works now, I had an error in my code.
Thank you!
- alexp
- 24 Posts
- Index
- » Programming
- » Modelica Language
- » keep modelinstances in an array