Modelica Triggered Trapezoid模块使用异常:输出值错误咨询
Triggered Trapezoid模块输出异常问题排查
我使用Modelica逻辑模块库中的Triggered Trapezoid模块,目的是消除模型中某一变量的峰值——该变量由名为ON的布尔量触发,当ON为1的初始几秒内,变量会出现峰值。但使用该模块后,变量输出值异常,以下是我编写的模型代码片段:
model prog Real y; Boolean u; protected discrete Real endValue "Value of y at time of recent edge"; discrete Real rate "Current rising/falling rate"; discrete Modelica.SIunits.Time T "Predicted time of output reaching endValue"; equation amplitude = Var; u = ON; y = if time < T then endValue - (T - time) * rate else endValue; when {initial(), u, not u} then endValue = if u then offset + amplitude else offset; rate = if u and rising > 0 then amplitude / rising else if not u and falling > 0 then -amplitude / falling else 0; T = if u and not rising > 0 or not u and not falling > 0 or not abs(amplitude) > 0 or initial() then time else time + (endValue - pre(y)) / rate; end when; end prog;
问题分析与修复建议
- 未声明变量报错:代码中用到的
amplitude、offset、rising、falling、Var、ON均未在模型变量段声明,Modelica会直接抛出未定义变量错误。需在模型开头补充这些变量的声明:Real Var; // 输入变量,为amplitude提供来源 Boolean ON; // 触发用布尔量 Real amplitude; // 梯形波幅值 Real offset; // 输出偏移量 Modelica.SIunits.Time rising; // 上升沿时间 Modelica.SIunits.Time falling; // 下降沿时间 - 初始值未定义:初始触发(
initial())时,pre(y)的值没有初始参考,y的初始状态不明确。需给y添加初始值,比如:Real y(start=offset); - 逻辑表达式优先级歧义:
T的赋值语句中,and与or的优先级未明确,可能导致逻辑判断出错。建议用括号明确分组:T = if (u and not rising > 0) or (not u and not falling > 0) or (not abs(amplitude) > 0) or initial() then time else time + (endValue - pre(y)) / rate; - 除零风险:当
rising或falling为0时,rate会被设为0,若此时endValue - pre(y)不为0,计算(endValue - pre(y))/rate会触发除零错误。需在T的判断逻辑中优先处理rate=0的场景,避免非法运算。
内容的提问来源于stack exchange,提问作者Dahmani Merzaka
相关产品推荐
相关产品推荐

