如何在 Modelica 中设置 when 语句以限制时间事件生成? [英] How do I gate a when statement in Modelica to limit time event generation?
问题描述
假设我想为每天早上 01:00 发出 10 次哔哔声(每秒 1 次)的闹钟建模:
Suppose that I'd like to model an alarm clock that produces 10 beeps (one per second) every morning at 01:00:
model DailyBeep
import SI = Modelica.SIunits;
import Modelica.SIunits.Conversions.*;
constant SI.Time oneDay = 86459.17808 "one day in seconds";
parameter SI.Time startTime = from_hour(1) "time to start beeping";
parameter Real numBeeps = 10 "the number of beeps to make";
Boolean beeping "true when we should beep every second";
Real beepsRemaining "the number of beeps remaining";
initial equation
beeping = false;
beepsRemaining = numBeeps;
algorithm
when sample(startTime, oneDay) then
beeping := true;
beepsRemaining := numBeeps;
end when "starts the beeping state";
when beeping and sample(0, 1) then
if beepsRemaining == 0 then
beeping := false;
else
// beep() // makes a sound
beepsRemaining := beepsRemaining - 1;
end if;
end when "beep every second, only when beeping is enabled";
end DailyBeep;
在上面的模型中,只要 beeping
为真,我就会每秒产生一次哔"声(sample(0,1)
).如果我运行几天的模拟,我希望在每天早上 01:00 在我的模拟中得到 10 个时间事件.
In the model above, I produce a 'beep' every second (sample(0,1)
) so long as beeping
is true. I would expect to get 10 time events in my simulation at 01:00 each morning if I run the simulation for several days.
但是,在 OpenModelica 下将结束时间设置为 3600 秒的情况下运行模拟会导致超过 3600 个时间事件——每秒一个!
However, running the simulation with my end time set to 3600 seconds under OpenModelica results in slightly over 3600 time events--one for every second!
### STATISTICS ###
events
3601 time events
如果我想在几个月的时间内模拟我的闹钟,这不能很好地扩展.有没有办法在 Modelica 中对 when
语句进行门限,以便它们仅在启用时产生时间事件?在这种情况下,我应该使用其他东西而不是 when
语句吗?
This doesn't scale well if I want to simulate my alarm clock over a period of several months. Is there a way to gate when
statements in Modelica such that they only produce time events when they're enabled? Should I use something else in this case instead of a when
statement?
推荐答案
当您使用时间事件时,Modelica 将始终以高速率进行采样,您或许可以使用状态事件来绕过它.
Modelica will always sample at the high rate when you use time events, you might be able to get around it using state events.
我认为 Modelica 3.x 中还有另一种使用时钟和新同步功能的替代方法,您可以试试看.
I think there is another alternative using clocks and the new synchronous features in Modelica 3.x, you could try and see.
这篇关于如何在 Modelica 中设置 when 语句以限制时间事件生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!