在 Modelica 中评估特定时间的变量 [英] Evaluating variables at a specific time in Modelica

查看:57
本文介绍了在 Modelica 中评估特定时间的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Modelica 模拟(使用 Dymola)中选择了一些运行良好的变量.现在我想绘制某些这些变量的行为,这些变量被编号(带有索引).我不想绘制变量与时间的关系,而是与它们的索引.我打算使用 plotArray 函数,但这并不是我在这篇文章中真正感兴趣的.问题是,当我调用它们时,所有变量都为零,这确实是它们的初始值,但是我想在进入稳定状态时获得它们的值,假设 time = 5000.因此我需要在这个特定时间评估变量.

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?

经过广泛的谷歌搜索后,我找到了一个名为 val() 的函数,它似乎可以做我想做的事,但我无法得到它使用我的 Dymola 软件.

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.

编辑 2: 我已经设法根据需要评估了我的变量,但我使用的方法(在我看来)超出了乏味.我将模拟中的 .mat 文件提取到 MATLAB 中,在那里我最终设法识别出我想要的变量,然后在所需的时间绘制它们.然而,真正让我感到惊讶的是 .mat 文件中变量的明显混乱.乍一看,我的变量在 Modelica 模型中的排序方式与它们在 .mat 文件中的排序方式之间几乎没有一致,并且没有命名变量,让我仅根据比较变量来搜索变量值与 Dymola 模拟.我在这里完全错了,还是有一些更简单的方法可以从 .mat 文件中提取变量?

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?

推荐答案

也许我误解了你的问题,但我怀疑这里有一个简单的答案.听起来您有一个数组,并且您想在特定时间使用特定变量的值填充该数组,然后绘制该数组.因此,例如,假设您有一个变量 x,并且您想记录 x 跨越特定阈值的时间.像这样的简单模型就足够了:

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;

当然,写出所有那些 when 子句是相当乏味的.所以我们实际上可以像这样创建一个更紧凑的版本:

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;

关于这种方法的一些评论.首先,请注意 pre 运算符的使用.这对于区分 when 子句生成的事件之前和之后的变量 inext_level 的值是必要的.其次,您会注意到 when 子句中的 if 语句阻止索引 i 变得足够大以溢出" 缓冲.这允许您将 times 设置为您想要的任何大小,并且永远不会冒这样的溢出风险.但是请注意,在此模型中完全有可能使 times 变得很大,以至于某些值永远不会被填充.

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.

我希望这会有所帮助.

这篇关于在 Modelica 中评估特定时间的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆