Modelica Flow Simulation says divide by zero

故事扮演 提交于 2019-12-12 06:55:44

问题


I am trying to simulate the flow in a pipe that starts out partially full, and gradually fills up. Once it is full, the flow equation needs to change. I have tried the following code, but I get a divide by zero error once the pipe is full. TestTrap1 is a demo that shows the problem. It seems that some equation is not correct once the pipe is full. I am using openmodelica to run this.

    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;

Any suggestions about why this doesn't work, or how to fix it would be greatly appreciated.


回答1:


firstly, you need to define what is what you want, I mean, if you want to start storing fluid within the pipe before transferring it, you need to define the set of mass and energy equations to store liquid, i.e: if you want to store liquid you cannot calculate Re, because v=0, and once is filled, swap the model as you did with one conditional and define the navier-stokes equations to define the momentum of the fluid. Anyway, I think you should try connecting one mass flow rate source, one tank, one valve (define the opening law for the valve in function of the level of the tank), one pipe and one sink with the Modelica Standard library components.



来源:https://stackoverflow.com/questions/35022856/modelica-flow-simulation-says-divide-by-zero

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