Dynamically switching connect in Modelica

爷,独闯天下 提交于 2019-12-10 20:18:58

问题


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;

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

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;

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;

回答1:


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.




回答2:


Note that connect statements are equations. You can expand them out yourself. They exist mainly to avoid "bookkeeping" errors for generating boilerplate equations. So what I suggest you do is simply take your switch model and expand each connect into equations. The it should work.



来源:https://stackoverflow.com/questions/29189519/dynamically-switching-connect-in-modelica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!