如何在不影响仿真性能的情况下检查终止进程的绝对时间约束?
How to check constraint on absolute time to terminate process without affecting simulation performance?
我想读取当前日期,如果日期超出限制,则系统应该终止。下面的代码有效。
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() 获取日期(如果适用)。
但是只要将当前部分更改为“初始方程”就会出现编译错误,错误文本为:“...以下变量无法与任何方程匹配:今天[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;
或者,您可以按照@ReneJustNielsen
的建议,将其设为带有 fixed=false
的参数
我想读取当前日期,如果日期超出限制,则系统应该终止。下面的代码有效。
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() 获取日期(如果适用)。
但是只要将当前部分更改为“初始方程”就会出现编译错误,错误文本为:“...以下变量无法与任何方程匹配:今天[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;
或者,您可以按照@ReneJustNielsen
的建议,将其设为带有fixed=false
的参数