首页 文章

在Modelica中的特定时间评估变量

提问于
浏览
1

我在Modelica模拟中使用了一系列变量(使用Dymola),这些变量运行良好 . 现在我想绘制某些变量的行为,这些变量被编号(带索引) . 我不希望绘制变量与时间的关系,而是与它们的索引相比较 . 我打算使用 plotArray 函数,但是在这篇文章中我很好奇 . 问题是,当我调用它们时,所有变量都为零,这确实是它们的初始值,但是当稳态已经设置时我想达到它们的值,让我们说 time = 5000 . 因此,我需要在这个特定时间评估变量 .

如何在模拟的特定时间或模拟结束时评估变量?

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

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

2 回答

  • 2

    也许我说'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'你说你有一个变量 x 并且你想记录 x 越过某些阈值的时间 . 像这样的简单模型就足够了:

    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;
    

    当然,写出所有这些条款是相当繁琐的 . 所以我们实际上可以创建一个更紧凑的版本:

    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 缓冲区 . 这允许您将 times 设置为具有您想要的任何大小,从不冒这样的溢出风险 . 但请注意,在此模型中完全有可能使 times 如此之大,以至于某些值永远不会被填充 .

    我希望这有帮助 .

  • 1

    您可以使用DymBrowse.m将结果文件中的变量加载到Matlab . 它应该在\ Program Files \ Dymola xx \ Mfiles \ dymtools中可用 . 将目录\ Mfiles ...添加到matlab路径 .

相关问题