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

set as initial conditions the last values

set as initial conditions the last values

Hi,

Is there a method in order to set as initial conditions the last values of the variables of the previous simulation.
Stupid Example:
there is a resistor, a capacitor and a voltage source. The initial voltage of the capacitor is 3V. At the end of the simulation the voltage is 6.7V because the capacitor is charging.
In the second simulation I'd like to start with the initial voltage of the capacitor at 6.7V.

I'm able to use the setInitXmlStartValue function but this can't help me.
I need this feature because I'm trying to couple a modelica model and a 3D cfd model.

thank you
Regards

Re: set as initial conditions the last values

Code:

loadString("model M

  Real x;
equation
  der(x) = 1.0;
end M;");getErrorString();
simulate(M);getErrorString();
x:=String(OpenModelica.Scripting.val(x,1.0));getErrorString();
simulate(M,simflags="-override x=" + x);getErrorString();
val(x,1.0);getErrorString();

Re: set as initial conditions the last values

Hi,

You could use -override var=value or -overrideFile=file arguments to the simulation executable.
instead of setInitXmlStartValue.

Another way is to give a .mat file to the simulation executable to be use as initial conditions
using -iif=mat_file_with_initial_values.mat

The idea is to first use buildModel(Model) to generate the executable,
then run the executable with different flags several times.

Cheers,
Adrian Pop/

Re: set as initial conditions the last values

Hi,
thank you Adrian and sjoelund.se for the quick reply!

I think Adrian's solution is better but I've 2 problems:
1) I'm working in python and OMPython.execute("buildModel(\"myModel\")") doesn't initialize the model. The simulate command works.
2) I need to compile the "mat_file_with_initial_values.mat" from the result.mat from the previous simulation in order to write in the "mat_file_with_initial_values.mat " the variables values of the last timestep in the previous simulation and modify only 2 or 3 values.

thank you

Re: set as initial conditions the last values

Adrian,
             I am glad that a similar query I made earlier was answered here. All along I have been relying on the following reference documentation on scripting
https://build.openmodelica.org/Document … ulate.html for help on the use of parameters and options on scripting commands.

But that switches such as " override , iif" exist is a revelation to me.  I wish these were included in the above documentation. Anyway, out of curiosity, I have executed the compiled model in the interactive bash shell (command window for Windows users)    with  -- help  switch.  and lo and behold it showed me all the options one could specify for subsequent execution . I hope this helps others.


|                 | |       | | <-clock=value> or <-clock value>
|                 | |       | |   selects the type of clock to use -clock=RT, -clock=CYC or -clock=CPU
|                 | |       | | <-cpu>
|                 | |       | |   dumps the cpu-time into the results-file
|                 | |       | | <-f=value> or <-f value>
|                 | |       | |   value specifies a new setup XML file to the generated simulation code
|                 | |       | | <-help=value> or <-help value>
|                 | |       | |   get detailed information that specifies the command-line flag
|                 | |       | | <-iif=value> or <-iif value>
|                 | |       | |   value specifies an external file for the initialization of the model
|                 | |       | | <-iim=value> or <-iim value>
|                 | |       | |   value specifies the initialization method
|                 | |       | | <-iit=value> or <-iit value>
|                 | |       | |   [double] value specifies a time for the initialization of the model
|                 | |       | | <-ils=value> or <-ils value>
|                 | |       | |   [int] default: 1
|                 | |       | | <-interactive>
|                 | |       | |   specify interactive simulation
|                 | |       | | <-iom=value> or <-iom value>
|                 | |       | |   value specifies the initialization optimization method
|                 | |       | | <-ipopt_hesse=value> or <-ipopt_hesse value>
|                 | |       | |   value specifies the hessematrix for Ipopt
|                 | |       | | <-ipopt_init=value> or <-ipopt_init value>
|                 | |       | |   value specifies the initial guess for optimization
|                 | |       | | <-l=value> or <-l value>
|                 | |       | |   value specifies a time where the linearization of the model should be performed
|                 | |       | | <-logFormat=value> or <-logFormat value>
|                 | |       | |   value specifies the log format of the executable. -logFormat=text (default) or -logFormat=xml
|                 | |       | | <-ls=value> or <-ls value>
|                 | |       | |   value specifies the linear solver method
|                 | |       | | <-ls_ipopt=value> or <-ls_ipopt value>
|                 | |       | |   value specifies the linear solver method for ipopt
|                 | |       | | <-lv=value> or <-lv value>
|                 | |       | |   [string list] value specifies the logging level
|                 | |       | | <-measureTimePlotFormat=value> or <-measureTimePlotFormat value>
|                 | |       | |   value specifies the output format of the measure time functionality
|                 | |       | | <-nls=value> or <-nls value>
|                 | |       | |   value specifies the nonlinear solver
|                 | |       | | <-noemit>
|                 | |       | |   do not emit any results to the result file
|                 | |       | | <-noEventEmit>
|                 | |       | |   do not emit event points to the result file
|                 | |       | | <-output=value> or <-output value>
|                 | |       | |   output the variables a, b and c at the end of the simulation to the standard output
|                 | |       | | <-override=value> or <-override value>
|                 | |       | |   override the variables or the simulation settings in the XML setup file
|                 | |       | | <-overrideFile=value> or <-overrideFile value>
|                 | |       | |   will override the variables or the simulation settings in the XML setup file with the values from the file
|                 | |       | | <-port=value> or <-port value>
|                 | |       | |   value specifies interactive simulation port
|                 | |       | | <-r=value> or <-r value>
|                 | |       | |   value specifies a new result file than the default Model_res.mat
|                 | |       | | <-s=value> or <-s value>
|                 | |       | |   value specifies the solver
|                 | |       | | <-w>
|                 | |       | |   shows all warnings even if a related log-stream is inactive

Re: set as initial conditions the last values

I'm writing a python script in order to:
- read the variables at a specific timestep (at last timestep for me)
- compile a .mat file in order to use the "-iif" option when I'll restart the simulation and use the lest variables values.

The question is: I need the structure of the .mat file for the initial values. Where can I find this structure? Can I use a .txt file instead .mat?

thank you

Re: set as initial conditions the last values

I see...

I am facing the same problem, and hope to read a good solution for this. My concern is to set the initial values for every single variable in the model. So, using the .mat file or .txt file is the best option for me.

How should be the structure of the .mat file? I should be the same as the output .mat file from a simulation?

Cheers!

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