Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

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

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/

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

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/

Re: keep modelinstances in an array

It works now, I had an error in my code.
Thank you!

There are 0 guests and 0 other users also viewing this topic
You are here: