如何通过modelica语言实现从一个卷到上一个卷的回流?

如何通过modelica语言实现从一个卷到上一个卷的回流?,modelica,Modelica,这是一个简单的想法,当下一个体积的变量x小于0时,我想在有限体积中求解相同的方程。由于modelica语言的特点,我很难实现循环。我希望有人能帮我。谢谢 model Model parameter Real L = 10; parameter Real r = 5; parameter Integer N = 20; Real x[N](each start = 0); equation if noEvent(x[1] >= 0 and x[1] < L) th

这是一个简单的想法,当下一个体积的变量x小于0时,我想在有限体积中求解相同的方程。由于modelica语言的特点,我很难实现循环。我希望有人能帮我。谢谢

model Model
  parameter Real L = 10;
  parameter Real r = 5;
  parameter Integer N = 20;
  Real x[N](each start = 0);
equation 
  if noEvent(x[1] >= 0 and x[1] < L) then 
    der(x[1]) = r;
  else
    der(x[1]) = 0;
  end if;
  for i in 2:N loop 
    if noEvent(x[i - 1] >= L and x[i] >= 0 and x[i] < L) then 
      der(x[i]) = r - 1 * time;
    elseif noEvent(x[i] < 0) then 
      der(x[i - 1]) = r - x[i - 1];
    else
      der(x[i]) = 0;
    end if;
  end for;
end model;

我没有工作,只有迪莫拉。如果我们在Dymola中模拟您的代码,我们会得到以下错误消息:

Error: The following error was detected at time: 7.99999999711724
Error: Singular inconsistent scalar system for 
  der(x[2]) = ( -(if x[1] >= 10 and x[2] >= 0 and x[2] < 10 then 1*time-5 else (if x[2] < 0 then der(x[1])+x[1]-5 else 0.0)))/((if x[1] >= 10 and x[2] >= 0 and x[2] < 10 then 1.0 else (if x[2] < 0 then 0.0 else 1.0))) = -5/0
这源于for循环中的elseif,您在其中写入:

elseif noEvent(x[i] < 0) then 
  der(x[i - 1]) = r - x[i - 1];
对方程进行因果化后,我们得到:

der(x[i]) := - (der(x[i - 1]) - r + x[i - 1]) / 0
为了避免被零除,必须重写for循环,以便在if语句的每个分支中使用
der(x[i])
。下面是这样一个示例:

model Model2
  parameter Real L = 10;
  parameter Real r = 5;
  parameter Integer N = 20;
  Real x[N](each start = 0);
equation 
  if x[1] >= 0 and x[1] < L then
    der(x[1]) = r;
  else
    der(x[1]) = 0;
  end if;

  for i in 2:N-1 loop
    if x[i - 1] >= L and x[i] >= 0 and x[i] < L then
      der(x[i]) = r - 1 * time;
    elseif x[i+1] < 0 then
      der(x[i]) = r - x[i];
    else
      der(x[i]) = 0;
    end if;
  end for;

  if x[N - 1] >= L and x[N] >= 0 and x[N] < L then
      der(x[N]) = r - 1 * time;
    else
      der(x[N]) = 0;
    end if;
end Model2;
模型2
参数实L=10;
参数实数r=5;
参数整数N=20;
实x[N](每次启动=0);
方程式
如果x[1]>=0且x[1]=L,x[i]>=0,且x[i]=L,x[N]>=0,且x[N]
elseif分支现在展望下一个元素。这要求在单独的方程中处理
x
的最后一个元素,就像处理第一个一样

这段代码现在模拟的是8秒以上,但您的方程式似乎有一些问题。8s后没有任何事情发生,因为
x[2]
从未到达
L
。我想你必须重新考虑等式
der(x[I])=r-1*time(或完整模型)来解决这个问题


请注意,我还删除了所有
noEvent()
操作符,因为我希望您的模型在状态事件中运行得更好。

谢谢您的回答!但是我认为没有x[2]<0的情况。你能再考虑一下吗?也许我有点困惑。谢谢,我在自己的真实代码中用你的方法得到了正确的解决方案。上面的代码只是一个简单的例子来表达我的想法。干得好我真的很感激。使用内联for循环和内联if表达式(而不是if语句)是另一种确保不同分支中的方程数量相同的方法。
0 * der(x[i]) + der(x[i - 1]) = r - x[i - 1]
der(x[i]) := - (der(x[i - 1]) - r + x[i - 1]) / 0
model Model2
  parameter Real L = 10;
  parameter Real r = 5;
  parameter Integer N = 20;
  Real x[N](each start = 0);
equation 
  if x[1] >= 0 and x[1] < L then
    der(x[1]) = r;
  else
    der(x[1]) = 0;
  end if;

  for i in 2:N-1 loop
    if x[i - 1] >= L and x[i] >= 0 and x[i] < L then
      der(x[i]) = r - 1 * time;
    elseif x[i+1] < 0 then
      der(x[i]) = r - x[i];
    else
      der(x[i]) = 0;
    end if;
  end for;

  if x[N - 1] >= L and x[N] >= 0 and x[N] < L then
      der(x[N]) = r - 1 * time;
    else
      der(x[N]) = 0;
    end if;
end Model2;