如何在不影响仿真性能的情况下检查终止过程的绝对时间约束?



我想读取当前日期,如果日期在约束之外,那么系统应该终止。下面的代码可以工作。

import Modelica.Utilities.System.getTime; 
model M
Integer[7] today;
constant Integer[7] limit={0,0,0,0,0,0,2021};
Real x;
equation
today = getTime();
if today[7] > limit[7] then
terminate();
end if;
// Process
x = 1;
end M;

但是最好在启动时读取日期,避免一直检查日期,这应该是一个负担,尽管运行上面的代码并不明显。

最合乎逻辑的将是一个初始算法(或方程)部分,使用getTime()获取日期,并在适当时使用terminate()。

但只需将当前部分更改为&;initial equation&;给出编译错误,错误文本为:"…以下变量不能与任何方程匹配:今天[1],....今天[7]!">

模型中不需要today,因此可以将其移动到函数中:

import Modelica.Utilities.System.getTime; 
function afterLimit
input Integer limit[7];
output Boolean after;
protected
Integer[7] today;
algorithm
(,,,,,,today[7]) := getTime();
after := today[7]>limit[7];
end afterLimit;
model M
constant Integer[7] limit={0,0,0,0,0,0,2021};
Real x;
initial equation
if afterLimit(limit) then
terminate("Time is up!");
end if;
equation
// Process
x = 1;
end M;

或者,您可以使用fixed=false作为参数,如@ReneJustNielsen

所建议的那样

最新更新