Modelica-增量不';我不遵守条件

Modelica-增量不';我不遵守条件,modelica,systemmodeler,Modelica,Systemmodeler,我正在Wolfram System Modeler中创建每间隔最大块 为了便于解释,我只将最大值设置为10 block HighWaterMarkPerInterval extends Modelica.Blocks.Interfaces.SISO; protected Integer index; Real currentMax; Real endTimes[1, 45] = [30812532.2, 32037805, 33265581.8, 34493233.8, 3572

我正在Wolfram System Modeler中创建每间隔最大块

为了便于解释,我只将最大值设置为10

block HighWaterMarkPerInterval
  extends Modelica.Blocks.Interfaces.SISO;
protected
  Integer index;
  Real currentMax;
  Real endTimes[1, 45] = [30812532.2, 32037805, 33265581.8, 34493233.8, 35720861.5, 36948483, 38176307.7, 39426940.6, 40654485.4, 41882212.1, 43109672.7, 44337076, 45564265.7, 46793039.6, 48045130.9, 50749960.3, 52040090.6, 53558507.7, 54814537.3, 56331978.2, 57587753.3, 59105952.9, 60362517.8, 61879307.8, 63136031.5, 64363411.4, 65590464.3, 67738027.40000001, 84725789.8, 87831338.09999999, 89030965.40000001, 90258821.8, 91486663.5, 92714210.3, 93941727.7, 95166770.3, 97283519, 99434222.90000001, 100658067.1, 102807019, 104030032.7, 106179193, 107402090, 109550214.2, 110771545.3];
algorithm
  if endTimes[1, index] < time then
    index := pre(index) + 1;
    currentMax := 0;
  else
    currentMax := 10; // Constant to until I get logic working
  end if;
initial algorithm
  index := 0;
equation
  y = currentMax;
end HighWaterMarkPerInterval;
block HighWaterMarkPerInterval
扩展Modelica.Blocks.Interfaces.SISO;
受保护的
整数指数;
实际最大电流;
实际结束时间[1,45]=[30812532.2, 32037805, 33265581.8, 34493233.8, 35720861.5, 36948483, 38176307.7, 39426940.6, 40654485.4, 41882212.1, 43109672.7, 44337076, 45564265.7, 46793039.6, 48045130.9, 50749960.3, 52040090.6, 53558507.7, 54814537.3, 56331978.2, 57587753.3, 59105952.9, 60362517.8, 61879307.8, 63136031.5, 64363411.4, 65590464.3, 67738027.40000001, 84725789.8, 87831338.09999999, 89030965.40000001, 90258821.8, 91486663.5, 92714210.3, 93941727.7, 95166770.3, 97283519, 99434222.90000001, 100658067.1, 102807019, 104030032.7, 106179193, 107402090, 109550214.2, 110771545.3];
算法
如果endTimes[1,index]<时间,则
索引:=pre(索引)+1;
currentMax:=0;
其他的
currentMax:=10;//在逻辑正常工作之前为常量
如果结束;
初始算法
指数:=0;
方程式
y=电流最大值;
末期高水位;
运行时,索引立即递增到无穷大。我认为我的逻辑有问题,但我无法理解

代码应该检查我们是否仍在间隔时间内,当我们进入下一个间隔时间时,它将“currentMax”值设置为零。这将重置我在另一个块中实现的最大值

任何帮助都将不胜感激。谢谢

编辑:代码部分表单示例

model HighWaterMarkPerInterval
  annotation(Diagram(coordinateSystem(extent = {{-148.5, -105}, {148.5, 105}}, preserveAspectRatio = true, initialScale = 0.1, grid = {5, 5})));
  extends Modelica.Blocks.Interfaces.SISO;
  Modelica.Blocks.Math.Max maxblock(u1 = currentMax, u2 = u);
  Real flybyEnds[1, 45] = [30813151,32038322,33266015, truncated for space saving...];
  Integer index;
  Real currentMax;
initial equation
  index = 1;
  currentMax = 0;  
algorithm
  // When we are in the interval continually grab max block output and output currentMax
  when {time>=flybyEnds[1, index-1], time <=flybyEnds[1,index]} then 
    currentMax := pre(maxblock.y);
    y := currentMax;
  end when;
  // When we move to the next interval reset current max and move to the next interval
  when time > flybyEnds[1, index] then
    currentMax := 0;
    index := pre(index) + 1;
  end when;
end HighWaterMarkPerInterval;
model-HighWaterMarkPerInterval
注释(图(坐标系统(范围={-148.5,-105},{148.5,105}},保留AspectRatio=true,initialScale=0.1,网格={5,5}));
扩展Modelica.Blocks.Interfaces.SISO;
Modelica.Blocks.Math.Max maxblock(u1=currentMax,u2=u);
实际飞越终点[1,45]=[308131513203832233266015,为节省空间而截断…];
整数指数;
实际最大电流;
初始方程
指数=1;
currentMax=0;
算法
//当我们处于区间时,连续抓取最大块输出和输出电流最大值
当{time>=flybeyends[1,index-1]时,时间flybeyends[1,index]
currentMax:=0;
索引:=pre(索引)+1;
结束时;
末期高水位;

您需要在时使用,而不是在时使用。您可以在中找到关于这两者以及它们之间差异的讨论

这一问题也在SO和上进行了讨论

下面是一个示例(完全未经测试,但它显示了基本思想):


过去我尝试过实现
when
,但我不知道如何使用when功能重置它。如果理解正确,我需要
when else
语句的功能来执行我试图执行的操作。你能解释一下“重置它”是什么意思吗是的,对不起。
currentMax
应该在每个
endTime
重置。我试图为每个间隔计算一个
currentMax
值,而不是在整个模拟过程中计算一个高水位线。
endTimes
对应于间隔结束时间。你应该看看。我想它会解释你所做的一切nt to do。它计算给定时间间隔内轴的旋转次数以估计速度。似乎与您想要做的事情非常相似。您不了解这里的
示例的用法。它不是要捕获飞越间隔。它是要在飞越间隔内采样。您正试图记录极值,但无法明确检测。因此,使用
sample
检查当前值是否大于当前飞行间隔中的任何先前采样值。该检查使用
sample
完成,因为它无法“连续”完成在Modelica中,采样周期必须比飞越间隔短得多。
model HighWaterMarkPerInterval
  extends Modelica.Blocks.Interfaces.SISO;
  parameter Modelica.SIunits.Time sample_rate=3600;
  Real flybyEnds[45] = {30813151,32038322,33266015,...};
  Integer index;
  Real currentMax;
initial algorithm
  // Specify the first time we are interested in...
  index := 1;
algorithm 
  // At the start of the simulation, the initial max for the current
  // interval [0,30813151] is whatever u is.  The initial output value
  // is also the initial value for u
  when initial() then
    currentMax := u
    y := u;
  end when;

  // Check at some sample rate (faster than the flyby interval!)
  // if u > currentMax...
  when sample(sample_rate, sample_rate) then
    // New currentMax is the larger of either currentMax or u
    // when the sample took place
    currentMax := max(pre(currentMax), pre(u));
  end when;

  // At the end of the "flyby", record the maximum found since
  // the last flyby and specify the next flyby index.
  when time>=flybyEnd[index] then
    // New output is the value of currentMax from this interval
    y := pre(currentMax);
    // Now reset currentMax
    currentMax := pre(u);
    // Increment index up to the length of flybyEnd
    index := min(pre(index)+1, size(flybyEnd,1));
  end when;
end HighWaterMarkPerInterval;