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 call an API function in a Script loop

How to call an API function in a Script loop

Hello,

is there a way to call an API function like simulate() or setComponentModifierValue() in a loop?
My aim is to restart a simulation in OMNotebook with different initial Values.

Example:
j:=5;
for i in 1:j loop
setComponentModifierValue(Modelname, VariableInitial, Code(=i);
simulate(Modelname);
end for;

Thanks in advance,

Regards,

Marco

Re: How to call an API function in a Script loop

Maybe something like this:

Code:

loadString("model M

  parameter Real p = 3.0;
end M;
");
buildModel(M);
for i in 1:5 loop
  system("sh \"" + getInstallationDirectoryPath() + "/share/omc/scripts/replace-startValue.sh\" p " + String(i) + " M_init.xml > new_init.xml");
  j:=system("./M -f new_init.xml > log 2>&1");
  print("Output of run " + String(i) + " (exit code "+String(j) +"): " + readFile("log") + "\n");
end for;
getErrorString();

Re: How to call an API function in a Script loop

That's kind of awesome :-)
Thanks, but what for Commands are you using? I don't find them in the help()?

Re: How to call an API function in a Script loop

The syntax of for-loops is the same as for Modelica algorithm sections with the caveat that some of its semantics are a bit weird since the scripting environment does type-checking once for each execution of a statement. Unlike Modelica functions where the type is calculated only once.

This is because some of the API calls, like setComponentModifierValue (?) are part of an untyped API (they do not evaluate the arguments; expecting only literal values, etc). The typed API can be found here: https://build.openmodelica.org/Document … pting.html or by using  getDocumentationAnnotation(OpenModelica.Scripting.XXX).
As long as you used typed API you can do a lot of cool things with for-loops or reduction expressions (which are a lot faster since the expression is typed only once).

Code:

{simulate(M) for M in {

$TypeName(Modelica.Mechanics.MultiBody.Examples.Elementary.Pendulum),
$TypeName(Modelica.Mechanics.MultiBody.Examples.Elementary.DoublePendulum)
}}

Some cool examples for utilizing the scripting environment in the later versions of OpenModelica:
https://openmodelica.org/svn/OpenModeli … ursive.mos
https://openmodelica.org/svn/OpenModeli … ateDoc.mos
(user: anonymous, password (4 letters): none)

Re: How to call an API function in a Script loop

Thanks a lot, I see, I still have a long way to go!

For example the $TypeName and the system() Command are not easy to understand. Even with the examples in Dr.Modelica and with the Book of P.Fritzen it's pretty hard to get all the symantics of the Language.

So if you are working with the script you are using a lot of stuff which isn't obvious for me.
It seems to me you are working a lot with the API system(). But where can I find the Commands which I can use with this expression? They are not documented.
And which meaning has the $ in front of Typename?

Excuse me for this annoying questions but like I said it's not easy to get from the Documentation..

Re: How to call an API function in a Script loop

https://build.openmodelica.org/Document … ystem.html
http://linux.die.net/man/3/system
Any command you can run from command-line, like cat, grep, sed and awk

$TypeName() is an OpenModelica code quoting construct.

Re: How to call an API function in a Script loop

Thanks Hero :-)

Now I get it with system, very usefull !!

Re: How to call an API function in a Script loop

Hello,

unfortunately I'm still working on this Problem.

How to set new Initial Values?
Your suggestion doesn't work, I think there's a problem with the replace-startValue.sh.

In the Documentary is the script descriped by:
#usage: ./replace-startValue.sh variableName variableStartValue Model_init.xml

Now an Example with your Model:

Code:


loadString("model M

  parameter Real p = 3.0;

end M;

");

buildModel(M);
system("sh C:/OpenModelica1.8.1/share/omc/scripts/replace-startValue.sh p 10 M_init.xml");

This should be enough to set the initial value of p=10 or not? In OMNotebook it doesn't work, even your original Code doesn't work.

Thank you very much for Informations.

Re: How to call an API function in a Script loop

Hi,

Upgrade to the latest build:
https://build.openmodelica.org/omc/buil … ly-builds/
and you can use the new facility I developed for the OpenTurns connection:
Model -override x=v1,y=v2,z=v3
this will read the Model_init.xml file, override the variables on the command line with the new values, then run the simulation.
Run ./Model -help for more info

I also fixed some issues with replace-startValue.bat,/sh so you can run them better now.
Are you on Windows? Then use system("cmd /C C:/OpenModelica1.8.1/share/omc/scripts/replace-startValue.bat p 10 M_init.xml > New_M_init.xml");

Cheers,
Adrian Pop/

Re: How to call an API function in a Script loop

Hello Developers,

I tried to use the override command in different ways now and it doesn't work.
To run the simulation by the call system("Modelname"), works fine of course.
To run the simulation by the call  system("Modelname -override startTime=0, stopTime=0.1, numberofIntervals=5"); will make a new result file with the default startTime, stopTime, numberofintervalls.

The intended changes are not executed!!!
startTime, stopTime, numberofintervalls is on default!!

By the way to change the Variables by replace start Value works more or les fine.
I have to define the Variable in the model as Input to make a usefull change.
Else the value of the variable is changed in the Result file, but the equation which uses this variable will be compute with the original value of the Variable defined in the model.

Skript2 is my modelname.
o:=2;
system("cmd /C C:/OpenModelica1.8.1/share/omc/scripts/replace-startValue.bat X" + String(o) + " Skript2_init.xml > new_Skript2_init.xml");
system("cp new_Skript2_init.xml Skript2_init.xml");
system("Skript2")
And this solution doesn't work for the simulation command because it will compile again (It took a little to get that)

Regards,

Marco

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