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

How to pass parameter value on the fly when calling simulate()

How to pass parameter value on the fly when calling simulate()

Code:


//HelloWorld.mo

class K
  parameter Real a = 1;
  parameter Real b = 2;
  parameter Real c = 3;
  parameter Real d = 4; 
  Real vK;
  Real vK1;
  Real vK2;
equation
  vK = vK1 * vK2;
  vK1 = a * b;
  vK2 = c * d;
end K;

If I have following mos file :

Code:


HelloWorld.mos
loadFile("HelloWorld.mo");
simulate(K, numberOfIntervals=1, outputFormat="plt");
plot(vK)

and use following command to run,

Code:


omc HelloWorld.mos

I got 24, which is correct.

How do I change parameter to the model?
For example, I want my class be defined as:

Code:


//HelloWorld.mo

class K
  parameter Real;
  parameter Real;
  parameter Real;
  parameter Real; 
  Real vK;
  Real vK1;
  Real vK2;
equation
  vK = vK1 * vK2;
  vK1 = a * b;
  vK2 = c * d;
end K;

and using something like

Code:


HelloWorld.mos
loadFile("HelloWorld.mo");
simulate(K, a=1, b=2, c=3, d=4, numberOfIntervals=1, outputFormat="plt");
plot(vK)

to run, then I can change the argument without changing my model.

I know the "simulate" function is not accepting "a=1" way of setting, that's why I'm seeking help here.

How can I pass argument like "a=10,b=20,c=30,d=40" on the fly?

Or do I need to use "input variable" rather than parameter? If that's the case, how to do that?

Thanks
Miles

Edited by: milesma - Aug-12-15 03:17:05

Re: How to pass parameter value on the fly when calling simulate()

I solved it by defining

Code:


class K
  parameter Real a;
  parameter Real b;
  parameter Real c;
  parameter Real d;
  Real vK;
  Real vK1;
  Real vK2;
equation
  vK = vK1 * vK2;
  vK1 = a * b;
  vK2 = c * d;
end K;

class KClient
    K k(a=1,b=2,c=3,d=4);
end KClient;

then call by omc HelloWorld.mos
HelloWorld.mos file

Code:


loadFile("HelloWorld.mo");
simulate(KClient, numberOfIntervals=1, outputFormat="plt");
plot(k.vK)

in the result file, I have

Code:


DataSet: time
0, 0
1, 1
1, 1

DataSet: vK
0, 24
1, 24
1, 24

DataSet: vK1
0, 2
1, 2
1, 2

DataSet: vK2
0, 12
1, 12
1, 12

This is OK, but actually, all I wanted is the result "24". How can I configure the "simulate" command options to get a single result of "24".
may I use "val()" ?

Any help is highly appreciated.

Re: How to pass parameter value on the fly when calling simulate()

Re: How to pass parameter value on the fly when calling simulate()

Thank you so much Adrian!

I just learned from this post that I could divide the "simulate" function into following commands:

Code:


loadFile("HelloWorld.mo");    getErrorString();
buildModel(K);     getErrorString();
str_cmd := "K.exe -override a=4,b=5,c=6,d=7";
system(str_cmd); getErrorString();

Then I can get "K_res.mat", that's great!

Could you help me a little bit more? What is the complete list of option settings on the built K.exe in this example?

For example, I want to specify generated file type be "plt", not "mat", how could I achieve this?
if I want to setup the "startTime" and "stopTime", how could? (I tried following command, it is not working)

Code:


str_cmd := "K.exe -override a=4,b=5,c=6,d=7 -startTime 0.0 -stopTime 10.0 -outputFormat plt";

I would assume all the parameters supported by "simulate" function should be supported by this way (calling .exe file with options), is that correct?

Re: How to pass parameter value on the fly when calling simulate()

Hi,

You can run the generated executable with:

Code:


> K -help

to get the list of options or you can run:

Code:


> omc --locale=C --help=simulation

for more info you can use

Code:


> omc --locale=C --help
> omc --locale=C --help=topics

You can use -override to set the startTime,stopTime and outputFormat.

Code:


str_cmd := "K.exe -override a=4,b=5,c=6,d=7,startTime=0.0,stopTime=10.0,outputFormat=plt";

As far as I know if you select outputFormat to plt some things might not work like val or readSimulationResult.
The best supported result file is .mat.

Why do you need .plt? You can also have .csv (comma separated values file which you can open in Excel or OpenOffice).

Cheers,
Adrian Pop/

Re: How to pass parameter value on the fly when calling simulate()

Thank you so much Adrian.

I used .plt file just because it is easy to read current/smile

I don't know how to open .mat file (open by Notepad++ showed only binaries). Any tool can open this file?

I found that you like to call "--locale=C", by reading the help, it simply said "Override the locale from the environment."

Then what are the list of valid locale values? Why is "C" special for you (or for us)?

Thanks again
Miles

Re: How to pass parameter value on the fly when calling simulate()

The list of locales depends on your operating system. The "C" locale is simply the POSIX default. It is usually (U.S.) English.

Re: How to pass parameter value on the fly when calling simulate()

Hi,

I used --locale=C because my Windows locale is in Swedish and omc has translations. If you set it to C then is the default English.
.mat files can be opened by Matlab or Octave (Matlab open-source clone).
You can also use python with the matplotlib package:
http://stackoverflow.com/questions/2078 … ith-python

I think .csv are easier to read than .plt but maybe that's subjective current/smile

Cheers,
Adrian Pop/

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