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

Import initial values in Model

Import initial values in Model

Hi,

I have a Model which needs a big amount of initial values and parameters changed before simulating.
In Dymola it is done by executing a script which contains all initial values changing them after translating.

You could write all values within the modelcode but the good thing about the script is that you can easily change the values and it keeps the modelcode short and easy to read.

Is something similar like the script possible in OpenModelica?
Or is there a different way to solve this.

Thank you
Julian

Re: Import initial values in Model

The start values are part of the ModelName_init.xml file. You can modify it manually, try your luck with xsltproc, or hope https://build.openmodelica.org/Document … Value.html works properly

Re: Import initial values in Model

Hi sjoelund,

Thanks for the tip, the function "setInitXmlStartValue" works, I can write the desired value(s) into the <modelName>_init.XML file.

However, how do I make it so that the next time I enter the "simulate" command, the values from this file will be used? Can I add a parameter to the "simulate(...)" command that forces the use of some other XML file than the automatically generated file?

I read the OpenModelica User Guide, and could not find any info on how to do that.

Here is how I try to do it:
1) simulate(WasteWater.ASMx.Examples.TestDeniNitriModel) so that the XML file is there in the first place
2) setInitXmlStartValue("WasteWater.ASMx.Examples.TestDeniNitriModel_init.xml", "Sink2.SP", "7", "WasteWater.ASMx.Examples.TestDeniNitriModel_init.xml") to write the alternative value for Sink2.SP into the XML file
3) simulate(WasteWater.ASMx.Examples.TestDeniNitriModel) again to start the simulation

But instead of using the value I wrote into the XML file in step 2, it re-generates the XML file so the value is overwritten. Simulationsresult from step 3 is the same as from step 1.

Best regards,
Julian

Re: Import initial values in Model

You do not re-simulate. Use:

Code:

buildModel(M);

setInitXml...;
system("./M -f newFile.xml");

Re: Import initial values in Model

Hi,
thank you for your answer. It is actually working this way.

But there is a little problem. In the model I tested there are 170 Variables that need to be changed. The  setInitXmlStartValue command to change them takes about 3 minutes.
3 minutes wouldn't be a problem, but we got other models that need more than a thousand Variables to be changed. So this would take too long.

This is the script I wrote:

InitialComponents := {"nitri2.Si","nitri2.Ss", .....};

InitialValues := {"5.178780049396e+01","2.156778436699e+00", .....};

buildModel(WasteWater.ASM1.Examples.ComplexPlant);

setInitXmlStartValue("WasteWater.ASM1.Examples.ComplexPlant_init.xml", InitialComponents, InitialValues, "WasteWater.ASM1.Examples.ComplexPlant_init.xml");

system("WasteWater.ASM1.Examples.ComplexPlant.exe");

I think setInitXmlStartValue rewrites the init.xml for every single variable.
Is there a way to make this faster with the function like it is or could you modify the setInitXmlStartValue function so that it writes all variables in the init.xml in one step?

Best regards,
Julian

Re: Import initial values in Model

Hi,

Since a while now you can do the overrides at the command line directly, i.e.
WasteWater.ASM1.Examples.ComplexPlant.exe -override nitri2.Si=5.178780049396e+01,nitri2.Ss=2.156778436699e+00

run:
WasteWater.ASM1.Examples.ComplexPlant.exe -?
at the command line to see the -override help.

This is very fast and does not change the .xml file at all, just overrides the values after they are read from the .xml file.
However if you have a lot of values that you need to change then most likely you will run out of command line size (Windows has 2047 chars only).

We could probably extend this very easily with something like -overrideFile=f.txt where each line contains
x1=val1
x2=val2
etc...

Cheers,
Adrian Pop/

Re: Import initial values in Model

Hi Adrian,
thank you for your reply.

As you already said writing it directly in the command line won't work for the values must be as precice as the examples I wrote. So that would do for about 50-70 Components.

But if you could extend the command with "-overrideFile=f.txt" it would be perfect.
That would be just what I have been looking for.

When would that be available?

Best regard,
Julian

Re: Import initial values in Model

Hi,

I will try to implement it until the end of next week.

Cheers,
Adrian Pop/

Re: Import initial values in Model

Hi,

Done in revision 13104, see here:
https://trac.openmodelica.org/OpenModel … eset/13104

Cheers,
Adrian Pop/

Re: Import initial values in Model

Hey,
tried it today and it works perfectly.

Thank you very much!

Re: Import initial values in Model

julian.wyszynski wrote:

Hey,
tried it today and it works perfectly.

Thank you very much!

How did you run Wastewater model from OpenModelica?  I just tried to open it with OMEdit but couldn't see details of the model except the folder wastewater. 

I installed the demo version of Dymola and tried to open it.  Well I see the details of the model but it doesn't seem to run as time related inputs are missing like CombiTableTime for the WWsource, FixedDelay function for a Limiter library, etc. 

I would be glad if you or anyone here can tell me how to fix the missing components and to run it preferably from OpenModelica.  I am not sure if the demo version of Dymola can simulate that even if it helps to visualize the components.

Re: Import initial values in Model

Hey,
the Version of the WasteWater Library like found on the Modelica website is based on the Modelica Standard Library 2.2
It is not compatible with the current version of Modelica and is not working in your Dymola version and probably will have the same problems in OpenModelica.

You need to modify some components like the declaration of CombiTableTime. It is now found in "Modelica.Blocks.Sources.CombiTimeTable".
It should be possible to make everything run, just use equivalent components which are available in the current version of Modelica.

OpenModelica has some additional issues with this library that Dymola ignores.
If you try and open the Library with OMShell you are told what courses the problem and can fix them.

Commands you need to execute are:

loadModel(Modelica)
loadFile("YOURPATH/package.mo")

I hope this helps you.

Re: Import initial values in Model

Thank you Julian.  I tied OMShell commande but couldn't see the any of the models doing something.  For example, I tried to see ASM1 example of SmallPlant with small_asm1.mos script.  Below are the steps I tired (root drive path is truncated):

>> loadModel(Modelica)
true

>> loadFile("WasteWater/WasteWater/package.mo")
true
[WasteWater/WasteWater/../WasteWater/ASM2d/package.mo:202:14-202:79:writable] Error: The file was not encoded in UTF-8:
  "Concentration of effective substance in precipitant flow   [g/m?]".
  To change encoding when loading a file: loadFile(encoding="ISO-XXXX-YY").
  To change it in a package: add a file package.encoding at the top-level.
  Note: The Modelica Language Specification only allows files encoded in UTF-8.


Then I also tried to see one of the examples of ASM1 with the following command.  The shell returned me false.

>> loadFile("F:/OMODs/WasteWater/WasteWater/AMS1/package.mo")
false

>>


I have OpenModelica 1.8.1 installed.  What version of OpenModelica with OMShell do you use to have it working?  By the way, what is the difference between using omc.exe commands and OMShell commands?  As omc is a compiler, is OMShell using omc.exe at the background or it has its own compiler?

CombiTimeTable doesn't seem available in the Dymola demo 2012 version.

Re: Import initial values in Model

Hi,

This should work (script.mos file):

Code:


loadModel(Modelica,{"2.2.1"}); getErrorString();
loadFile("WasteWater/WasteWater/package.mo"); getErrorString();
simulate(WasteWater.Path.To.What.Model.You.Want.To.Simulate); getErrorString();

However you have to go to the directory where package.mo is and add a package.encoding file
containing: Windows-1252
Just make a package.txt, write Windows-1252 and rename it to package.encoding.
This way you're telling omc to expect to have the files encoded in Windows-1252 char set.

I doubt that loading parts of the WaterWaste package would work as things are dependent on each other.

You could try the latest Windows nightly build from here:
https://build.openmodelica.org/omc/buil … ly-builds/

The best way would be to use omc from command line, i.e. Start->cmd.exe
/path/to/your/mosfile/ > C:\OpenModelicaX.Y.Z\bin\omc script.mos
If this library is build for Modelica 2.x you can run OMC like this (also note i loaded Modelica 2.2.1 in script.mos):
/path/to/your/mosfile/ > C:\OpenModelicaX.Y.Z\bin\omc +std=2.x script.mos
run > C:\OpenModelicaX.Y.Z\bin\omc +help
to get a list of flags for the command line.

Cheers,
Adrian Pop/

Re: Import initial values in Model

Hi Adrian, Thank you.  I followed your instructions and got the following error:  Error: The file was not encoded in UTF-8:

I was trying to run the 'SmallPlant' model at WasteWater.ASM1.Examples.SmallPlant

The mos file is like this:
loadModel(Modelica,{"2.2.1"}); getErrorString();
loadFile("F:/OMODs/WasteWater/WasteWater/package.mo"); getErrorString();
simulate(ASM1.Examples.SmallPlant); getErrorString();


Below is the complete information on the command line run

F:\OMODs\WasteWater\WasteWater\ASM1> C:\OpenModelica1.8.1\bin\omc +std=2.x myScript.mos
true
""
true
"[F:/OMODs/WasteWater/WasteWater/../WasteWater/ASM2d/package.mo:202:14-202:79:wr
itable] Error: The file was not encoded in UTF-8:
  "Concentration of effective substance in precipitant flow   [g/m?]".
  To change encoding when loading a file: loadFile(encoding="ISO-XXXX-YY").
  To change it in a package: add a file package.encoding at the top-level.
  Note: The Modelica Language Specification only allows files encoded in UTF-8.
"
record SimulationResult
    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 50
0, tolerance = 1e-006, method = 'dassl', fileNamePrefix = 'ASM1.Examples.SmallPl
ant', storeInTemp = false, noClean = false, options = '', outputFormat = 'mat',
variableFilter = '.*', measureTime = false, cflags = '', simflags = ''",
    messages = "Simulation Failed. Model: ASM1.Examples.SmallPlant does not exis
t! Please load it first before simulation.",
    timeFrontend = 0.0,
    timeBackend = 0.0,
    timeSimCode = 0.0,
    timeTemplates = 0.0,
    timeCompile = 0.0,
    timeSimulation = 0.0,
    timeTotal = 0.0
end SimulationResult;
""

I haven't tried Nightly-builds yet.  Should I uninstall OpenModelica1.8.1 and replace it with Nightly-builds?  Or I can install it separately?

Edited by: Scoda - Oct-13-12 15:36:41

Re: Import initial values in Model

Hi,

I now converted the library to Modelica 3.x and also the annotations to 3.x.
You can get it from here (using a Subversion client, i.e. TortoiseSVN):
https://openmodelica.org/svn/OpenModeli … /libraries
user: anonymous
pass: none
You can also get it as a zip file from here:
https://trac.openmodelica.org/OpenModel … WasteWater
Click on "Download in other formats: Zip Archive".

I found a strange bug in OpenModelica so you cannot simulate any of the plant models until is it fixed. The bug is reported here:
https://trac.openmodelica.org/OpenModelica/ticket/1882

Also, it seems the library is rather weird as you need to run a Dymola mos script to set the initial values of the plant models.
For example this script WasteWater\ASM1\small_asm1.mos should be used for the model: WasteWater.ASM1.Examples.SmallPlant
However Modelica has evolved since this library was made, so you don't need such a script anymore, you can just set the
start values of those components to the values given in the .mos file. I haven't done that yet.

Cheers,
Adrian Pop/

Re: Import initial values in Model

Hi Adrian,

Many thanks for your prompt action.  Now, I have opened the model without any issue both in OpenModelica and Dymola(demo).  However, I was not able to simulate it with any of the two tools as OpenModelica prints 'Symbolic' error when I tried to simulate and Dymola demo found the model too complicated to run under the demo version.  So, it would be nice to simulate the model rather with OpenModelica when the issue (bug) you have identified is fixed. 

Thanks once more and I look forward to reading OpenModelica update related to this issue,

Re: Import initial values in Model

adrpo wrote:

Also, it seems the library is rather weird as you need to run a Dymola mos script to set the initial values of the plant models.
For example this script WasteWater\ASM1\small_asm1.mos should be used for the model: WasteWater.ASM1.Examples.SmallPlant
However Modelica has evolved since this library was made, so you don't need such a script anymore, you can just set the
start values of those components to the values given in the .mos file. I haven't done that yet.

I have a Script for the initial values which is working.


tempString := getSourceFile(WasteWater);
tempString := dirname(tempString);
cd(tempString);

buildModel(WasteWater.ASM1.Examples.BenchPlant);

system("WasteWater.ASM1.Examples.SmallPlant.exe -overrideFile ASM1BenchPlant.txt");

What is the other way to do it? Writing them into the Variabledeklaration like this

Real a(start=1);

makes it hard to read the code and to see which initial values are used.

I tried to write them into an "initial equation" section but this doesn't seem to work in OpenModelica.
I wrote it like this:

model testModel
...
...
initial equation
  variable1 = 1;
  variable2 = 2;
equation
...
...
end testModel;


Another question:
If I try and simulate ASM1.Examples.BenchPlant I get this warning:
[C:/OpenModelica1.9.0/lib/omlibrary/Modelica 3.1/Blocks/Nonlinear.mo:19:12-23:97:writable] Warning: If-equations are only partially supported.

Will this limiter be available in the future?

Re: Import initial values in Model

Hi,

I think is fine the way you do it with overrideFile.
I don't know why initial equation doesn't work, I think it should.
If equations support is on the way, so that warning will disappear in the future.
We currently translate if equations in if expressions but that's not always possible, especially when you have asserts.

Cheers,
Adrian Pop/

Re: Import initial values in Model

Thank you for the info.

The strange bug you mentioned in your earlier post is that the one which comes when simulating "SmallPlant"?


Error: Interner Fehler generateHelpVarsForWhenStatements failed
Error: Interner Fehler ./Compiler/BackEnd/SimCode.mo: function createSimCode failed [Transformation from optimised DAE to simulation code structure failed]

Re: Import initial values in Model

Hi,

No, this is some other bug it seems. The bug I mentioned above [https://trac.openmodelica.org/OpenModelica/ticket/1882]
makes the system of equations overdetermined, so you cannot actually simulate it at all.
If you have a model that triggers your bug:

Code:


Error: Interner Fehler generateHelpVarsForWhenStatements failed
Error: Interner Fehler ./Compiler/BackEnd/SimCode.mo: function createSimCode failed [Transformation from optimised DAE to simulation code structure failed]

add it to our bug tracker here with a bug description: https://trac.openmodelica.org/OpenModelica
so we can have a look at it.

Cheers,
Adrian Pop/

Re: Import initial values in Model

Hi,

Bug:
https://trac.openmodelica.org/OpenModelica/ticket/1882
was fixed now.

Cheers,
Adrian Pop/

Re: Import initial values in Model

Hello,
         I'm trying to get Wastewater Library working in order to implement it for my thesis investigating energy efficiency control of pumps and blowers preserving the process.
Unfortunately the link provided in the post is not working anymore.
I wish I could download the most recent version with the correction and bug fixes and get it work, if possible, with a software running on the Mac OS environment.
Any help will be appreciated.

Regards,

Marco Carolli

Re: Import initial values in Model

OpenModelica ships with the following version: https://github.com/modelica-3rdparty/Wa … ee/master. If you want it fixed, you will need to submit pull requests there.
Note that this is the MSL 2.x version, so you need to use command-line omc or OMShell to use it. If there is an MSL 3.x branch created on github, OpenModelica can also ship with this one.

Re: Import initial values in Model

I tried to open it with omc but it didn't work.

Edited by: Carolli - Oct-28-14 10:34:23

Re: Import initial values in Model

There is no such latest version with MSL 3.x corrections. The latest one is the one in github, and it is for MSL 2.x with Modelica 1.x annotations. If you want an MSL 3.x version, you need to open the library in Dymola and convert it to MSL 3.x. It is as far as I know the only Modelica tool that supports conversion scripts.

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