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

Class objects instantiation and variables assignment in functions

Class objects instantiation and variables assignment in functions

I’m a engineer of Porto University and I’m doing a computer network simulation in Open Modelica and I’m facing some hard issues.
When a create a function in order to assign class variables and return a object that I’ve created, the compiler says that the object is not declared and I’m sure that it is because I declare this output as follow: output class_name object_name;
Can I create functions to assign an object attributes or parameters? If yes how can I do it? (a simple example would be enough).
If I change the class_name by Real it works but I really want to return a specific class object.
The other issue that I’m facing is the encapsulation of a certain class or function in a package since as soon as I create a new class and add some code in it and save it, the class it’s removed out of the package in OM edit 3.1.
How can I aggregate several tasks in one function that creates instances and assigns the attributes or parameters?
Can anybody tell me how can I create functions to assign class attributes and instantiate class objects so the code can be easily used by another person?
I’ve already created my network nodes model and it works but now I’m trying to create functions to set all the class objects and assign the attributes values.
I’m looking forward to participate in Modelica community.

Many thanks in advance.




Kind regards,
João Santos

Re: Class objects instantiation and variables assignment in functions

In the Modelica 3.3 specification it will be clear that you cannot use models in functions: "A function may only contain components of restricted class type, record, operator record, and function; i.e. no model or block components."

This was always the case but not clearly stated. You are expected to extend a class and set the attributes in the modifier of the extends, i.e.:

Code:

model M

  extends N(p=1.5);
end M;

or directly as a component:

Code:

model M

  N n(p=1.5);
end M;

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