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

Setting parameters to Modelica model using Corba interface

Setting parameters to Modelica model using Corba interface

Hi,

In my model, I would like to optimize parameters from the PI controllers by minimizing the sum of quadratic errors:
I'm currently using the JMetal package (I may develop my own algorithm in the future) and I would like to do the following thing as my optimization problem:
- Setting PI parameters (calculated by my optimization algorithm)
- Simulate the model with those parameters (it's ok for the simulation process with the simulate() function)
- Get the values from the feedback.y and feedback1.y variables that are containing the error values.
- Calculate the sum of errors using a java function (ok for that too)

I would like to know how I can set the parameters that are calculated by my algorithm to my Modelica model (PIctrl.k, PIctrl.T, Pictrl1.k and PIctrl1.T variables) and get simulation results (feedback.y and feedback1.y).

My question is the following : How can I set parameters to my modelica model using the corba interface and how can I get the simulation results contained in the feedback.y and feedback1.y variables?

Is it possible ?

Thank you in advance for your kindly reply.

Best,

Edited by: dcasner - Jun-10-11 09:54:58

Re: Setting parameters to Modelica model using Corba interface

Hi,

This is possible. Read the user and system manuals.
Basically you start OMC from Java with +d=interactiveCorba (see here: http://www.ida.liu.se/~adrpo/omc/corba/_java/ for a client) .
Then you:
1. load your model
2. send setParameterValue commands to omc to change parameters
3. simulate (maybe you would like to simulate using outputFormat = "csv" so you can read the values easier)
4. search for the Model_res.csv file, read the values
5. calculate the sum of errors
6. go to step 2 and repeat until you're done

Alternatively you can see how is done in the OMOptim client (OpenModelica SVN) which is C++, but you
can get the idea.

Cheers,
Adrian Pop/

Re: Setting parameters to Modelica model using Corba interface

Hi,

Thanks for your reply.
I tried what you said by adding the following lines:

Code:

executeCommand("setParameterValue(MyModel, PIctrl.k,"+k+")");

and in my Modelica model, I changed from

Code:

Modelica.Blocks.Continuous.PI PIctrl(k=1, T=0.01, initType=Modelica.Blocks.Type.Init.NoInit);

to

Code:

parameter Modelica.Blocks.Continuous.PI PIctrl(k=1, T=0.01, initType=Modelica.Blocks.Type.Init.NoInit);

But, when I try to simulate, it says that the model is now overdetermined: 62 unknowns and 68 equations.

Warning: Parameter PIctrl.u has neither value nor start value, and is fixed during initialization (fixed=true)
Warning: Parameter PIctrl.y has neither value nor start value, and is fixed during initialization (fixed=true)
Warning: Parameter PIctrl1.u has neither value nor start value, and is fixed during initialization (fixed=true)
Warning: Parameter PIctrl1.y has neither value nor start value, and is fixed during initialization (fixed=true)


Should I add u(start=0, fixed=false)?
Best,

Re: Setting parameters to Modelica model using Corba interface

You can also skip calculating the sum of the errors by using:
der(myErrorEstimate) = my.Variable.To.Measure);

And then just call val(myErrorEstimate,stopTime)

Re: Setting parameters to Modelica model using Corba interface

Hi,

You don't need to make Modelica.Blocks.Continuous.PI a parameter, in fact it *should not*
be a parameter as that would mean that ALL components inside it become parameters.
However, component k inside it it is a parameter already so you can change its value.

You can also change start values of variables if you want/need to via:
setComponentModifierValue(MyModel, PIctrl.x.start, $Code(=4));

See more examples of commands in:
https://openmodelica.org/svn/OpenModeli … teractive/      (all *.mos/*.mo files)
username: anonymous
pass: none     <-- write none here.
See at least files:
interactive_api_param.mos
interactive_api_attributes.mos

Cheers,
Adrian Pop/

Edited by: adrpo - Jun-10-11 17:03:36

Re: Setting parameters to Modelica model using Corba interface

Thank you very much for your reply that solved my problem.
I indeed saw that all block components were considered as parameters when I checked the "full" modelica code using the instantiate model function in OMEdit!

Now I know how I may set only a few components as parameters.

Once again, thanks.
Best,

Re: Setting parameters to Modelica model using Corba interface

You could also say list() to list the entire Abstract Syntax Tree or list(Path.ClassName) to see just parts of it.
That will dump the actual Modelica code (including the modifications you did to the parameters/variables).
For example:
list(MyModel);

You can also create the model on the fly using the available API, i.e:
createModel(MyModel); 
addComponent(name, Type, MyModel);
setComponentModiferValue(...);
...
Then simulate and read variables at any timeValue.
simulate(MyModel);
val(var, timeValue);

Cheers,
Adrian Pop/

Re: Setting parameters to Modelica model using Corba interface

There is yet another way to change parameters and that's in the init file directly.
So you just say buildModel(MyModel) instead of simulate and that will generate
MyModel.exe and MyModel_init.txt. You can read MyModel_init.txt and write it back
with the same name or a different name (say MyModel_init.changeSet1.txt) and
with different parameters, then just call MyModel.exe -f MyModel_init.changeSet1.txt
and it will read the new parameters/start value from the new init file.
This will make things much faster because simulate instantiates, generates code
and compiles from scratch.

However note that in the current /trunk we changed the Model_init.txt file to Model_init.xml
and with a different format, so if you choose this method you will have to adapt it later on
when we make a new release or nightly build.

Cheers,
Adrian Pop/

Re: Setting parameters to Modelica model using Corba interface

Thank you again for that.
I will try with the simulate function first in order to check if everything is working fine.
Than I will try it by using a txt or a xml file.
I indeed don't need to compile the model for thousand times : I just need to do it once in my class constructor !
Best,

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