Modelica流量模拟表示除以零

Modelica流量模拟表示除以零,modelica,openmodelica,Modelica,Openmodelica,我试图模拟管道中的流动,开始部分充满,然后逐渐充满。一旦满了,流量方程需要改变。我尝试了下面的代码,但是一旦管道满了,我会得到一个被零除的错误。TestTrap1是一个演示程序,演示了这个问题。似乎有些等式在管道装满后就不正确了。 我正在使用openmodelica运行此操作 package FlowPackage package Interfaces extends Modelica.Icons.InterfacesPackage; //Use this for

我试图模拟管道中的流动,开始部分充满,然后逐渐充满。一旦满了,流量方程需要改变。我尝试了下面的代码,但是一旦管道满了,我会得到一个被零除的错误。TestTrap1是一个演示程序,演示了这个问题。似乎有些等式在管道装满后就不正确了。 我正在使用openmodelica运行此操作

    package FlowPackage
  package Interfaces
    extends Modelica.Icons.InterfacesPackage;

    //Use this for a inlet outlet device

    connector Fitting "Inlet port"
      flow Modelica.SIunits.MassFlowRate q "Flow rate [Kg/s]";
  Modelica.SIunits.Pressure P "Pressure";
      annotation(defaultComponentName = "fitting", Icon(coordinateSystem(preserveAspectRatio = true, extent = {{-100, -100}, {100, 100}}), graphics = {Rectangle(extent = {{-100, 100}, {100, -100}}, lineColor = {0, 0, 255}, fillColor = {0, 0, 255}, fillPattern = FillPattern.Solid)}), Diagram(coordinateSystem(preserveAspectRatio = true, extent = {{-100, -100}, {100, 100}}), graphics = {Rectangle(extent = {{-40, 40}, {40, -40}}, lineColor = {0, 0, 255}, fillColor = {0, 0, 255}, fillPattern = FillPattern.Solid), Text(extent = {{-160, 110}, {40, 50}}, lineColor = {0, 0, 255}, textString = "%name")}));
    end Fitting;
    end Interfaces;

  model SourceConstant
    parameter Modelica.SIunits.MassFlowRate q = 0.015 "Flow rate [Kg/s]";

    Interfaces.Fitting fitting annotation(Placement(visible = true, transformation(origin = {100, -6}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {100, -6}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    equation
    -fitting.q = q;
    annotation(Icon(graphics = {Rectangle(origin = {-3, 30}, extent = {{-93, 60}, {93, -60}}), Rectangle(origin = {0, 15}, fillColor = {0, 0, 255}, fillPattern = FillPattern.Solid, extent = {{-90, 45}, {90, -45}})}), Diagram);
  end SourceConstant;

  model ZeroP 
    parameter Modelica.SIunits.Pressure p = 0.00 "Pressure";
    Interfaces.Fitting fitting annotation(Placement(visible = true, transformation(origin = {100, -6}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {100, -6}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    equation
    fitting.P = p;
    annotation(Icon(graphics = {Rectangle(origin = {-1, 2}, extent = {{-95, 94}, {95, -94}})}), Diagram);
  end ZeroP;

  model TestTrap1
  TrapTemp trap1        (  level(start = 0.0509)) annotation(Placement(visible = true, transformation(origin = {10, 8}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  SourceConstant  sourceConstant1(q =  0.03) annotation(Placement(visible = true, transformation(origin = {-50, 26}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  ZeroP zeroP1 (p = 101000) annotation(Placement(visible = true, transformation(origin = {64, 8}, extent = {{10, -10}, {-10, 10}}, rotation = 0)));
  equation
  connect(sourceConstant1.fitting, trap1.fitting_in) annotation(Line(points = {{-40, 26}, {-18, 26}, {-18, 8}, {0, 8}, {0, 8}}, color = {0, 0, 255}));
  connect(trap1.fitting_out, zeroP1.fitting) annotation(Line(points = {{20, 8}, {54, 8}, {54, 8}, {54, 8}}, color = {0, 0, 255}));
  annotation(Icon, Diagram, experiment(StartTime = 0, StopTime = 1450, Tolerance = 1e-6, Interval = 2.9));
  end TestTrap1;

  model TrapTemp
  //Calculate the velocity for pipe segments
 Interfaces.Fitting fitting_in annotation(Placement(visible = true, transformation(origin = {-100, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-100, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Interfaces.Fitting fitting_out annotation(Placement(visible = true, transformation(origin = {98, -2}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {98, -2}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  import m = Modelica.Math;
    parameter Modelica.SIunits.Length L = 25000 "pipe length (m)";
    parameter Modelica.SIunits.Radius R = 0.10194 / 2.0 "pipe inner radius (m)";
    parameter Modelica.SIunits.Height roughness = 6.0e-5 "Average height of surface asperities (default: smooth steel pipe)";
    /*Values below are liquid at 300K*/
    parameter Modelica.SIunits.DynamicViscosity mu = 0.092977 "Dynamic Viscocity (cp)";
    parameter Modelica.SIunits.Density row = 501.22 "Density (kg/m^3)";
    Modelica.SIunits.Area A;
    Modelica.SIunits.Length s(start = 0.158) "wetted perimeter";
    Modelica.SIunits.Length rh "hydraulic radius";
    Modelica.SIunits.Diameter De(start = 0.1) "eqivalent diameter [m]";
    Modelica.SIunits.ReynoldsNumber Re(start = 3000);
    Modelica.SIunits.Velocity v(start = 0.0001);
    Modelica.SIunits.Length hf "Friction head loss";
    Modelica.SIunits.Acceleration g = Modelica.Constants.g_n;
    Modelica.SIunits.Acceleration a "Acceleration of flow";
    Modelica.SIunits.CoefficientOfFriction f;
    Real relative_roughness;
    Real percent_full "Fraction of pipe is full [%]";
    Modelica.SIunits.Mass mass;
    Real percent_full "Fraction of pipe is full [%]";
  Modelica.SIunits.Height level  "Liquid level (m)";
  equation
  percent_full = level / R * 100.0;
    relative_roughness = roughness / De;
  a = der(v);
  s = 2 * level + R;
  rh = A / s;
  De = 4 * rh;
  //Protect against negative Re:
    Re = De * abs(v) * row / mu;
  //Head loss due to friction.  Corrected for direction
    hf = sign(v) * f * L * v ^ 2 / (2 * De * g);
  f = 64 / Re ;
    mass = A * row * L;
  fitting_in.q + fitting_out.q = der(mass);
  fitting_out.q = -v * A * row;
    mass * a = A * (fitting_in.P - fitting_out.P - hf * row * g ) ;
  A = R * level;
  if  noEvent(level >= R) then
  //full pipe
      fitting_in.q = -fitting_out.q;
    else
  //partially full pipe
  fitting_in.P = fitting_out.P;
  end if;
  annotation(Icon, Diagram);
  end TrapTemp;
  annotation(Icon, Diagram);
end FlowPackage;

如果您能提出任何建议,说明为什么这不起作用,或者如何解决它,我们将不胜感激。

首先,您需要定义您想要的是什么,我的意思是,如果您想在输送流体之前开始在管道中储存流体,您需要定义一组储存液体的质量和能量方程,也就是说:如果你想储存液体,你不能计算Re,因为v=0,一旦填充,就用一个条件交换模型,并定义navier-stokes方程来定义流体的动量。
无论如何,我认为您应该尝试将一个质量流量源、一个储罐、一个阀门(根据储罐液位定义阀门的开启规律)、一个管道和一个水槽与Modelica标准库组件连接起来。

只是为了澄清:我希望在通道中流动(因此它有v和Re),直到通道填满,然后,它应该表现为一个完整的管道(所以现在可以有一个压力差通过它)。我可以让他们各自独立工作,但我在从一个过渡到另一个的过程中遇到了麻烦。