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

model inheritance problem

model inheritance problem

Hi everyone,
I want to create a block which defines some physical constants that can be changed between simulations. Simple example:

model Const
    parameter Real A = 2;
end Const;

model Calc
    extends Const;
    parameter Real B = 3;
    Real C;
equation
    C = B + A;
end Calc;

So, variable C depends on parameters A and B. When I change parameter B in block Calc, the result (C) changes, but when I change the parameter A in block Const, the result (C) doesn't change. Model Calc always use the default value (2) of parameter A.

Can somebody help?

Thanks

Re: model inheritance problem

Hello maxev5

The easiest way to do it is by changing the value of those parameters in the .xml file.

Best regards
Koldo

Re: model inheritance problem

Hi Koldo,

Yes, I know I can do that in .xml file, but the block seems more elegant way to me. Do you have any ideas?

Thanks

Re: model inheritance problem

No current/sad

Re: model inheritance problem

I am not sure if the solution is this simple , but if I save the "model Const" after changing parameter A, variable C is changing as it should do.

Re: model inheritance problem

Exactly how are you changing the values between simulations? I just tried this in OMEdit by first simulating the model, then changing the parameter values in the variable browser and choosing re-simulate, and C changed as expected. This is pretty much the same as manually editing the init xml-file.

Re: model inheritance problem

I was thinking to make a block in library so I can drag it to designer window. And when I click on it there I want to change its parameters (e.g. parameter A) in the dialog window.

Re: model inheritance problem

Ok, do you mean that you want to drag Const to Calc? That would create a new instance of Const in Calc, i.e.

Code:


model Calc
  Const c;
end Calc;

Then you could change the parameters of the instance c, and then use c.A in your Calc model. Inheritance doesn't seem necessary in this case, but you could also have Calc inherit from Const as in your example. Then you would modify the inherited Const like this:

Code:


model Calc
  extends Const(A = 4);
end Calc;

I don't think there's any way of doing that via the GUI though.

Re: model inheritance problem

Yes, I know I can do it without inheritance when I have only one "Calc" model (block). But if I have e.g. 40 different "Calc" blocks and all of them use the same constant (for example density or constant A in this case), it's not practical to change that constant in 40 dialog windows one by one. That's the reason why I want one block ("Const") that defines that constant which will be used then by all 40 blocks. I know that it can be easily done in .xml file but, as I said, the block would be more elegant to me.

Re: model inheritance problem

Ok, I thought you wanted different blocks somehow. Then the question is when you want to change the parameter value, before building the simulation executable or after? I.e. do you want to be able to compile the model, simulate it, change the parameter, compile it again, etc. Or do you want to compile the model once, and then change the parameter between simulation without recompiling it?

Changing the parameter before simulation is trivial, just change Const itself. In that case you might as well just make Const a package and have A be a constant.

To change the parameter after compilation is a bit trickier, since inheritance probably won't work for large models. For example:

Code:


model Const
  parameter Real A = 2;
end Const;

model A
  extends Const;
end A;

model B
  extends Const;
end B;

model C
  A a;
  B b;
end C;

In this case a.A and b.A are different parameters, which both have to be changed in either the variable browser or the init-file. So there's no longer a single parameter that needs to be changed in this case. One solution is to define the parameter at the top level and then propagate it downwards as needed. A possibly more convenient method is to use inner/outer:

Code:


model M1
  outer parameter Real A;  // This is a reference to A higher up in the instance hierarchy.
end M1;

model M2
  inner parameter Real A = 3; // This is the actual definition of A.
  M1 m1;
end M2;

Re: model inheritance problem

Thank you very much, the inner/outer method works!

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