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

Get Variable-Values from former Timestep

Get Variable-Values from former Timestep

Hi there,
as many askers before, I'm relatively new to programming in Modelica but already realy fascinated from the language power.
What I wasn't able to find yet, is how to get values of a variable from former timesteps.
So like I want to have a Heat-Loss calculation with a temperature-dependent value for the convection coefficient. As the fan that should generate the new convection coefficient needs 3 seconds to start up, I'd need the Temperature value from 3 seconds ago to calculate the current convection coefficient.

Does anyone knows how to do something like that?

Thank you very much for every kind of answer and help.

Daniel

Re: Get Variable-Values from former Timestep

Re: Get Variable-Values from former Timestep

Thank you very very much!

Re: Get Variable-Values from former Timestep

sjoelund.se wrote:

delay()

sjoelund,

Is there any similar function to get only the value of a given variable at the END of simulation can consider that as fixed.  For instance in the example der(x)=2, I would like to get Y(x(endTime)) and fix that value as constant throughout the simulation.  Y will be a horizontal plot over the entire simulation period.  In the chart, since the final simulation value of X = 10, I would like to get Y as 10, means Y plot will be a horizontal line of value 10.

I do not know if a combination of delay function and something else can solve that

Re: Get Variable-Values from former Timestep

For a horizontal line based on stop-time... use some other tool for post-processing the data. OpenModelica plotting functionality is limited as the project is more focused on simulation results. Octave and Matlab can both read the results and I believe they have plotting tools.

Maybe it's possible to hold the plot in OpenModelica and put additional lines afterwards, but I do not know the API that well.

Re: Get Variable-Values from former Timestep

sjoelund.se wrote:

For a horizontal line based on stop-time... use some other tool for post-processing the data. OpenModelica plotting functionality is limited as the project is more focused on simulation results. ...

In fact, I used the graphical plot analog to explain the problem.  But my point of interest is to to get the the final simulation output of a dynamic system and plug that as constant input to a static model through a connector.  Consider the above example, der(x)=2 with y=f(x), and if I connect this model to a static model that requires only the final value of der(x), I observe two things: one erroneous output and the other a failed simulation

1) the first model (component) has to be simulated for a time t, say t=10
2) the second model being static, the simulation time can be anything. 

Now, the two models being connected, when I run the composite model for a simulation time of 1, der(x) sends only the value x that corresponds to t=1.  Since the second model needs only der(x) value at t=10, the whole result  is thus false.  Now, when I use a simulation time of t=10, the the simulation fails.  Because, at time t=10 the input to model2 is rather variable instead of a constant input through the connector.

I am sure there must  be a workaround to handle this issue.  In other programming languages, it is easy to grab a variable result in a given time t and pass it to any other operation.  With Modelica, it is very strange that there is no such a thing to grab or if any, I am just looking for it to know how to do that.

I do hope that you would get my point.

Re: Get Variable-Values from former Timestep

Ah. That's possible to do in OpenModelica. Of course you would need to make it into two different experiments. One to get the final value of x, and one to use it:

Code:

loadString("model M

  Real x(start=0);
equation
  der(x) = 1.0;
end M;");getErrorString();
simulate(M, stopTime=10.0);
y := val(x, 10.0);
loadString("model N
  Real x = " + String(y) + ";
  Real y = sin(x);
end N;");getErrorString();
simulate(N);
val(y, 0.0);

Re: Get Variable-Values from former Timestep

Thanks sjoelund. Yes, indeed they are separate experiments.  I didn't use the appropriate term.  Now where can I put this piece of code? Is it as a *.mos or inline call through the loadString function? 
I have got a scripting function for loadString :

Code:


function loadString
  input String data;
  input String filename = "<interactive>";
  input String encoding = "UTF-8";
  output Boolean success;
end loadString;

Let me take the following two experiments each based on the corresponding model below (the model M is for experiment 1 and model N be for experiment 2):

Code:

model M  //from experiment 1

  Real x;
  Real y;
equation
  der(x) = 2;
  y = x;
  Outoing_Connector = y;
end M;

and

Code:

model N  //from experiment 2

   Real y;
equation
    y = Incoming_connector;
end M;

Now I have to create a third experiment, Experiment 3 that connects Experiment 1 and Experiment 2
How can I use your code based on this situation?

Re: Get Variable-Values from former Timestep

The loadString is just a way of loading a string value instead of a file in a mos-script. I realized another way of doing it that possibly suits you better.

Run as omc a.mos. The disadvantage to the approach above is that here the value is a parameter (which means fewer optimizations are possible). The advantage is that you can write the models as usual and only script the override. (Can also be done using -overrideFile if you have many parameters to set)

a.mos:

Code:

loadFile("a.mo"); // experiment 1 & 2

simulate(M); // experiment 1
y := val(y, 1.0); // read the end value from result-file
simulate(N, simflags = "-override y=" + String(y)); // experiment 2, read the parameter value at the start of the simulation
val(z, 0.5); // verify that it worked

a.mo:

Code:

model M  //from experiment 1 

  Real x;
  Real y;
equation
  der(x) = 2;
  y = x;
end M;

model N  //from experiment 2
   parameter Real y; // to be read from file
   Real z = sin(y); // f(y)
end N;

Re: Get Variable-Values from former Timestep

Oh, and to add my output from running that code:

Code:

$ omc a.mos

/home/marsj/trunk/build/bin/omc 1.9.0 Beta2 (r14687)
true
record SimulationResult
    resultFile = "/home/marsj/tmp/M_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 0.000001, method = 'dassl', fileNamePrefix = 'M', storeInTemp = false, noClean = false, options = '', outputFormat = 'mat', variableFilter = '.*', measureTime = false, cflags = '', simflags = ''",
    messages = "",
    timeFrontend = 0.052824347,
    timeBackend = 0.18675881800000002,
    timeSimCode = 0.05071623099999989,
    timeTemplates = 0.130822174,
    timeCompile = 2.005965308,
    timeSimulation = 0.01198327,
    timeTotal = 2.439157141
end SimulationResult;
2.0
record SimulationResult
    resultFile = "/home/marsj/tmp/N_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 0.000001, method = 'dassl', fileNamePrefix = 'N', storeInTemp = false, noClean = false, options = '', outputFormat = 'mat', variableFilter = '.*', measureTime = false, cflags = '', simflags = '-override y=2'",
    messages = "",
    timeFrontend = 0.039377649,
    timeBackend = 0.018874146,
    timeSimCode = 0.011657719,
    timeTemplates = 0.018043621000000003,
    timeCompile = 0.265671996,
    timeSimulation = 0.010105428000000001,
    timeTotal = 0.36379121200000003
end SimulationResult;
0.9092974268256817

Re: Get Variable-Values from former Timestep

The mos execution went fine.  Very helpful.  With your last code (to add results to output),  is this to be executed with a separate .mos or a commandLine istruction? In addition, I thought of using omc from local directory path. 

Code:

$ omc a.mos

/home/marsj/trunk/build/bin/omc 1.9.0 Beta2 (r14687)

Just one more question if you bear with me

(Can also be done using -overrideFile if you have many parameters to set)

Could you provide me an example based on the above M & N models? 
For instance, if I were to pass an array of a variable, say y[:] in model M to z[:] = f(y[:], stopTime in model N)

Again, thank you.

Re: Get Variable-Values from former Timestep

Something like:

Code:

loadFile("a.mo"); // experiment 1 & 2 

res := simulate(M); // experiment 1
writeFile("N.override","");
stopTime := 1.0;
y1 := val(y[1], stopTime);
y2 := val(y[2], stopTime);
writeFile("N.override","y[1]=" + String(y1) + "\n",append=true);
writeFile("N.override","y[2]=" + String(y2) + "\n",append=true);
getErrorString();
readFile("N.override");
simulate(N, simflags = "-overrideFile N.override"); // experiment 2, read the parameter value at the start of the simulation
val(z[1], 0.5); // verify that it worked

There does not seem to be a better way to script it

Re: Get Variable-Values from former Timestep

sjoelund, thank you very much.  I will try out the last one.

Re: Get Variable-Values from former Timestep

sjoe, I have for the following feedback:

for a single parameter simulation, it works BUT when I change the simulation time to anything above 1, it prints NaN.  One parameter with stopTime=1.0 is ok. 

for multiple parameters, the second model can't process data from the first model even if simulation time is set to 1.0.  If time is above 1.0, the same way it prints NaN.  The record keeps a stopTime of 1


Look at the models and the results below:

Code:

model M

Real x, y1,y2;
equation
    der(x) = 2;
    y1 = x;
    y2 = 2*x;
end M;

model N
  parameter Real y1,y2;
  Real z1 = sin(y1); // f(y)
  Real z2 = sin(y2); // f(y)
end N;

Results (simulation time 1):

Code:


record SimulationResult
    resultFile = "M_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 50
0, tolerance = 1e-006, method = 'dassl', fileNamePrefix = 'M', storeInTemp = f
alse, noClean = false, options = '', outputFormat = 'mat', variableFilter = '.*'
, measureTime = false, cflags = '', simflags = ''",
    messages = "",
    timeFrontend = 0.00632564799369095,
    timeBackend = 0.0052462182423628,
    timeSimCode = 0.00283488596844051,
    timeTemplates = 0.548435656583297,
    timeCompile = 1.49165812579071,
    timeSimulation = 1.64479135634378,
    timeTotal = 3.69934957640679
end SimulationResult;
1.0
2.0
4.0

record SimulationResult
    resultFile = "N_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 50
0, tolerance = 1e-006, method = 'dassl', fileNamePrefix = 'N', storeInTemp = f
alse, noClean = false, options = '', outputFormat = 'mat', variableFilter = '.*'
, measureTime = false, cflags = '', simflags = '-overridefile N.override'",
    messages = "",
    timeFrontend = 0.00677093252141672,
    timeBackend = 0.00439515979274317,
    timeSimCode = 0.00358519237318874,
    timeTemplates = 0.0135766343759655,
    timeCompile = 1.53475471420793,
    timeSimulation = 0.343025548347943,
    timeTotal = 1.90614690201291
end SimulationResult;
NaN
NaN

Results (stopeTime=10) but the script doesn't seem to pass this time.  Instead, we always see the default simulation time 1.0

Code:


record SimulationResult
    resultFile = "M_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 50
0, tolerance = 1e-006, method = 'dassl', fileNamePrefix = 'M', storeInTemp = f
alse, noClean = false, options = '', outputFormat = 'mat', variableFilter = '.*'
, measureTime = false, cflags = '', simflags = ''",
    messages = "",
    timeFrontend = 0.00602576249542665,
    timeBackend = 0.00445403059543273,
    timeSimCode = 0.0028285642715074,
    timeTemplates = 0.0140187580552248,
    timeCompile = 4.64686487293784,
    timeSimulation = 0.345604405590593,
    timeTotal = 5.01983788008215
end SimulationResult;

10.0
NaN
NaN

record SimulationResult
    resultFile = "N_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 50
0, tolerance = 1e-006, method = 'dassl', fileNamePrefix = 'N', storeInTemp = f
alse, noClean = false, options = '', outputFormat = 'mat', variableFilter = '.*'
, measureTime = false, cflags = '', simflags = '-overridefile N.override'",
    messages = "",
    timeFrontend = 0.00647934425037713,
    timeBackend = 0.00441214935325089,
    timeSimCode = 0.00388468276539472,
    timeTemplates = 0.0128057824561847,
    timeCompile = 1.77746204413651,
    timeSimulation = 0.63764823391543,
    timeTotal = 2.4427305621648
end SimulationResult;
NaN
NaN

Re: Get Variable-Values from former Timestep

Change overridefile to overrideFile, stopeTime to stopTime, I think

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