在 Modelica 中动态切换连接 [英] Dynamically switching connect in Modelica

查看:19
本文介绍了在 Modelica 中动态切换连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含各种基本类型(Real、Integer、String、Boolean)的大型连接器.如何根据状态事件切换连接?我想做这样的事情:

Assume I have a large connector involving all kinds of base types (Real, Integer, String, Boolean). How can I switch connections based on state events? I would like to do something like this:

model switch
 input ComplicatedConnector icon[2];
 output ComplicatedConnector ocon;
 input Real x;
 equation
   if x >= 0 then
     connect(ocon, icon[1]);
   else
     connect(ocon, icon[2]);
   end if;
end switch;

这不起作用.如何在 Modelica 中正确表达?

This does not work. How can it be properly expressed in Modelica?

根据 Adrian Pop 的评论回答.

Answer based on comment by Adrian Pop.

model switch
 input ComplicatedConnector icon[2];
 output ComplicatedConnector ocon;
 input Real x;
 ComplicatedConnector con;
 initial equation
   con = icon[1];
 equation
   connect(ocon, con);
   when x >= 0 then
     con := icon[1];
   end when;
   when x < 0 then
     con := icon[2];
   end when;
end switch;

更新:上面的模型是错误的,因为如果没有事件发生,ocon 将永远输出 icon[1] 的初始值,这不是您期望从 switch 中得到的.请注意,这不是由于错误的答案,而是由于我对答案的错误解释.以下模型基于 Michael Tiller 的回答.

Update: The model above is wrong because ocon outputs the initial value of icon[1] forever if no event occurs which is not what you would expect from a switch. Note that this is not due to a wrong answer but due to my false interpretation of the answer. The following model is based on the answer by Michael Tiller.

model switch
 input ComplicatedConnector icon[2];
 output ComplicatedConnector ocon;
 input Real x;
 Integer k;
 initial equation
   k = 1;
 equation
   ocon = icon[k];
   when x >= 0 then
     k := 1;
   elsewhen x < 0 then
     k := 2;
   end when;
end switch;

推荐答案

不可能.您只能根据编译时已知的参数(也称为结构参数)来切换它们.包含connects的if方程中的条件需要是参数表达式.

Is not possible. You can only switch them based on a parameter known at compile time (also known as structural parameter). The condition in the if equation containing connects needs to be a parameter expression.

这篇关于在 Modelica 中动态切换连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆