modelica:计算随时间变化的连续变量的最小值/最大值 [英] modelica: compute minimum/maximum of continuous variable over time

查看:624
本文介绍了modelica:计算随时间变化的连续变量的最小值/最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如上所述:我希望计算连续变量随时间的最小值(和/或最大值).这是一个最小的示例来演示:

As stated above: I wish to compute the minimum (and/or maximum) of a continuous variable over time. Here is a minimal example to demonstrate:

model MinMaxTest
  Real u;
  Real u_min(start = 10);
  Real u_max(start = -10);
equation
  u = sin(time / 180 * Modelica.Constants.pi);
  u_min = min(u, u_min);
  u_max = max(u, u_max);
  annotation(experiment(StartTime = 0, StopTime = 360, Tolerance = 1e-06, Interval = 1));
end MinMaxTest;

u是任意连续变量(出于演示目的,是一个简单的正弦波). u_min/u_max是一段时间内的最小值/最大值.

u is the arbitrary continuous variable (for demo purposes a simple sinus wave). u_min/u_max is the minimum/maximum over time.

显然,预期结果是u_min=-1u_max=1.不幸的是,仿真因矩阵奇异点"而崩溃!错误.谁能指导我如何避免这种情况?

Obviously the expected result is u_min=-1 and u_max=1. Unfortunately the simulation crashes with a "Matrix singular!" error. Can anyone direct me how to avoid that?

我正在使用OpenModelica 1.15(原为1.9.2)

I'm using OpenModelica 1.15 (was 1.9.2)

由于我对Modelica还是很陌生,所以我正努力了解以下方法之间的区别:

As I'm quite new to Modelica, I'm struggling to understand the differences between the following approaches:

  1. u_min = if noEvent(u < u_min) then u else pre(u_min);
  2. if noEvent(u < u_min) then u_min = u; else u_min = pre(u_min); end if;
  3. u_min = if noEvent(u < u_min) then u else u_min;
  4. u_min = if u < u_min then u else pre(u_min);
  5. u_min = if u < u_min then u else u_min;
  6. when u < u_min then u_min = u; end when;
  7. u_min + T*der(u_min) = if u <= u_min then u else u_min;
  1. u_min = if noEvent(u < u_min) then u else pre(u_min);
  2. if noEvent(u < u_min) then u_min = u; else u_min = pre(u_min); end if;
  3. u_min = if noEvent(u < u_min) then u else u_min;
  4. u_min = if u < u_min then u else pre(u_min);
  5. u_min = if u < u_min then u else u_min;
  6. when u < u_min then u_min = u; end when;
  7. u_min + T*der(u_min) = if u <= u_min then u else u_min;

1和2是等效的,并导致预期的行为.

1 and 2 are equivalent and result in the expected behavior.

3产生期望的结果,但是给出有关代数循环"的翻译通知",为什么?

3 produces the desired result but gives a "Translation Notification" about an "algebraic loop", why?

4失败,因此生成的u_min曲线与u相同?为什么?

4 fails in so far, that the resulting u_min curve is identical to u?! why?

5结合了3和4.

6无法使用Sorry - Support for Discrete Equation Systems is not yet implemented

7我不清楚这背后的想法是什么,但是如果T具有建议的大小,它会起作用.

7 I'm unclear what the idea behind this is, but it works if T is of the suggested size.

如果我正确地理解了Modelica文档,则1-5的共同点是,始终有一个方程始终处于活动状态. noEvent禁止在指定的零交叉处生成事件.我的印象是,这主要是效率的提高.为什么忽略它会导致4失败? pre引用变量的先前值,所以我想如果我们想使变量保持恒定是有道理的,但是为什么7在没有该变量的情况下会起作用呢?我对when的理解是,它的方程式仅在该精确事件上有效,否则保留先前的值,这就是为什么我尝试在6中使用它.针对常量值(对于此特定问题没有用).

If I'm understanding the Modelica documentation correctly then 1-5 have in common that exactly one equation is active at all times. noEvent suppresses event generation at the specified zero crossing. I had the impression that this is mostly an efficiency improvement. Why does leaving it out cause 4 to fail? pre refers to the previous value of the variable, so I guess that makes sense if we want to keep a variable constant, but why does 7 work without it? My understanding of when was, that its equation is only active at that precise event, and otherwise keeps the previous value, which is why I tried using it in 6. It seems to work if I compare against constant values (which is of no use for this particular problem).

  1. u_min = smooth(0, if u < u_min then u else pre(u_min));

有趣的是,这也起作用.

Interestingly, this works also.

推荐答案

我用Dymola 2016测试了您的模型,它可以工作,但是您可以尝试使用其他方法.在Modelica中,您必须根据方程式而不是赋值进行思考.

I tested your model with Dymola 2016 and it works, however you can try to use an alternative approach. In Modelica you have to think in terms of equations and not in terms of assignments.

u_min = min(u, u_min);

如果代码要按照一系列指令执行,那将是您要做的事情.在底层,Modelica工具可以转换该方程式 变成一个非线性系统,随着仿真的进行,该系统将得到解决.

Is what you would do if the code were to be executed as a sequence of instructions. Under the hood the Modelica tool converts this equation into a nonlinear system that is solved as the simulation proceed.

这些是我在模拟模型时获得的统计信息

These are the statistics I get when simulating your model

Statistics

Original Model
Number of components: 1
Variables: 3
Unknowns: 3 (3 scalars)
Equations: 3
Nontrivial: 3

Translated Model
Time-varying variables: 3 scalars
Number of mixed real/discrete systems of equations: 0
Sizes of linear systems of equations: { }
Sizes after manipulation of the linear systems: { }
Sizes of nonlinear systems of equations: {1, 1}
Sizes after manipulation of the nonlinear systems: {1, 1}
Number of numerical Jacobians: 0

如您所见,有两个非线性系统,一个用于u_min,一个用于u_max.

As you can see there are two nonlinear systems, one for u_min and one for u_max.

以下是您解决问题的另一种方法

An alternative solution to you problem is the following

model Test
  Real x;
  Real y;
  Real u_min;
  Real u_max;
  parameter Real T = 1e-4;
equation 
  x = sin(time) + 0.1*time;
  y = sin(time) - 0.1*time;
  u_min + T*der(u_min) = if y <= u_min then y else u_min;
  u_max + T*der(u_max) = if x >= u_max then x else u_max;

end Test;

在这种情况下,u_minu_max是两个状态变量,它们遵循 变量x和y,取决于它们的值.例如,当x低于u_max时,u_max会卡住"到该时间点之前达到的最大值.

In this case u_min and u_max are two state variables and they follow the variables x and y, depending on their values. For example, when x is lower than u_max then u_max gets "stuck" to the maximum value reached up to that point in time.

很抱歉,但是我无法发布正在运行的模型的图片,因为这是我的第一份答复.

Sorry but I can't post an image of the model running since this is my first reply.

这篇关于modelica:计算随时间变化的连续变量的最小值/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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