OpenModelica交互式仿真OPC UA接口变量声明问题求助
问题背景
搭建燃气管道仿真模型用于模拟充气过程、验证密封性测试流程,计划通过OpenModelica内置OPC UA接口连接PLC程序。交互式仿真时OPC UA嵌入式服务器正常启动,可通过OPC Expert浏览模型对象并实时查看数据刷新。
最初基于仿真时间触发阀门动作的代码:
Real leak ; equation leak = if (time > 20) then 0.01 else 0.0;// no leak at staring up to help init CO0011_FSV02.opening = if (time > 60) and (time < 70) then 0.05 else if time > 110 then 1 else leak; CO0011_FSV03.opening = if (time > 20) and (time < 30) or ( time > 110 ) then 1 else leak; CO0011_FSV04.opening = if (time > 10) and (time < 100) then 1 else leak;
改为通过PLC经OPC UA写入布尔变量控制阀门后,代码如下:
//valves commands from PLC Boolean YV01; Boolean YV02; Boolean YV03; Boolean YV04; equation leak = if (time > 20) then 0.01 else 0.0;// no leak at staring up to help init // CO0011_FSV02.opening = if (time > 60) and (time < 70) then 0.05 else if time > 110 then 1 else leak; // CO0011_FSV03.opening = if (time > 20) and (time < 30) or ( time > 110 ) then 1 else leak; // CO0011_FSV04.opening = if (time > 10) and (time < 100) then 1 else leak; CO0011_FSV02.opening = if YV02 then 0.05 else if YV01 then 1 else leak; CO0011_FSV03.opening = if YV03 then 1 else leak; CO0011_FSV04.opening = if YV04 then 1 else leak;
出现以下错误:
[2] 18:59:41 Symbolique Erreur
Too few equations, under-determined system. The model has 239 equation(s) and 243 variable(s).[3] 18:59:41 Symbolique Avertissement
[Gas_supply_test: 34:3-34:15]: Variable YV01 does not have any remaining equation to be solved in.
The original equations were:
Equation 204: CO0011_FSV02.opening = if YV02 then 0.05 else if YV01 then 1.0 else leak, which needs to solve for YV02
解决方案
错误原因
声明的YV01-YV04是普通变量,OpenModelica要求每个变量都有对应的求解方程,但这些变量是外部输入(来自PLC的OPC UA写入),没有提供方程导致系统欠定。
正确声明方式
使用input前缀声明变量,将其标记为外部输入变量,OpenModelica会自动跳过对这类变量的方程求解要求,同时支持交互式仿真在线修改、OPC UA服务器暴露可写接口:
//valves commands from PLC input Boolean YV01 = false; // 设置默认值确保初始化正常 input Boolean YV02 = false; input Boolean YV03 = false; input Boolean YV04 = false; equation leak = if (time > 20) then 0.01 else 0.0;// no leak at staring up to help init CO0011_FSV02.opening = if YV02 then 0.05 else if YV01 then 1 else leak; CO0011_FSV03.opening = if YV03 then 1 else leak; CO0011_FSV04.opening = if YV04 then 1 else leak;
关键说明
- 给
input变量设置默认值是可选但推荐的操作,能避免仿真初始化时出现未定义变量的问题。 - 不要使用
parameter声明:参数是编译时确定的常量,无法在仿真运行时修改,也不会被OPC UA服务器识别为可写变量。 - 使用
input声明后,系统方程数与变量数会匹配,解决欠定系统错误,同时满足OPC UA写入控制、交互式仿真在线调整的需求。
内容的提问来源于stack exchange,提问作者Piwi447

