问题
I have a selection of variables in my Modelica simulation (using Dymola) which is running good. Now I want to plot the behaviour of certain of these variables, which are numbered (with indices). I do not wish to plot the variables versus time, but versus their index. I'm planning to use the plotArray
function, but that's not really what I'm curious about in this post. The problem is, all variables are zero when I call them, which indeed is their initial value, but I want to attain their value when steady state has set in, let's say when time = 5000
. Hence I need to evaluate the variables at this specific time.
How do I evaluate a variable at a specific time from the simulation, or at the end of the simulation for that matter?
Edit: After extensive googling I've come over a function called val()
, which seems to do what I want, but I can't get it to work with my Dymola software.
Edit 2: I've managed to evaluate my variables as desired, but the approach I used was (in my opinion) beyond tedious. I extracted the .mat-file from the simulation into MATLAB, where I eventually managed to identify the variables of my desire, and then plotted them at the desired time. What really surprised me, however, was the apparent chaos with respect to the variables in the .mat-file. On first glance, there was little agreement between how my variables are ordered in the Modelica model and how they are ordered in the .mat-file, and there was no naming of the variables, leaving me to search for variables solely based on comparing their value with the Dymola simulation. Am I simply completely mistaken here, or is there some easier way to extract variables from the .mat-file?
回答1:
Maybe I'm misunderstanding your question, but I suspect there is a simple answer here. It sounds like you have an array and you want to populate that array with the values of a specific variable at a specific time and then plot the array. So, for example, let's say you had a variable x
and you want to record the time that x
crossed certain threshholds. A simple model like this would suffice:
model RecordVariables
Real x;
Real times[10];
initial equation
x = 11;
equation
der(x) = -x;
when x<=10.0 then
times[1] = time;
end when;
when x<=9.0 then
times[2] = time;
end when;
when x<=8.0 then
times[3] = time;
end when;
when x<=7.0 then
times[4] = time;
end when;
when x<=6.0 then
times[5] = time;
end when;
when x<=5.0 then
times[6] = time;
end when;
when x<=4.0 then
times[7] = time;
end when;
when x<=3.0 then
times[8] = time;
end when;
when x<=2.0 then
times[9] = time;
end when;
when x<=1.0 then
times[10] = time;
end when;
end RecordVariables;
Of course, writing out all those when
clauses is pretty tedious. So we can actually create a more compact version like this:
model RecordVariables2
Real x;
Real times[5];
Integer i;
Real next_level;
initial equation
next_level = 10.0;
x = 11;
i = 1;
algorithm
der(x) :=-x;
when x<=pre(next_level) then
times[i] :=time;
if i<size(times,1) then
i :=pre(i) + 1;
next_level :=next_level - 1.0;
end if;
end when;
end RecordVariables2;
A few comments about this approach. First, note the use of the pre
operator. This is necessary to distinguish between the values of the variables i
and next_level
both before and after the events generated by the when
clause. Second, you will note the if
statement within the when
clause that prevents the index i
from getting large enough to "overflow" the times
buffer. This allows you to set times
to have whatever size you want and never risk such an overflow. Note, however, that it is entirely possible in this model to make times
so large that some values will never be filled in.
I hope this helps.
回答2:
You can use DymBrowse.m to load variables from the resultfile to Matlab. It should be available in \Program Files\Dymola xx\Mfiles\dymtools. Add the directory \Mfiles... to your matlab paths.
来源:https://stackoverflow.com/questions/18233203/evaluating-variables-at-a-specific-time-in-modelica