Modelica 如何在一次模拟中逐个执行2个模型?

Modelica 如何在一次模拟中逐个执行2个模型?,modelica,openmodelica,dymola,Modelica,Openmodelica,Dymola,我试图通过状态图在模拟中一个接一个地执行两个模型。下面是伪代码。在模型测试中,有两个块,stateA和stateB。在模拟开始时,执行stateA。10秒后,stateA停止,而stateB执行。停止点处的v值需要从stateA转移到stateB。谁能给我一些建议吗?非常感谢 另一件事是为什么我不能在openModelica中模拟下面的简单示例 model StateMachine1 inner Integer i(start=0); block State1 outer

我试图通过状态图在模拟中一个接一个地执行两个模型。下面是伪代码。在模型测试中,有两个块,stateA和stateB。在模拟开始时,执行stateA。10秒后,stateA停止,而stateB执行。停止点处的v值需要从stateA转移到stateB。谁能给我一些建议吗?非常感谢



另一件事是为什么我不能在openModelica中模拟下面的简单示例

model StateMachine1
  inner Integer i(start=0);

  block State1
    outer output Integer i;
  equation
    i = previous(i) + 2;
  end State1;
  State1 state1;

  block State2
    outer output Integer i;
  equation
    i = previous(i) - 1;
  end State2;
  State2 state2;

equation
  initialState(state1);
  transition(state1, state2, i > 10, immediate=false);
  transition(state2, state1, i < 1, immediate=false);
end StateMachine1;
模型状态机1
内部整数i(开始=0);
块状态1
外输出整数i;
方程式
i=先前(i)+2;
结束状态1;
状态1状态1;
块状态2
外输出整数i;
方程式
i=先前(i)-1;
结束状态2;
状态2状态2;
方程式
初始状态(状态1);
转换(state1,state2,i>10,immediate=false);
转换(state2,state1,i<1,immediate=false);
终端状态机1;

错误:在scope StateMachine1中找不到类initialState。有关Modelica 3.3规范中引入的新状态机功能的资料中详细描述了您尝试执行的操作。您可以在下面找到一些参考资料:

您的模型中的错误是,在Modelica 3.3中引入了对所有变量的采样时间推断,即时间离散化不同的离散变量不能用于相同的方程,换句话说,您必须仅使用方程中具有相同时钟的变量。在你的例子中,你有一个连续变量(时间),和一个离散变量v。因此,您不能将时间用于影响离散变量v行为的转换语句(在modelica代码转换期间,它将成为一个等式)。为了满足您的需要,一种解决方案是为模型中的所有变量定义一个时钟,然后构建逻辑(如果您需要多个采样时间,则必须使用特殊运算符,如superSample)


关于您的模型,我认为实现它的最佳方式是将三个模型编码在三个不同的类中,然后有一个包装器,其中包含我们模型的所有变量,然后在包装器中实现when-elsewhen-elsewhen语句,该语句将重新启动已激活模型的状态变量,最后是if-elseif-else语句,用于将包装器变量链接到活动块的对应变量。通过这种方式,您还可以更改模型中变量的因果关系,而不会导致错误。作为一个例子,我发布了一个钟摆模型断裂的案例:

model BreakingPendulum2
extends BasePendulum(p(m=1, g=9.81, L=0.5));
input Boolean Broken;
protected
Pendulum pend (p=p, u=u, enable=not Broken);
BrokenPendulum bpend(p=p, u=u, enable=Broken);
equation
when Broken then
reinit(bpend.pos, pend.pos);
reinit(bpend.vel, pend.vel);
end when;
pos = if not Broken then pend.pos else bpend.pos;
vel = if not Broken then pend.vel else bpend.vel;
end BreakingPendulum2;

类basebe摆实现了您需要计算的有关系统的变量,这些变量是模拟的。从cass开始,您实现了在不同情况下有效的三个不同版本的模型。然后编写一个类似于我发布的块,并根据不同模型的有效性实现逻辑。

你好,Marco,非常感谢!你的回答很有用。我现在正在阅读参考资料。实际上,我的案例是这样的:我用3种不同的模型建模了一个流程,这个流程的行为根据状态变量v的不同而不同。所以模型1对xx0.75有效。我不确定使用状态机是否是实现它的最佳方式?还是有更好的?Marco,你能告诉我你用哪个工具进行modelica编程吗?为什么我不能在openModelica或dymola中执行您的代码?嗨,Rui,我使用dymola,但要执行我的模型,您必须运行dymola 2013FD01并设置标志:Advanced.Synchronous=true;这样,您将能够支持新的Modelica 3.3功能和语法。非常感谢您,Marco!很抱歉,我没有及时给出反馈。实际上,我用了类似的方法来解决这个问题。再次感谢!
    model StateMachine3
  Real i(start = 0,fixed=true);
  inner Real id(start = 2);
  Real td(start = 0,fixed=true);

  State1 state1;

  model State1
    outer output Real id;
  equation 
    id = 2;
  end State1;

  model State2
    outer output Real id;
  equation 
    id = - 1;
  end State2;
  State2 state2;
equation 
  td = sample(time, Clock(1, 10));
  when Clock(1, 10) then
    i =  previous(i) + id;
  end when;
  initialState(state1) ;
  transition(
    state1,
    state2,td > 10,
    immediate=false,
    reset=false,
    priority=1,synchronize=false);
end StateMachine3;
model BreakingPendulum2
extends BasePendulum(p(m=1, g=9.81, L=0.5));
input Boolean Broken;
protected
Pendulum pend (p=p, u=u, enable=not Broken);
BrokenPendulum bpend(p=p, u=u, enable=Broken);
equation
when Broken then
reinit(bpend.pos, pend.pos);
reinit(bpend.vel, pend.vel);
end when;
pos = if not Broken then pend.pos else bpend.pos;
vel = if not Broken then pend.vel else bpend.vel;
end BreakingPendulum2;