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 initialize the values of parameters with a .mos file

how to initialize the values of parameters with a .mos file

Hi,

I am a beginner with OpenModelica, so sorry if my question is a stupid one.

I am working with a Dymola model. I have a .mos file, which contains the values of variables that have to be given to OpenModelica so that it could simulate (initialization values).
I'm working with OpenModelicaShell.
I've read it was possible to run a Script  with : runScript("script_name").
Yet, when I do it, I get "failed".
I think it is because the variables of my model are not created when I run the script (because when I write getParameterNames(model_name), I get "{ }" ), but I do not know how to create them, without simulating.

To sum up, here are the commands I use :
loadModel(Modelica,{"3.1"});
loadFile("file_name")
checkModel(model_name)
translateModel(model_name)
runScript("script_name").

What do I do wrong ?

Thank you very much for your help !
Ben

Re: how to initialize the values of parameters with a .mos file

runScript() works the same as running the commands in your mos-script one by one. So you could try seeing where the error occurs. I recommend putting getErrorString() after any command.

I do not know how Dymola sets parameters before running a simulation, but in OpenModelica you can use:

Code:

simulate(model_name, simflags = "-overrideFile=fileName")

Where fileName is a file:

Code:

variable=startValue

someOtherVariable=someOtherStartValue

Re: how to initialize the values of parameters with a .mos file

Thank you very much : I didn't know this "simflags = "-overrideFile=fileName"" technique !
I get "" when I write :
getErrorString()

Is it possible to integrate this "getErrorString" command in the simulate one ?
I've tried :
simulate(model_name,simflags = "-overrideFile=Script.mos",getErrorString)
but I didn't work.

The simulation still does not work.
It may be because OpenModelica doesn't need initialization values for the same variables as Dymola.
How could I get the names of the variables which need to be given a start value so that the simulation could work ?

Re: how to initialize the values of parameters with a .mos file

Code:


simulate(ModelName, simflags="-overrideFile=file.txt"); getErrorString();

The file.txt should looks like

Code:


variable1=value1
variable2=value2

Re: how to initialize the values of parameters with a .mos file

My file .txt is excatly like yours.

When I write :

simulate(ModelName, simflags="-overrideFile=file.txt"); getErrorString();

OpenModelica does not write anything, whereas when I write :

simulate(ModelName, simflags="-overrideFile=file.txt")

I get lots of warnings. Are you sure of this syntax ?

Re: how to initialize the values of parameters with a .mos file

Hi,

If you use OMShell to write these commands then getErrorString() is not needed as OMShell will sent it after each command.
I usually use omc directly from command line via:

Code:


omc script.mos

and when using it from command line you need getErrorString().

Cheers,
Adrian Pop/

Re: how to initialize the values of parameters with a .mos file

Ok Adrian, thank you very much !

And how could I get the names of the variables which need to be given a start value so that the simulation could work ?
I think that my problem is that OpenModelica has not the same "initialization variables" as Dymola.

Thanking you in advance,
Julien

Re: how to initialize the values of parameters with a .mos file

Hi again,

Do you have Dymola 2014?
Have you run Dymola in pedantic mode? Simulation tab->Simulation->Setup->Translation->"Pedantic mode for checking Modelica semantics"
It will tell you which variables are not fully initialized and then you have to add (start = Value, fixed = true) for them until you have a fully initialized model.
Then try the model in OpenModelica.

You cannot use -overrideFile to set values for initialization (that can only be used to set bindings, not start values for parameters and variables).
For initialization you have to edit your model and add (start = Value, fixed = true).

Cheers,
Adrian Pop/

Re: how to initialize the values of parameters with a .mos file

Hi again Adrian,

I work on Dymola 2012.
I've ticked the "Warn about parameters with no default" box in Simulation tab->Simulation->Setup->Translation (I imagine it is the closest to "Pedantic mode for checking Modelica semantics"  on Dymola 2012). Then I simulated my model with Dymola, and I got "true", therefore the model is working well.
Nevertheless, before simulating,  Dymola needs to be given a script containing guess values for some of the variables of the sytem, in order to be able to calculate the final state of the system. I don't succeed in doing that with OpenModelica. It is possible to do it through a .mos file, isn't it?

When I write in the Shell:
runScript("script_init.mos")
I get:
"Failed"
Without further explanation.

The commands I use before runnig my Script are:
loadModel(Modelica,{"3.1"});
loadFile("file_name")
checkModel(model_name)
translateModel(model_name)

Before running my script, when I write:
listVariables()
I get:
{}
Therefore, I imagine the problem is that my variables do not exist when I try to give them a value.

What do you think about it ?
What should I do?
Thank you for all your answers,

Cheers,
Julien

Re: how to initialize the values of parameters with a .mos file

Hey Ben!

I do have the same Problem. Have you been able to solve it and add inital values for your variables using the .mos file?
If so could you tell me how?

Greetings

Pliskin

Re: how to initialize the values of parameters with a .mos file

I see...

adrpo wrote:


You cannot use -overrideFile to set values for initialization (that can only be used to set bindings, not start values for parameters and variables).
For initialization you have to edit your model and add (start = Value, fixed = true).

So Adrian, there is no way to set initial values (for example, from a power flow solution) directly to the model using scripting? What about using the option -override?

Cheers!

Re: how to initialize the values of parameters with a .mos file

hello,

I'm new to  OM, and i have maybe a stupid question.

I want to control a simulation of my model by doing several simulations. and I want to know how can I control that. I know, that with a mos script we can do such things, but i don't know how to create a .mos script.

My question is : how can I create mos file ?

thank you.

Re: how to initialize the values of parameters with a .mos file

Nasrou wrote:


hello,

I'm new to  OM, and i have maybe a stupid question.

I want to control a simulation of my model by doing several simulations. and I want to know how can I control that. I know, that with a mos script we can do such things, but i don't know how to create a .mos script.

My question is : how can I create mos file ?

thank you.

Hi,

A .mos script is just a file you can make with any editor that has the extension ".mos". You can create a "script.txt" with Notepad and then rename it to "script.mos".
You can see in this post how to do multiple simulations with different parameters:
https://openmodelica.org/forum/default- … eter-sweep

Cheers,
Adrian Pop/

Re: how to initialize the values of parameters with a .mos file

Hi Adrian

thank you for your answer, and i would like to know with which programming language i should write my script.

Cheers.

Re: how to initialize the values of parameters with a .mos file

hello,
I am new to OpenModelica.
I need help in changing the input values before running the simulation from OMShell.
Is there any specific command for that?

Thanks & Regards,
Shruthi Ramakumar

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