OpenModelica


Forgot password? | Forgot username? | Register

How to perform a parameter sweep

How to perform a parameter sweep

Hi,
I have a very simple question, but I think that I got stock in the way modelica has the be used. Presumably, someone can give me a hint:

I have a simple hello world class:
class Mode
  parameter Real a=-1;
  Real x(start=1);
equation
  der(x)=a*x;
end Mode;

and I want to generate a plot, were the value of x(t=T_end) as a function of is plotted.

Is there a simple solution?

Best Regards

Administrator has disabled public posting. Please login or register in order to proceed.

Re: How to perform a parameter sweep

Do you mean you want to variate parameter a and gather all the results for x at the end of each simulation with the new value of a?

Administrator has disabled public posting. Please login or register in order to proceed.

Re: How to perform a parameter sweep

Hi again,

I made a script that does parameter sweep:

Code:


loadString("
class Model
  parameter Real a=-1;
  Real x(start=1);
equation
  der(x) = a*x;
end Model;
"); getErrorString();
buildModel(Model); getErrorString();
results := "time,x,\n";
for a in {-2, -1, 1, 2} loop
  str_a := String(a); getErrorString();
  str_cmd := "Model.exe -override a=" + str_a;
  print(str_cmd + "\n"); getErrorString();
  system(str_cmd, "output.txt"); getErrorString();
  res := readSimulationResult("Model_res.mat", x); getErrorString();
  results := results + str_a + "," + String(res[size(res, 1), size(res, 2)]) + ",\n";
  closeSimulationResultFile();
end for;
print(results); getErrorString();
writeFile("results.csv", results);
plot(x, true, "results.csv");

Running the script from command line gets you:

Code:


adrpo@ida-liu050 ~/dev/OMTesting/forum/1670
$ /f/OpenModelica1.9.3-v1.9.3-dev-523-g1d963ac/bin/omc +locale=C sweep.mos
true
""
{"c:/bin/cygwin/home/adrpo/dev/OMTesting/forum/1670/Model","Model_init.xml"}
"Warning: The initial conditions are not fully specified. Use +d=initialization for more information.
"
"time,x,
"
Model.exe -override a=-2
Model.exe -override a=-1
Model.exe -override a=1
Model.exe -override a=2

time,x,
-2,0.135336,
-1,0.367879,
1,2.71829,
2,7.38909,

""
true
true

I used the values of a as the time variable and plotted x at the end of the simulation for each a.


https://openmodelica.org/images/agorapro/attachments/118/mini_plot.png

Attachments:
Administrator has disabled public posting. Please login or register in order to proceed.

Re: How to perform a parameter sweep

Hi Adrian,

Definitely five start post current/smile

I have a question about this line:

results := results + str_a + "," + String(res[size(res, 1), size(res, 2)]) + ",\n";

I know "res" is a matrix, then I guess this line returning the "bottom right" number of the matrix, is that correct?

Code:


res[size(res, 1), size(res, 2)]

If so, is this a common way to get value of a variable regardless of time?
Or (ask in another way), in what scenario do we use this code?

Thanks again.

Edited by: milesma - Aug-12-15 05:28:14
Administrator has disabled public posting. Please login or register in order to proceed.

Re: How to perform a parameter sweep

Hi,

Yes, I use:

Code:


res[size(res, 1), size(res, 2)]

to get the last value of the variable x as in this case I build a matrix: time, x.

I don't know if is a common way to get the value regardless of time but it works in this case as I know the matrix.
If you don't know how many variables are in the matrix then is better to use val(variable, time).

Cheers,
Adrian Pop/

Administrator has disabled public posting. Please login or register in order to proceed.

Re: How to perform a parameter sweep

the command readSimulationResult is VERY useful.
The example from adrpo shows how to use it effectively.
The explanation in: https://build.openmodelica.org/Document … pting.html
is instead cryptic. Maybe a description of the parameters to be passed and an example can be added?
I mean, something like the explanation given in plotParametric().

Edited by: ceraolo - Aug-18-15 19:13:58
Administrator has disabled public posting. Please login or register in order to proceed.

Re: How to perform a parameter sweep

Hello everyone,

I want to know how we can do a sweep parameter and save results in different csv files.
For example, I have a RC model and I want to change the value of R and save results of the simulation in a csv file.

I tried with a mos script but it didn't work.

Thank you

Nasrou

Administrator has disabled public posting. Please login or register in order to proceed.

Re: How to perform a parameter sweep

You could do:

Code:


buildModel(Model, outputFormat="csv"); getErrorString();

to output csv files.

Code:


Model.exe -override var1=val1 -r File1.csv
Model.exe -override var1=val2 -r File2.csv

It might work, I haven't tried it myself.

Administrator has disabled public posting. Please login or register in order to proceed.

Re: How to perform a parameter sweep

Administrator has disabled public posting. Please login or register in order to proceed.

Re: How to perform a parameter sweep

hi
  how to write script if i have already build model?and i have to sweep i block parameter and extract that values and plot graph
please help me out.

Administrator has disabled public posting. Please login or register in order to proceed.
There are 0 guests and 0 other users also viewing this topic
You are here: